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

The following examples show how to use org.robolectric.android.controller.ActivityController#start() . 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: AndroidLifecycleFragmentTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(LifecycleOwner owner) {
    Fragment fragment = (Fragment) owner;
    ActivityController<?> controller = startFragment(fragment);

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

    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
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: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void bothTabsExists() throws Exception
{
    ActivityController activityController = Robolectric.buildActivity(TransactionActivity.class).create();

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

    TabLayout tabLayout = (TabLayout) activity.findViewById(R.id.tabLayout);
    assertNotNull(tabLayout);
    assertEquals(2, tabLayout.getTabCount());

    TabLayout.Tab expenseTab = tabLayout.getTabAt(0);
    assertNotNull(expenseTab);
    String expenseTabTitle = activity.getResources().getString(R.string.expensesTitle);
    assertEquals(expenseTabTitle, expenseTab.getText().toString());

    TabLayout.Tab revenueTab = tabLayout.getTabAt(1);
    assertNotNull(revenueTab);
    String revenueTabTitle = activity.getResources().getString(R.string.revenuesTitle);
    assertEquals(revenueTabTitle, revenueTab.getText().toString());
}
 
Example 4
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithoutParametersCreateGiftCardNoReceipt()
{
    ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
    activityController.start();
    activityController.visible();
    activityController.resume();

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

    saveGiftCardWithArguments(activity, "store", "cardId", "value", "", true);
}
 
Example 5
Source File: GroupSceneCompatUtility_SetupWithFragment_Tests.java    From scene with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetupInFragmentLifecycleMethods_Fragment_Added_In_Activity_OnCreate() {
    ActivityController<TestAppCompatActivity> controller = Robolectric.buildActivity(TestAppCompatActivity.class).create();
    TestAppCompatActivity activity = controller.get();
    TestNormalFragment_Add_In_Activity_OnCreate testFragment = new TestNormalFragment_Add_In_Activity_OnCreate();
    activity.getSupportFragmentManager().beginTransaction().add(activity.mFrameLayout.getId(), testFragment).commitNowAllowingStateLoss();
    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();
    assertEquals(6, testFragment.mMethodInvokedCount);
}
 
Example 6
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithGiftCardWithReceiptUpdateReceiptCancel() throws IOException
{
    ActivityController activityController = createActivityWithGiftCard();
    Activity activity = (Activity)activityController.get();
    DBHelper db = new DBHelper(activity);

    db.insertGiftCard("store", "cardId", "value", "receipt");

    activityController.start();
    activityController.visible();
    activityController.resume();

    checkAllFields(activity, "store", "cardId", "value", "receipt", false);

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

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

    checkAllFields(activity, "store", "cardId", "value", "receipt", true);

    // Cancel the gift card creation
    final Button cancelButton = (Button) activity.findViewById(R.id.cancelButton);
    assertEquals(false, activity.isFinishing());
    cancelButton.performClick();
    assertEquals(true, activity.isFinishing());
    activityController.destroy();

    // Ensure the image has been deleted
    File imageFile = new File(imageLocation.getPath());
    assertEquals(false, imageFile.exists());
}
 
