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

The following examples show how to use org.robolectric.android.controller.ActivityController#get() . 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: AnimatorUtilityTests.java    From scene with Apache License 2.0 6 votes vote down vote up
@Test
public void testResetViewStatus() {
    ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
    NavigationSourceUtility.TestActivity testActivity = controller.get();
    View view = new View(testActivity);
    view.setTranslationX(1);
    view.setTranslationY(1);
    view.setScaleX(2.0f);
    view.setScaleY(2.0f);
    view.setRotation(1.0f);
    view.setRotationX(1.0f);
    view.setRotationY(1.0f);
    view.setAlpha(0.5f);
    view.startAnimation(new AlphaAnimation(0.0f, 1.0f));

    AnimatorUtility.resetViewStatus(view);
    assertEquals(0.0f, view.getTranslationX(), 0.0f);
    assertEquals(0.0f, view.getTranslationY(), 0.0f);
    assertEquals(1.0f, view.getScaleX(), 0.0f);
    assertEquals(1.0f, view.getScaleY(), 0.0f);
    assertEquals(0.0f, view.getRotation(), 0.0f);
    assertEquals(0.0f, view.getRotationX(), 0.0f);
    assertEquals(0.0f, view.getRotationY(), 0.0f);
    assertEquals(1.0f, view.getAlpha(), 0.0f);
    assertNull(view.getAnimation());
}
 
Example 2
Source File: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void correctFragmentsInTabs()
{
    ActivityController activityController = Robolectric.buildActivity(TransactionActivity.class).create();

    Activity activity = (Activity)activityController.get();
    activityController.start();
    activityController.resume();

    final ViewPager viewPager = (ViewPager) activity.findViewById(R.id.pager);
    assertNotNull(viewPager);
    final FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter)viewPager.getAdapter();
    assertNotNull(adapter);
    assertEquals(2, adapter.getCount());

    checkFragmentStatus(activity, 0);
    checkFragmentStatus(activity, 1);
}
 
Example 3
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void startAsAddCaptureReceiptFailureCreateExpense() throws IOException
{
    ActivityController activityController = setupActivity("budget", null, false, false);

    // Add something that will 'handle' the media capture intent
    registerMediaStoreIntentHandler();

    Activity activity = (Activity)activityController.get();

    checkAllFields(activity, "", "", "budget","", "", "", nowString, "", false, false);

    // Complete image capture in failure
    Uri imageLocation = captureImageWithResult(activity, R.id.captureButton, false, ORIGINAL_JPEG_QUALITY);

    checkAllFields(activity, "", "", "budget", "", "", "", nowString, "", false, false);

    // Save and check the gift card
    saveExpenseWithArguments(activity, "name", "account", "budget", 100, "note",
            nowString, nowMs, "", true);

    // Check that no file was created
    File imageFile = new File(imageLocation.getPath());
    assertEquals(false, imageFile.exists());
}
 
Example 4
Source File: NavigationSceneUtilityTests.java    From scene with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetupInActivityOnStart() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().start();
    TestActivity activity = controller.get();
    SceneDelegate sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).build();
    NavigationScene navigationScene = sceneDelegate.getNavigationScene();
    assertNotNull(navigationScene);
    assertEquals(State.STARTED, navigationScene.getState());
    controller.resume();
    assertEquals(State.RESUMED, navigationScene.getState());
    controller.pause();
    assertEquals(State.STARTED, navigationScene.getState());
    controller.stop();
    assertEquals(State.ACTIVITY_CREATED, navigationScene.getState());
    controller.destroy();
    assertEquals(State.NONE, navigationScene.getState());
}
 
Example 5
Source File: NavigationSceneUtilityTests.java    From scene with Apache License 2.0 6 votes vote down vote up
@LooperMode(PAUSED)
@Test
public void testNotImmediateSetupInActivityOnPause() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().start().resume().pause();
    TestActivity activity = controller.get();
    SceneDelegate sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).immediate(false).build();
    NavigationScene navigationScene = sceneDelegate.getNavigationScene();
    assertNotNull(navigationScene);

    assertEquals(State.NONE, navigationScene.getState());
    shadowOf(getMainLooper()).idle();//execute Handler posted task
    assertEquals(State.STARTED, navigationScene.getState());

    controller.stop();
    assertEquals(State.ACTIVITY_CREATED, navigationScene.getState());
    controller.destroy();
    assertEquals(State.NONE, navigationScene.getState());
}
 
