Java Code Examples for org.robolectric.android.controller.ActivityController#create()

The following examples show how to use org.robolectric.android.controller.ActivityController#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RibActivityTest.java    From RIBs with Apache License 2.0 6 votes vote down vote up
@Test
public void onCreate_withSaveInstanceState_shouldForwardToRootRiblet() {
  android.os.Bundle interactorBundle = new android.os.Bundle();
  interactorBundle.putString(TEST_BUNDLE_KEY, TEST_BUNDLE_VALUE);

  android.os.Bundle testBundle = new android.os.Bundle();
  testBundle.putBundle(Router.KEY_INTERACTOR, interactorBundle);

  ActivityController<EmptyActivity> activityController =
      Robolectric.buildActivity(EmptyActivity.class);
  activityController.create(testBundle);

  assertThat(
          activityController
              .get()
              .getTestInteractor()
              .getSavedInstanceState()
              .getString(TEST_BUNDLE_KEY))
      .isEqualTo(TEST_BUNDLE_VALUE);
}
 
Example 2
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testLifecycle(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Lifecycle.Event> testObserver = provider.lifecycle().test();

    controller.create();
    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();

    testObserver.assertValues(
            Lifecycle.Event.ON_CREATE,
            Lifecycle.Event.ON_START,
            Lifecycle.Event.ON_RESUME,
            Lifecycle.Event.ON_PAUSE,
            Lifecycle.Event.ON_STOP,
            Lifecycle.Event.ON_DESTROY
    );
}
 
Example 3
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> activity = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test();

    controller.create();
    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
Example 4
Source File: RxActivityLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testLifecycle(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) {
    LifecycleProvider<ActivityEvent> activity = controller.get();

    TestObserver<ActivityEvent> testObserver = activity.lifecycle().test();

    controller.create();
    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();

    testObserver.assertValues(
        ActivityEvent.CREATE,
        ActivityEvent.START,
        ActivityEvent.RESUME,
        ActivityEvent.PAUSE,
        ActivityEvent.STOP,
        ActivityEvent.DESTROY
    );
}
 
Example 5
Source File: RxActivityLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) {
    LifecycleProvider<ActivityEvent> activity = controller.get();

    TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(STOP)).test();

    controller.create();
    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
Example 6
Source File: TestActivityTest.java    From Awesome-WanAndroid with Apache License 2.0 5 votes vote down vote up
@Test
public void testActivityLifecycle() {
    ActivityController<TestActivity> activityController =
            Robolectric.buildActivity(TestActivity.class);
    TestActivity testActivity = activityController.get();
    Assert.assertNull(testActivity.getLifeCycleStatus());

    activityController.create();
    Assert.assertEquals("onCreate", testActivity.getLifeCycleStatus());

    activityController.start();
    Assert.assertEquals("onStart", testActivity.getLifeCycleStatus());

    activityController.resume();
    Assert.assertEquals("onResume", testActivity.getLifeCycleStatus());

    activityController.pause();
    Assert.assertEquals("onPause", testActivity.getLifeCycleStatus());

    activityController.stop();
    Assert.assertEquals("onStop", testActivity.getLifeCycleStatus());

    activityController.restart();
    Assert.assertEquals("onStart", testActivity.getLifeCycleStatus());

    activityController.destroy();
    Assert.assertEquals("onDestroy", testActivity.getLifeCycleStatus());
}
 
Example 7
Source File: RibActivityTest.java    From RIBs with Apache License 2.0 5 votes vote down vote up
@Test
public void onCreate_withNullSaveInstanceState_shouldForwardNullToRootRiblet() {
  ActivityController<EmptyActivity> activityController =
      Robolectric.buildActivity(EmptyActivity.class);
  activityController.create();

  assertThat(activityController.get().getTestInteractor().getSavedInstanceState()).isNull();
}
 
Example 8
Source File: RibActivityTest.java    From RIBs with Apache License 2.0 5 votes vote down vote up
@Test
public void rxActivity_delaySubscription_shouldIgnoreOtherEvents() {
  ActivityController<EmptyActivity> activityController = buildActivity(EmptyActivity.class);
  final RibActivity activity = activityController.get();

  final PublishSubject<Object> subject = PublishSubject.create();
  AndroidRecordingRx2Observer<Object> o = new AndroidRecordingRx2Observer<>();
  subject
      .hide()
      .delaySubscription(
          activity
              .lifecycle()
              .filter(
                  new Predicate<ActivityLifecycleEvent>() {
                    @Override
                    public boolean test(ActivityLifecycleEvent activityEvent) throws Exception {
                      return activityEvent.getType() == ActivityLifecycleEvent.Type.RESUME;
                    }
                  }))
      .subscribe(o);

  subject.onNext(new Object());
  activityController.create();
  subject.onNext(new Object());
  o.assertNoMoreEvents();
  activityController.start();
  subject.onNext(new Object());
  o.assertNoMoreEvents();
  activityController.postCreate(null);
  subject.onNext(new Object());
  o.assertNoMoreEvents();
  activityController.resume();
  subject.onNext(new Object());
  assertThat(o.takeNext()).isNotNull();
  o.assertNoMoreEvents();
}
 
