下面列出了android.support.test.filters.SdkSuppress#androidx.lifecycle.Lifecycle.Event 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout layout = new FrameLayout(this);
layout.setId(R.id.fragment_container);
setContentView(layout);
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_CREATE));
getLifecycle().addObserver(mTestObserver);
}
@Override
protected void onPause() {
super.onPause();
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_PAUSE));
// helps with less flaky API 16 tests.
overridePendingTransition(0, 0);
}
@Test
public void testPressHomeButton() throws Throwable {
setupObserverOnResume();
Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
NavigationDialogActivity.class.getCanonicalName(), null, false);
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.addMonitor(monitor);
NavigationTestActivityFirst activity = activityTestRule.getActivity();
activity.startActivity(new Intent(activity, NavigationDialogActivity.class));
FragmentActivity dialogActivity = (FragmentActivity) monitor.waitForActivity();
checkProcessObserverSilent(dialogActivity);
List<Event> events = Collections.synchronizedList(new ArrayList<>());
LifecycleObserver collectingObserver = new LifecycleObserver() {
@OnLifecycleEvent(Event.ON_ANY)
public void onStateChanged(@SuppressWarnings("unused") LifecycleOwner provider,
Event event) {
events.add(event);
}
};
addProcessObserver(collectingObserver);
events.clear();
assertThat(activity.moveTaskToBack(true), is(true));
Thread.sleep(ProcessLifecycleOwner.TIMEOUT_MS * 2);
assertThat(events.toArray(), is(new Event[]{ON_PAUSE, ON_STOP}));
events.clear();
Context context = InstrumentationRegistry.getContext();
context.startActivity(new Intent(activity, NavigationDialogActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
waitTillResumed(dialogActivity, activityTestRule);
assertThat(events.toArray(), is(new Event[]{ON_START, ON_RESUME}));
removeProcessObserver(collectingObserver);
dialogActivity.finish();
}
@Test
public void testFullLifecycle() throws Throwable {
CollectingLifecycleOwner owner = activityTestRule.getActivity();
TestUtils.waitTillResumed(owner, activityTestRule);
activityTestRule.finishActivity();
TestUtils.waitTillDestroyed(owner, activityTestRule);
List<Pair<TestEvent, Event>> results = owner.copyCollectedEvents();
assertThat(results, is(flatMap(CREATE, START, RESUME, PAUSE, STOP, DESTROY)));
}
@Test
public void testFullLifecycle() throws InterruptedException {
State currentState = ProcessLifecycleOwner.get().getLifecycle().getCurrentState();
assertThat(currentState, is(CREATED));
activityTestRule.launchActivity(null);
List<Pair<TestEventType, Event>> events = SimpleAppLifecycleTestActivity.awaitForEvents();
assertThat("Failed to await for events", events, notNullValue());
//noinspection ConstantConditions
assertThat(events.subList(0, 6).toArray(), is(EXPECTED_EVENTS_CONSTRUCTION));
// TODO: bug 35122523
for (Pair<TestEventType, Event> event: events.subList(6, 11)) {
assertThat(event, isIn(EXPECTED_EVENTS_DESTRUCTION));
}
}
@Test
public void testOnCreateCall() throws Throwable {
testSynchronousCall(Event.ON_CREATE,
activity -> {
},
activity -> getInstrumentation().callActivityOnCreate(activity, null));
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStartCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_START,
activity -> getInstrumentation().callActivityOnCreate(activity, null),
SynchronousActivityLifecycleTest::performStart);
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnResumeCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_RESUME,
activity -> {
getInstrumentation().callActivityOnCreate(activity, null);
performStart(activity);
},
SynchronousActivityLifecycleTest::performResume);
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStopCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_STOP,
activity -> {
getInstrumentation().callActivityOnCreate(activity, null);
performStart(activity);
},
SynchronousActivityLifecycleTest::performStop);
}
public void testSynchronousCall(Event event, ActivityCall preInit, ActivityCall call)
throws Throwable {
uiThreadTestRule.runOnUiThread(() -> {
Intent intent = new Intent();
ComponentName cn = new ComponentName(LifecycleTestActivity.class.getPackage().getName(),
LifecycleTestActivity.class.getName());
intent.setComponent(cn);
Instrumentation instrumentation = getInstrumentation();
try {
Application app =
(Application) instrumentation.getTargetContext().getApplicationContext();
LifecycleTestActivity testActivity =
(LifecycleTestActivity) instrumentation.newActivity(
LifecycleTestActivity.class, instrumentation.getTargetContext(),
null, app, intent, new ActivityInfo(), "bla", null, null, null);
preInit.call(testActivity);
TestObserver testObserver = new TestObserver(testActivity, event);
testActivity.getLifecycle().addObserver(testObserver);
testObserver.unmute();
call.call(testActivity);
assertThat(testObserver.mEventReceived, is(true));
} catch (Exception e) {
throw new Error(e);
}
});
}
@Override
public void onStateChanged(LifecycleOwner lifecycleOwner, Event event) {
if (mMuted) {
return;
}
assertThat(event, is(mExpectedEvent));
assertThat(mActivity.mLifecycleCallFinished, is(true));
mEventReceived = true;
}
private void awaitAndAssertEvents(Event... events) throws InterruptedException {
//noinspection SynchronizeOnNonFinalField
synchronized (mLoggerEvents) {
int retryCount = 0;
while (mLoggerEvents.size() < events.length && retryCount++ < RETRY_NUMBER) {
mLoggerEvents.wait(TIMEOUT);
}
assertThat(mLoggerEvents, is(Arrays.asList(events)));
}
}
@Override
public void onReceive(Context context, Intent intent) {
synchronized (mLoggerEvents) {
mLoggerEvents.add((Event) intent.getSerializableExtra(TestService.EXTRA_KEY_EVENT));
mLoggerEvents.notifyAll();
}
}
@OnLifecycleEvent(Event.ON_RESUME)
private void onResume() {
continueUpdate();
}
@OnLifecycleEvent(Event.ON_DESTROY)
private void onDestroy() {
unregisterListener();
}
@Override
protected void onStart() {
super.onStart();
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_START));
}
@Override
protected void onResume() {
super.onResume();
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_RESUME));
}
@Override
protected void onDestroy() {
super.onDestroy();
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_DESTROY));
}
@Override
protected void onStop() {
super.onStop();
mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_STOP));
}
@Override
public List<Pair<TestEvent, Event>> copyCollectedEvents() {
return new ArrayList<>(mCollectedEvents);
}
TestObserver(List<Pair<TestEvent, Event>> collectedEvents) {
mCollectedEvents = collectedEvents;
}
@OnLifecycleEvent(Event.ON_ANY)
void onEvent() {
mChangedState = true;
}
@Test
public void testOnDestroyCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_DESTROY,
activity -> getInstrumentation().callActivityOnCreate(activity, null),
activity -> getInstrumentation().callActivityOnDestroy(activity));
}
private TestObserver(LifecycleTestActivity activity, Event expectedEvent) {
this.mActivity = activity;
this.mExpectedEvent = expectedEvent;
}
private EventLogger(List<Event> loggerEvents) {
mLoggerEvents = loggerEvents;
}
@Override
public void onStateChanged(LifecycleOwner source, Event event) {
// 调用 ClassesInfoCache.CallbackInfo.invokeCallbacks()
mInfo.invokeCallbacks(source, event, mWrapped);
}
@OnLifecycleEvent(ON_ANY)
public void onStop(Event e2, Event event) {
}
@OnLifecycleEvent(ON_ANY)
void any(LifecycleOwner provider, Event event){}
@OnLifecycleEvent(ON_STOP)
public void onStop(LifecycleOwner provider, Event event) {
}
@OnLifecycleEvent(ON_STOP)
public void onStop(Event event) {
}