Example 6
Source File: NavigationSceneUtilityTests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Test
public void testDrawWindowBackground() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().resume();
    TestActivity activity = controller.get();
    SceneDelegate sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).fixSceneWindowBackgroundEnabled(true).build();
    assertNotNull(sceneDelegate.getNavigationScene().requireView().getBackground());
    sceneDelegate.abandon();

    sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).drawWindowBackground(false).build();
    assertNull(sceneDelegate.getNavigationScene().requireView().getBackground());
}
 
Example 7
Source File: RobolectricTestBase.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private <T extends Form> void setUpForm(Class<T> clazz) {
  ShadowLooper.getShadowMainLooper().getScheduler().setIdleState(IdleState.PAUSED);
  ActivityController<T> activityController = Robolectric.buildActivity(clazz)
      .create().start().resume().visible();
  form = activityController.get();
  // Unfortunately Robolectric won't handle laying out the view hierarchy and because of how
  // we use runOnUiThread in the Initialize() method, tests will enter an infinite loop. This
  // code simulates enough of the layout process so that we don't loop forever.
  View v = ((ViewGroup) form.findViewById(android.R.id.content)).getChildAt(0);
  v = ((ViewGroup) v).getChildAt(1);  // frameLayoutWithTitle in Form
  v = ((ViewGroup) v).getChildAt(0);  // frameLayout in Form
  v.layout(0, 0, 240, 320);
  v.measure(240, 320);
  v.invalidate();
  v.requestLayout();
  ShadowLooper.getShadowMainLooper().getScheduler().setIdleState(IdleState.UNPAUSED);
  ShadowEventDispatcher.clearEvents();  // start with a clean slate
}
 
Example 8
Source File: NavigationSceneUtilityTests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Config(minSdk = Build.VERSION_CODES.JELLY_BEAN_MR1, maxSdk = Build.VERSION_CODES.P)
@Test
public void testSetupInActivityOnDestroy() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().start().resume().pause().stop().destroy();
    TestActivity activity = controller.get();
    SceneDelegate sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).build();
    NavigationScene navigationScene = sceneDelegate.getNavigationScene();
    assertNotNull(navigationScene);
    assertEquals(State.NONE, navigationScene.getState());
}
 
Example 9
Source File: BudgetViewActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startAsViewCheckDisplay() throws IOException
{
    ActivityController activityController = setupActivity("budgetName", 100, true, false);

    Activity activity = (Activity)activityController.get();

    checkAllFields(activity, ViewMode.VIEW_CARD, "budgetName", "100");
}
 
Example 10
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startAsAddCaptureReceiptCancel() throws IOException
{
    ActivityController activityController = setupActivity("budget", null, false, false);

    // Add something that will 'handle' the media capture intent
    registerMediaStoreIntentHandler();

    Activity activity = (Activity)activityController.get();

    checkAllFields(activity, "", "", "budget", "", "", "", nowString, "", false, false);

    // Complete image capture successfully
    Uri imageLocation = captureImageWithResult(activity, R.id.captureButton, true, ORIGINAL_JPEG_QUALITY);

    checkAllFields(activity, "", "", "budget", "", "", "", nowString, "", true, false);

    // Ensure that the file still exists
    File imageFile = new File(imageLocation.getPath());
    assertTrue(imageFile.isFile());

    // Cancel the expense creation
    assertEquals(false, activity.isFinishing());
    shadowOf(activity).clickMenuItem(android.R.id.home);
    assertEquals(true, activity.isFinishing());
    activityController.destroy();

    // Ensure the image has been deleted
    assertEquals(false, imageFile.exists());
}
 