Example 9
Source File: LoginActivityTest.java    From android-auth with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFinishLoginActivityWhenCompleted() {

    Activity context = Robolectric
            .buildActivity(Activity.class)
            .create()
            .get();

    AuthorizationRequest request = new AuthorizationRequest.Builder("test", AuthorizationResponse.Type.TOKEN, "test://test").build();
    AuthorizationResponse response = new AuthorizationResponse.Builder()
            .setType(AuthorizationResponse.Type.TOKEN)
            .setAccessToken("test_token")
            .setExpiresIn(3600)
            .build();

    Bundle bundle = new Bundle();
    bundle.putParcelable(LoginActivity.REQUEST_KEY, request);

    Intent intent = new Intent(context, LoginActivity.class);
    intent.putExtra(LoginActivity.EXTRA_AUTH_REQUEST, bundle);

    ActivityController<LoginActivity> loginActivityActivityController = buildActivity(LoginActivity.class, intent);

    final LoginActivity loginActivity = loginActivityActivityController.get();

    final ShadowActivity shadowLoginActivity = shadowOf(loginActivity);
    shadowLoginActivity.setCallingActivity(context.getComponentName());

    loginActivityActivityController.create();

    assertFalse(loginActivity.isFinishing());

    loginActivity.onClientComplete(response);

    assertTrue(loginActivity.isFinishing());
    assertEquals(Activity.RESULT_OK, shadowLoginActivity.getResultCode());
    assertEquals(response, shadowLoginActivity.getResultIntent().getBundleExtra(LoginActivity.EXTRA_AUTH_RESPONSE).get(LoginActivity.RESPONSE_KEY));
}
 
Example 10
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
private void testBindToLifecycle(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get());

    controller.create();
    TestObserver<Object> createObserver = observable.compose(provider.bindToLifecycle()).test();

    controller.start();
    createObserver.assertNotComplete();
    TestObserver<Object> startObserver = observable.compose(provider.bindToLifecycle()).test();

    controller.resume();
    createObserver.assertNotComplete();
    startObserver.assertNotComplete();
    TestObserver<Object> resumeObserver = observable.compose(provider.bindToLifecycle()).test();

    controller.pause();
    createObserver.assertNotComplete();
    startObserver.assertNotComplete();
    resumeObserver.assertComplete();
    TestObserver<Object> pauseObserver = observable.compose(provider.bindToLifecycle()).test();

    controller.stop();
    createObserver.assertNotComplete();
    startObserver.assertComplete();
    pauseObserver.assertComplete();
    TestObserver<Object> stopObserver = observable.compose(provider.bindToLifecycle()).test();

    controller.destroy();
    createObserver.assertComplete();
    stopObserver.assertComplete();
}
 
Example 11
Source File: AndroidLifecycleFragmentTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
private ActivityController<FragmentActivity> startFragment(Fragment fragment) {
    ActivityController<FragmentActivity> controller = Robolectric.buildActivity(FragmentActivity.class);
    controller.create();
    controller.get().getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, fragment)
            .commitNow();
    return controller;
}
 
Example 12
Source File: RxActivityLifecycleTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
private void testBindToLifecycle(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) {
    LifecycleProvider<ActivityEvent> activity = controller.get();

    controller.create();
    TestObserver<Object> createObserver = observable.compose(activity.bindToLifecycle()).test();

    controller.start();
    createObserver.assertNotComplete();
    TestObserver<Object> startObserver = observable.compose(activity.bindToLifecycle()).test();

    controller.resume();
    createObserver.assertNotComplete();
    startObserver.assertNotComplete();
    TestObserver<Object> resumeObserver = observable.compose(activity.bindToLifecycle()).test();

    controller.pause();
    createObserver.assertNotComplete();
    startObserver.assertNotComplete();
    resumeObserver.assertComplete();
    TestObserver<Object> pauseObserver = observable.compose(activity.bindToLifecycle()).test();

    controller.stop();
    createObserver.assertNotComplete();
    startObserver.assertComplete();
    pauseObserver.assertComplete();
    TestObserver<Object> stopObserver = observable.compose(activity.bindToLifecycle()).test();

    controller.destroy();
    createObserver.assertComplete();
    stopObserver.assertComplete();
}
 
Example 13
Source File: CourseUnitNavigationActivityTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method for testing the setup for the specified orientation
 *
 * @param orientation The orientation to be tested
 */
public void testOrientationSetup(int orientation) {
    ActivityController<? extends CourseUnitNavigationActivity> controller =
            Robolectric.buildActivity(getActivityClass(), getIntent());
    CourseUnitNavigationActivity activity = controller.get();
    activity.getResources().getConfiguration().orientation = orientation;
    controller.create();
    assertOrientationSetup(activity);
}
 
Example 14
Source File: CourseBaseActivityTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Testing initialization
 */
@Test
@SuppressLint("RtlHardcoded")
public void initializeTest() {
    ActivityController<? extends CourseBaseActivity> controller =
            Robolectric.buildActivity(getActivityClass(), getIntent());
    CourseBaseActivity activity = controller.get();

    controller.create();

    controller.postCreate(null).resume().postResume().visible();
}