博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Driving the Activity Lifecycle
阅读量:4313 次
发布时间:2019-06-06

本文共 3756 字,大约阅读时间需要 12 分钟。

Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActivity()) and then manually calling lifecycle methods such as onCreate(). Also widely used were a set of methods in ShadowActivity (for instanceShadowActivity.callOnCreate()) that are precursors to ActivityController.

It was a mess. The ActivityController is a Robolectric API that changes all of this. Its goal is to mimic how Android creates your Activities and drives them through their lifecycle.

ActivityController is a fluent API that was introduced in Robolectric 2.0 and is now required in 2.2. In addition to calling methods like onCreate(), it ensures that the internal state of the Activity is consistent with the lifecycle. This includes attaching the Activity to the Window and making system services like the LayoutInflater available.

//ActivityController是一个流畅的api 从2.2版本开始可用。除了调用像onCreate()方法之外,它保证了actvity的状态和生命周期一致。

What do I do now?

You don't generally create an ActivityController directly. Use Robolectric.buildActivity() to get started. For the most basic of tests where you simply need an initialized Activity, you can often get away with the following line:

//一般通常不会直接创建activitycontroller,而是使用Robolectric.buildActivity方法。如下:

Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).create().get();

This will create a new instance of MyAwesomeActivity and call through the life cycle to onCreate().

//这会创建一个activity的实例,然后调用到oncreate()生命周期方法

Want to check that something happens during onResume() but not onCreate()? Easy!

//想要查看onresume方法里面做了什么而不是oncreate?好办:

ActivityController controller = Robolectric.buildActivity(MyAwesomeActivity.class).create().start(); Activity activity = controller.get(); // assert that something hasn't happened activityController.resume(); // assert it happened!

Similar methods are included for start()pause()stop(), and destroy(). So, if you want to test the full creation lifecycle:

//相似的方法也包括在 start()pause()stop(), and destroy()中

Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).create().start().resume().visible().get();

You can simulate starting the Activity with an intent:

//模拟使用一个intent来开启activity

Intent intent = new Intent(Intent.ACTION_VIEW); Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class).withIntent(intent).create().get();

... or restore saved instance state:

//或者模拟开启一个带有bundle的activity

Bundle savedInstanceState = new Bundle(); Activity activity = Robolectric.buildActivity(MyAwesomeActivity.class) .create() .restoreInstanceState(savedInstanceState) .get();

Check out the  to see more public methods available for your testing needs.

Wait, What's This visible() Nonsense?

//visible()是不是废话啊?

Turns out that in a real Android app, the view hierarchy of an Activity is not attached to the Window until sometime afteronCreate() is called. Until this happens, the Activity's views do not report as visible. This means you can't click on them (amongst other unexpected behavior). The Activity's hierarchy is attached to the Window on a device or emulator afteronPostResume() on the Activity. Rather than make assumptions about when the visibility should be updated, Robolectric puts the power in the developer's hands when writing tests.

//当oncreate方法调用了activity仍然没有visible,这意味着你不能点击(或者其他的用户行为)。hierarche被添加到window上是直到onPostresume()方法被调用,这时候才可以和用户交互。robolectric把这个更改状态的权利交到开发者手中

So when do you call it? Whenever you're interacting with the views inside the Activity. Methods like Robolectric.clickOn()require that the view is visible and properly attached in order to function. You should call visible() after create().

//所以我们什么时候调用它呢?当你要和activity中的view交互时,你应该在create之后调用visible

转载于:https://www.cnblogs.com/yexiant/p/5692267.html

你可能感兴趣的文章
iOS - Actions and Outlets
查看>>
mysql 查询开销
查看>>
notepad++列模式
查看>>
[ios基础]IOS应用程序的生命周期问题
查看>>
python 之tornado 入门
查看>>
U3D的控制
查看>>
js浮点乘除法运算不精确bug
查看>>
成为java高手的25个目标
查看>>
200道物理学难题——001 三只蜗牛
查看>>
构建App首页
查看>>
BZOJ1443: [JSOI2009]游戏Game
查看>>
zookeeper客户端Watcher管理
查看>>
第二代蜂窝移动通信系统概述
查看>>
[转载] IOS 自动布局--先进的自动布局工具箱
查看>>
Matrix的一些知识
查看>>
sqlserver数据库中没有维护计划,使用windows默认的计划任务实现数据备份
查看>>
python __future__ 使用
查看>>
OpenGL4.6+vs2017+CMake+Glad+Glfw-3.2.1+GLM随手记一发完整版OpenGL配置过程
查看>>
第15周个人进度条
查看>>
Vue学习笔记:methods、computed、watch的区别
查看>>