Example 11
Source File: GodEyeHelperTest.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Test
public void onPageLoadedSuccessForActivity() {
    try {
        GodEye.instance().uninstall();
        GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
        ActivityController<Test1Activity> activityController = Robolectric.buildActivity(Test1Activity.class).create().start().resume();
        Test1Activity activity = activityController.get();
        TestObserver testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
        GodEyeHelper.onPageLoaded(activity);
        activityController.pause().stop().destroy();
        Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();

        List<TestPageEvent> testPageEvents = new ArrayList<>();
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_START, 2));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DRAW, 3));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_RESUME, 4));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_LOAD, 5));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 6));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 7));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 8));
        testObserver.assertValueCount(8);
        for (int i = 0; i < 8; i++) {
            int finalI = i;
            testObserver.assertValueAt(i, new Predicate<PageLifecycleEventInfo>() {
                @Override
                public boolean test(PageLifecycleEventInfo o) throws Exception {
                    return testPageEvents.get(finalI).pageHashCode == o.pageInfo.pageHashCode
                            && testPageEvents.get(finalI).allEventSize == o.allEvents.size()
                            && testPageEvents.get(finalI).lifecycleEvent.equals(o.currentEvent.lifecycleEvent);
                }
            });
        }
    } catch (UninstallException e) {
        fail();
    }
}
 
Example 12
Source File: SceneLifecycleManagerExceptionTests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testNPE2() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().start().resume();
    TestActivity testActivity = controller.get();
    SceneLifecycleManager<NavigationScene> sceneLifecycleManager = new SceneLifecycleManager<>();
    sceneLifecycleManager.onActivityCreated(testActivity, testActivity.mFrameLayout,
            null, null,
            false, null);

}
 
Example 13
Source File: NavigationSceneUtilityTests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetupInActivityOnResume() {
    ActivityController<TestActivity> controller = Robolectric.buildActivity(TestActivity.class).create().start().resume();
    TestActivity activity = controller.get();
    SceneDelegate sceneDelegate = NavigationSceneUtility.setupWithActivity(activity, TestScene.class).build();
    NavigationScene navigationScene = sceneDelegate.getNavigationScene();
    assertNotNull(navigationScene);
    assertEquals(State.RESUMED, navigationScene.getState());
    controller.pause();
    assertEquals(State.STARTED, navigationScene.getState());
    controller.stop();
    assertEquals(State.ACTIVITY_CREATED, navigationScene.getState());
    controller.destroy();
    assertEquals(State.NONE, navigationScene.getState());
}
 
Example 14
Source File: SceneLifecycleManagerExceptionTests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipOnStartOnResumeOnPauseOnStop() {
    final Scene scene = new Scene() {
        @NonNull
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedInstanceState) {
            return new View(requireSceneContext());
        }
    };
    ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
    NavigationSourceUtility.TestActivity testActivity = controller.get();
    NavigationScene navigationScene = new NavigationScene();
    NavigationSceneOptions options = new NavigationSceneOptions(scene.getClass());
    navigationScene.setArguments(options.toBundle());

    Scope.RootScopeFactory rootScopeFactory = new Scope.RootScopeFactory() {
        @Override
        public Scope getRootScope() {
            return Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
        }
    };

    SceneComponentFactory sceneComponentFactory = new SceneComponentFactory() {
        @Override
        public Scene instantiateScene(ClassLoader cl, String className, Bundle bundle) {
            if (className.equals(scene.getClass().getName())) {
                return scene;
            }
            return null;
        }
    };

    navigationScene.setDefaultNavigationAnimationExecutor(null);
    navigationScene.setRootSceneComponentFactory(sceneComponentFactory);
    SceneLifecycleManager<NavigationScene> sceneLifecycleManager = new SceneLifecycleManager<>();
    sceneLifecycleManager.onActivityCreated(testActivity, testActivity.mFrameLayout,
            navigationScene, rootScopeFactory, false, null);
    sceneLifecycleManager.onDestroyView();
}
 