Example 7
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithGiftCardWithReceiptUpdateReceipt() throws IOException
{
    ActivityController activityController = createActivityWithGiftCard();
    Activity activity = (Activity)activityController.get();
    DBHelper db = new DBHelper(activity);

    db.insertGiftCard("store", "cardId", "value", "receipt");

    activityController.start();
    activityController.visible();
    activityController.resume();

    checkAllFields(activity, "store", "cardId", "value", "receipt", false);

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

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

    checkAllFields(activity, "store", "cardId", "value", "receipt", true);

    // Save and check the gift card
    saveGiftCardWithArguments(activity, "store", "cardId", "value", 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 8
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithGiftCardWithReceiptCheckDisplay() throws IOException
{
    ActivityController activityController = createActivityWithGiftCard();
    Activity activity = (Activity)activityController.get();
    DBHelper db = new DBHelper(activity);

    db.insertGiftCard("store", "cardId", "value", "receipt");

    activityController.start();
    activityController.visible();
    activityController.resume();

    checkAllFields(activity, "store", "cardId", "value", "receipt", false);
}
 
Example 9
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 10
Source File: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
private void checkClickAddWhileOnTap(Integer tab, int expectedType)
{
    ActivityController activityController = Robolectric.buildActivity(TransactionActivity.class).create();
    Activity activity = (Activity)activityController.get();

    activityController.start();
    activityController.resume();
    activityController.visible();

    if(tab != null)
    {
        final ViewPager viewPager = (ViewPager) activity.findViewById(R.id.pager);
        viewPager.setCurrentItem(tab);
    }

    shadowOf(activity).clickMenuItem(R.id.action_add);

    ShadowActivity shadowActivity = shadowOf(activity);
    Intent startedIntent = shadowActivity.getNextStartedActivity();

    ComponentName name = startedIntent.getComponent();
    assertNotNull(name);
    assertEquals("protect.budgetwatch/.TransactionViewActivity", name.flattenToShortString());
    Bundle bundle = startedIntent.getExtras();
    assertNotNull(bundle);

    // Fields which should not be present
    assertEquals(-1, bundle.getInt("id", -1));
    assertEquals(false, bundle.getBoolean("update", false));
    assertEquals(false, bundle.getBoolean("view", false));

    // Check the field which is expected
    assertEquals(expectedType, bundle.getInt("type", -1));
}
 
Example 11
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
private ActivityController setupActivity(final int actionType, final String budget, final String receipt)
{
    Intent intent = new Intent();

    if(actionType == DBHelper.TransactionDbIds.EXPENSE)
    {
        intent.setAction(TransactionViewActivity.ACTION_NEW_EXPENSE);
    }
    else
    {
        intent.setAction(TransactionViewActivity.ACTION_NEW_REVENUE);
    }

    ActivityController activityController = Robolectric.buildActivity(TransactionViewActivity.class, intent).create();

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

    if(budget != null)
    {
        addBudget(activity, budget);

        if (receipt != null)
        {
            addTransactionForBudget(activity, budget, receipt);
        }
    }

    activityController.start();
    activityController.visible();
    activityController.resume();

    return activityController;
}
 
Example 12
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithoutParametersCaptureReceiptCreateGiftCard() 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 successfully
    Uri imageLocation = captureImageWithResult(activity, R.id.captureButton, true);

    checkAllFields(activity, "", "", "", "", true);

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

    // 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 13
Source File: BudgetActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void addOneBudget()
{
    ActivityController activityController = Robolectric.buildActivity(BudgetActivity.class).create();

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

    TextView helpText = (TextView)mainActivity.findViewById(R.id.helpText);
    ListView list = (ListView)mainActivity.findViewById(R.id.list);

    assertEquals(0, list.getCount());

    assertEquals(View.VISIBLE, helpText.getVisibility());
    assertEquals(View.GONE, list.getVisibility());

    DBHelper db = new DBHelper(mainActivity);
    db.insertBudget("name", 100);
    db.close();

    activityController.pause();
    activityController.resume();

    assertEquals(View.GONE, helpText.getVisibility());
    assertEquals(View.VISIBLE, list.getVisibility());

    assertEquals(1, list.getAdapter().getCount());
    Budget budget = (Budget)list.getAdapter().getItem(0);
    assertNotNull(budget);
    assertEquals("name", budget.name);
    assertEquals(100, budget.max);
    assertEquals(0, budget.current);
}
 
Example 14
Source File: MainActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testClickImportExport()
{
    ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create();
    Activity activity = (Activity)activityController.get();

    activityController.start();
    activityController.resume();
    activityController.visible();

    shadowOf(activity).clickMenuItem(R.id.action_import_export);
    testNextStartedActivity(activity, "protect.budgetwatch/.ImportExportActivity");
}
 
Example 15
Source File: GiftCardViewActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void startWithoutParametersCannotCreateGiftCard()
{
    ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
    activityController.start();
    activityController.visible();
    activityController.resume();

    Activity activity = (Activity)activityController.get();
    DBHelper db = new DBHelper(activity);
    assertEquals(0, db.getGiftCardCount());

    final EditText storeField = (EditText) activity.findViewById(R.id.storeName);
    final EditText cardIdField = (EditText) activity.findViewById(R.id.cardId);
    final TextView receiptField = (TextView) activity.findViewById(R.id.receiptLocation);

    final Button saveButton = (Button) activity.findViewById(R.id.saveButton);

    saveButton.performClick();
    assertEquals(0, db.getGiftCardCount());

    storeField.setText("store");
    saveButton.performClick();
    assertEquals(0, db.getGiftCardCount());

    cardIdField.setText("cardId");
    saveButton.performClick();
    assertEquals(0, db.getGiftCardCount());

    receiptField.setText("receipt");
    saveButton.performClick();
    assertEquals(0, db.getGiftCardCount());
}
 
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 startWithoutParametersCheckFieldsAvailable()
{
    ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
    activityController.start();
    activityController.visible();
    activityController.resume();

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

    checkAllFields(activity, "", "", "", "", false);
}
 
Example 17
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 18
Source File: MainActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testClickSettings()
{
    ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create();
    Activity activity = (Activity)activityController.get();

    activityController.start();
    activityController.resume();
    activityController.visible();

    shadowOf(activity).clickMenuItem(R.id.action_settings);
    testNextStartedActivity(activity, "protect.budgetwatch/.SettingsActivity");
}
 
Example 19
Source File: BudgetViewActivityTest.java    From budget-watch with GNU General Public License v3.0 4 votes vote down vote up
private ActivityController setupActivity(final String budget, int value,
                                         boolean launchAsView, boolean launchAsUpdate)
{
    Intent intent = new Intent();
    final Bundle bundle = new Bundle();

    if(budget != null)
    {
        bundle.putString("id", budget);
    }

    if(launchAsView)
    {
        bundle.putBoolean("view", true);
    }

    if(launchAsUpdate)
    {
        bundle.putBoolean("update", true);
    }

    intent.putExtras(bundle);

    ActivityController activityController = Robolectric.buildActivity(BudgetViewActivity.class, intent).create();

    Activity activity = (Activity)activityController.get();
    DBHelper db = new DBHelper(activity);

    if(budget != null)
    {
        boolean result = db.insertBudget(budget, value);
        assertTrue(result);
    }
    db.close();

    activityController.start();
    activityController.visible();
    activityController.resume();

    return activityController;
}
 
Example 20
Source File: BudgetActivityTest.java    From budget-watch with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void addBudgetTotal()
{
    ActivityController activityController = Robolectric.buildActivity(BudgetActivity.class).create();

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

    final long nowMs = System.currentTimeMillis();

    int current = 0;
    int max = 0;

    checkTotalItem(mainActivity, current, max);

    DBHelper db = new DBHelper(mainActivity);

    final int budgetValue = 1234;
    final int expenseValue = 123;
    final int revenueValue = 12;

    for(int index = 0; index < 10; index++)
    {
        final String budgetName = "budget" + index;
        db.insertBudget("budget" + index, budgetValue);
        max += budgetValue;

        db.insertTransaction(DBHelper.TransactionDbIds.EXPENSE, "description", "account", budgetName, expenseValue, "note", nowMs, "receipt");
        db.insertTransaction(DBHelper.TransactionDbIds.REVENUE, "description", "account", budgetName, revenueValue, "note", nowMs, "receipt");

        current = current + expenseValue - revenueValue;
    }

    activityController.pause();
    activityController.resume();

    checkTotalItem(mainActivity, current, max);

    // Add a few blank budget transactions

    for(int index = 0; index < 10; index++)
    {
        db.insertTransaction(DBHelper.TransactionDbIds.EXPENSE, "description", "account", "", expenseValue, "note", nowMs, "receipt");
        db.insertTransaction(DBHelper.TransactionDbIds.REVENUE, "description", "account", "", revenueValue, "note", nowMs, "receipt");

        current = current + expenseValue - revenueValue;
    }

    activityController.pause();
    activityController.resume();

    checkTotalItem(mainActivity, current, max);

    db.close();
}