Example 15
Source File: PresenterActivityTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
protected void startActivity(@NonNull final Intent intent) {
    final ActivityController<ActivityT> controller = Robolectric.buildActivity(
            getActivityType(), intent);
    this.activity = controller.get();
    this.presenter = mock(getPresenterType());
    activity.presenter = presenter;
    controller.setup();
    this.view = activity.view;
}
 
Example 16
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithoutParametersCaptureReceiptFailureCreateGiftCard() throws IOException
{
    ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
    activityController.start();
    activityController.visible();
    activityController.resume();

    // Add something that will 'handle' the media capture intent
    registerMediaStoreIntentHandler();

    Activity activity = (Activity)activityController.get();

    checkAllFields(activity, "", "", "", "", false);

    // Complete image capture in failure
    Uri imageLocation = captureImageWithResult(activity, R.id.captureButton, false);

    checkAllFields(activity, "", "", "", "", false);

    // Save and check the gift card
    saveGiftCardWithArguments(activity, "store", "cardId", "value", "", true);

    // Check that no file was created
    File imageFile = new File(imageLocation.getPath());
    assertEquals(false, imageFile.exists());
}
 
Example 17
Source File: SaveAndRestoreTests.java    From scene with Apache License 2.0 5 votes vote down vote up
public static NavigationScene createNavigationScene(final Scene rootScene) {
    SceneLifecycleManager<NavigationScene> sceneLifecycleManager = new SceneLifecycleManager<>();
    NavigationScene navigationScene = new NavigationScene();
    ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
    NavigationSourceUtility.TestActivity testActivity = controller.get();
    NavigationSceneOptions options = new NavigationSceneOptions(TestScene.class);
    navigationScene.setArguments(options.toBundle());

    Scope.RootScopeFactory rootScopeFactory = new Scope.RootScopeFactory() {
        @Override
        public Scope getRootScope() {
            return Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
        }
    };

    navigationScene.setDefaultNavigationAnimationExecutor(null);
    navigationScene.setRootSceneComponentFactory(new SceneComponentFactory() {
        @Override
        public Scene instantiateScene(ClassLoader cl, String className, Bundle bundle) {
            return rootScene;
        }
    });
    sceneLifecycleManager.onActivityCreated(testActivity, testActivity.mFrameLayout,
            navigationScene, rootScopeFactory, true, null);

    sceneLifecycleManager.onStart();
    sceneLifecycleManager.onResume();
    return navigationScene;
}
 
Example 18
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startAsEditWithExpensedWithReceiptUpdateReceipt() throws IOException
{
    ActivityController activityController = setupActivity("budget", "receipt", false, true);
    Activity activity = (Activity)activityController.get();

    checkAllFields(activity, "description", "account", "budget", "budget", "100.10", "note", nowString,
            "receipt", false, false);

    // Add something that will 'handle' the media capture intent
    registerMediaStoreIntentHandler();

    // Complete image capture successfully
    Uri imageLocation = captureImageWithResult(activity, R.id.updateButton, true, ORIGINAL_JPEG_QUALITY);

    checkAllFields(activity, "description", "account", "budget", "budget", "100.10", "note", nowString,
            "receipt", true, false);

    // Save and check the expense
    saveExpenseWithArguments(activity, "name", "account", "budget", 100, "note",
            nowString, nowMs, imageLocation.getPath(), false);

    // Ensure that the file still exists
    File imageFile = new File(imageLocation.getPath());
    assertTrue(imageFile.isFile());

    // Delete the file to cleanup
    boolean result = imageFile.delete();
    assertTrue(result);
}
 
Example 19
Source File: SceneLifecycleManagerExceptionTests.java    From scene with Apache License 2.0 4 votes vote down vote up
@Test
public void testTranslucentActivity() {
    final Scene scene = new Scene() {
        @NonNull
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedInstanceState) {
            return new View(requireSceneContext());
        }
    };
    ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
    NavigationSourceUtility.TestActivity testActivity = controller.get();
    NavigationScene navigationScene = new NavigationScene();
    NavigationSceneOptions options = new NavigationSceneOptions(scene.getClass());
    navigationScene.setArguments(options.toBundle());

    Scope.RootScopeFactory rootScopeFactory = new Scope.RootScopeFactory() {
        @Override
        public Scope getRootScope() {
            return Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
        }
    };

    SceneComponentFactory sceneComponentFactory = new SceneComponentFactory() {
        @Override
        public Scene instantiateScene(ClassLoader cl, String className, Bundle bundle) {
            if (className.equals(scene.getClass().getName())) {
                return scene;
            }
            return null;
        }
    };

    navigationScene.setDefaultNavigationAnimationExecutor(null);
    navigationScene.setRootSceneComponentFactory(sceneComponentFactory);
    SceneLifecycleManager<NavigationScene> sceneLifecycleManager = new SceneLifecycleManager<>();
    sceneLifecycleManager.onActivityCreated(testActivity, testActivity.mFrameLayout,
            navigationScene, rootScopeFactory, false, null);
    sceneLifecycleManager.onStart();
    sceneLifecycleManager.onResume();
    sceneLifecycleManager.onPause();
    sceneLifecycleManager.onStop();
    sceneLifecycleManager.onStart();
    sceneLifecycleManager.onStop();
    sceneLifecycleManager.onDestroyView();
}
 
Example 20
Source File: SaveAndRestoreTests.java    From scene with Apache License 2.0 4 votes vote down vote up
@Test
public void testParentSceneViewStateBundleShouldNotSaveChildSceneViewState() {
    SceneLifecycleManager<NavigationScene> sceneLifecycleManager = new SceneLifecycleManager<>();
    NavigationScene navigationScene = new NavigationScene();
    ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
    NavigationSourceUtility.TestActivity testActivity = controller.get();
    NavigationSceneOptions options = new NavigationSceneOptions(TestFixIdGroupScene.class);
    navigationScene.setArguments(options.toBundle());

    Scope.RootScopeFactory rootScopeFactory = new Scope.RootScopeFactory() {
        @Override
        public Scope getRootScope() {
            return Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
        }
    };

    navigationScene.setDefaultNavigationAnimationExecutor(null);

    sceneLifecycleManager.onActivityCreated(testActivity, testActivity.mFrameLayout,
            navigationScene, rootScopeFactory, true, null);

    sceneLifecycleManager.onStart();
    sceneLifecycleManager.onResume();

    TestFixIdGroupScene parentGroupScene = (TestFixIdGroupScene) navigationScene.getCurrentScene();
    TestScene childScene = new TestScene();
    parentGroupScene.add(parentGroupScene.id, childScene, "TAG");

    childScene.setValue("Test");
    childScene.getCheckBox().setChecked(true);

    Bundle bundle = new Bundle();
    sceneLifecycleManager.onSaveInstanceState(bundle);
    ArrayList<Bundle> navigationSceneChildrenSceneBundleList = bundle.getParcelableArrayList(ParcelConstants.KEY_NAVIGATION_SCENE_MANAGER_TAG);
    Bundle groupSceneBundle = navigationSceneChildrenSceneBundleList.get(0);
    SparseArray<Parcelable> groupSceneViewStateArray = groupSceneBundle.getSparseParcelableArray(ParcelConstants.KEY_SCENE_VIEWS_TAG);
    //Parent Scene's view state bundle should not have child Scene's view state, child Scene' view state is saved to child Scene's bundle.
    assertNull(groupSceneViewStateArray.get(childScene.requireView().getId()));
    ArrayList<Bundle> groupSceneChildrenSceneBundleList = groupSceneBundle.getParcelableArrayList(ParcelConstants.KEY_GROUP_SCENE_MANAGER_TAG);
    Bundle groupChildSceneBundle = groupSceneChildrenSceneBundleList.get(0);
    SparseArray<Parcelable> childSceneViewStateArray = groupChildSceneBundle.getSparseParcelableArray(ParcelConstants.KEY_SCENE_VIEWS_TAG);
    //Child Scene's view state should be saved in this SparseArray
    assertNotNull(childSceneViewStateArray.get(childScene.requireView().getId()));
}