Java Code Examples for org.robolectric.Robolectric#buildActivity()

The following examples show how to use org.robolectric.Robolectric#buildActivity() . 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: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void onSaveInstanceState_savesDeviceData() throws NoSuchFieldException,
        IllegalAccessException {
    mActivityController.setup();
    setField(DropInActivity.class, mActivity, "mDeviceData", "device-data-string");

    Bundle bundle = new Bundle();
    mActivityController.saveInstanceState(bundle)
            .pause()
            .stop()
            .destroy();

    mActivityController = Robolectric.buildActivity(DropInUnitTestActivity.class);
    mActivity = (DropInUnitTestActivity) mActivityController.get();
    mActivityController.setup(bundle);

    assertEquals("device-data-string", getField(DropInActivity.class, mActivity, "mDeviceData"));
}
 
Example 2
Source File: FontPreferenceTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    controller = Robolectric.buildActivity(PreferencesActivity.class,
            new Intent()
                    .putExtra(PreferencesActivity.EXTRA_TITLE, R.string.display)
                    .putExtra(PreferencesActivity.EXTRA_PREFERENCES, R.xml.preferences_display));
    activity = controller.create().postCreate(null).start().resume().visible().get();
    RecyclerView list = activity.findViewById(android.R.id.list_container);
    list.setLayoutManager(new LinearLayoutManager(activity));
    RecyclerView.Adapter adapter = list.getAdapter();
    int position = ShadowSupportPreferenceManager
            .getPreferencePosition((PreferenceGroupAdapter) adapter, FontPreference.class);
    RecyclerView.ViewHolder holder = CustomShadows.customShadowOf(adapter).getViewHolder(position);
    preferenceView = holder.itemView;
}
 
Example 3
Source File: PopularActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(itemManager);
    controller = Robolectric.buildActivity(PopularActivity.class);
    activity = controller.create().start().resume().visible().get();
}
 
Example 4
Source File: StoriesActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testShowActivity() {
    ActivityController<ShowActivity> controller = Robolectric.buildActivity(ShowActivity.class);
    ShowActivity activity = controller.create().start().resume().get();
    assertEquals(activity.getString(R.string.title_activity_show), activity.getDefaultTitle());
    assertEquals(ItemManager.SHOW_FETCH_MODE, activity.getFetchMode());
    controller.pause().stop().destroy();
}
 
Example 5
Source File: SubmitActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeriveTitle() {
    controller.pause().stop().destroy();
    controller = Robolectric.buildActivity(SubmitActivity.class,
            new Intent().putExtra(Intent.EXTRA_TEXT, "http://example.com"));
    activity = controller
            .create().start().resume().visible().get();
    assertEquals("http://example.com", ShadowWebView.getLastGlobalLoadedUrl());
}
 
Example 6
Source File: UserActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithDataId() {
    Intent intent = new Intent();
    intent.setData(Uri.parse(BuildConfig.APPLICATION_ID + "://user/123"));
    controller = Robolectric.buildActivity(UserActivity.class, intent);
    activity = controller.create().get();
    assertThat(activity).isNotFinishing();
}
 
Example 7
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testComment() {
    TestItem item = new TestItem() {
        @NonNull
        @Override
        public String getType() {
            return COMMENT_TYPE;
        }

        @Override
        public String getId() {
            return "1";
        }

        @Override
        public String getUrl() {
            return String.format(HackerNewsClient.WEB_ITEM_PATH, "1");
        }

        @Override
        public String getText() {
            return "comment";
        }
    };
    Intent intent = new Intent();
    intent.putExtra(WebActivity.EXTRA_ITEM, item);
    controller = Robolectric.buildActivity(WebActivity.class, intent);
    controller.create().start().resume().visible();
    activity = controller.get();
    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = shadowOf(webView);
    shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank");
    assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("comment");
}
 
Example 8
Source File: BrowserSwitchFragmentTest.java    From browser-switch-android with MIT License 5 votes vote down vote up
@Before
public void beforeEach() {
    browserSwitchClient = mock(BrowserSwitchClient.class);

    controller = Robolectric.buildActivity(FragmentActivity.class);
    controller.setup();

    activity = controller.get();

    // attach sut fragment to activity
    sut = new TestBrowserSwitchFragment();

    FragmentManager fm = activity.getSupportFragmentManager();
    fm.beginTransaction().add(sut, "test-fragment").commit();
}
 
Example 9
Source File: SubmitActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeriveEmptyTitle() {
    controller.pause().stop().destroy();
    controller = Robolectric.buildActivity(SubmitActivity.class,
            new Intent().putExtra(Intent.EXTRA_TEXT, " : http://example.com"));
    activity = controller
            .create().start().resume().visible().get();
    assertThat((EditText) activity.findViewById(R.id.edittext_title)).isEmpty();
    assertThat((EditText) activity.findViewById(R.id.edittext_content)).hasTextString("http://example.com");
}
 
Example 10
Source File: OfflineWebActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testHomeButton() {
    controller = Robolectric.buildActivity(OfflineWebActivity.class, new Intent()
            .putExtra(OfflineWebActivity.EXTRA_URL, "http://example.com"));
    activity = controller
            .create()
            .start()
            .resume()
            .visible()
            .get();
    shadowOf(activity).clickMenuItem(android.R.id.home);
    assertThat(activity).isFinishing();
}
 
Example 11
Source File: IpcamActivityTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
private void createWithIntent(String uuidExtra) {
    Intent intent = new Intent(RuntimeEnvironment.application, IpcamActivity.class);
    intent.putExtra(RealmModel.FIELD_UUID, uuidExtra);

    controller = Robolectric.buildActivity(IpcamActivity.class, intent);

    activity = controller
        .create()
        .start()
        .resume()
        .visible()
        .get();

    ButterKnife.bind(this, activity);
}
 
Example 12
Source File: AuthorizationManagementActivityTest.java    From AppAuth-Android with Apache License 2.0 5 votes vote down vote up
private void instantiateActivity(Intent managementIntent) {
    mController = Robolectric.buildActivity(
            AuthorizationManagementActivity.class,
            managementIntent);

    mActivity = mController.get();
    mActivityShadow = shadowOf(mActivity);
}
 
Example 13
Source File: SearchActivityTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testSort() {
    Intent intent = new Intent();
    intent.putExtra(SearchManager.QUERY, "filter");
    controller = Robolectric.buildActivity(SearchActivity.class, intent);
    controller.create().postCreate(null).start().resume().visible();
    activity = controller.get();
    assertTrue(AlgoliaClient.sSortByTime);
    activity.onOptionsItemSelected(shadowOf(activity).getOptionsMenu()
            .findItem(R.id.menu_sort_recent)); // should not trigger search
    activity.onOptionsItemSelected(shadowOf(activity).getOptionsMenu()
            .findItem(R.id.menu_sort_popular)); // should trigger search
    assertFalse(AlgoliaClient.sSortByTime);
    verify(itemManager, times(2)).getStories(any(), eq(ItemManager.MODE_DEFAULT));
}
 
Example 14
Source File: ActivityUtilsTest.java    From android-utilset with Apache License 2.0 4 votes vote down vote up
private <T extends Activity> T createActivity(Class<T> activityClass) {
	ActivityController<T> controller = Robolectric.buildActivity(activityClass);
	controller.create();
	return controller.get();
}
 
Example 15
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Test
public void testStory() {
    TestWebItem item = new TestWebItem() {
        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getId() {
            return "1";
        }

        @Override
        public String getUrl() {
            return String.format(HackerNewsClient.WEB_ITEM_PATH, "1");
        }

        @Override
        public String getDisplayedTitle() {
            return "Ask HN";
        }
    };
    Intent intent = new Intent();
    intent.putExtra(WebActivity.EXTRA_ITEM, item);
    controller = Robolectric.buildActivity(WebActivity.class, intent);
    controller.create().start().resume().visible();
    activity = controller.get();
    verify(itemManager).getItem(eq("1"), eq(ItemManager.MODE_DEFAULT), listener.capture());
    listener.getValue().onResponse(new TestItem() {
        @Override
        public String getText() {
            return "text";
        }
    });
    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = shadowOf(webView);
    shadowWebView.getWebViewClient().onPageFinished(webView, "about:blank");
    assertThat(shadowWebView.getLastLoadDataWithBaseURL().data).contains("text");
}
 
Example 16
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Config(shadows = ShadowRecyclerView.class)
@Test
public void testScrollToTop() {
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {
        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getId() {
            return "1";
        }

        @Override
        public boolean isStoryType() {
            return true;
        }

        @Override
        public int getKidCount() {
            return 10;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }
    });
    controller = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();
    // see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    RecyclerView recyclerView = activity.findViewById(R.id.recycler_view);
    recyclerView.smoothScrollToPosition(1);
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(1);
    TabLayout tabLayout = activity.findViewById(R.id.tab_layout);
    assertThat(tabLayout.getTabCount()).isEqualTo(2);
    tabLayout.getTabAt(1).select();
    tabLayout.getTabAt(0).select();
    tabLayout.getTabAt(0).select();
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(0);
}
 
Example 17
Source File: SettingsActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    TestApplication.applicationGraph.inject(this);
    controller = Robolectric.buildActivity(SettingsActivity.class);
    activity = controller.create().postCreate(null).start().resume().visible().get();
}
 
Example 18
Source File: MvvmActivityTest.java    From android-mvvm with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
    activityController = Robolectric.buildActivity(TestMvvmActivity.class);
    activity = activityController.get();
}
 
Example 19
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
@Test
public void testShare() {
    TestApplication.addResolver(new Intent(Intent.ACTION_SEND));
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {
        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }

        @Override
        public boolean isStoryType() {
            return true;
        }

        @Override
        public String getId() {
            return "1";
        }
    });
    controller = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();

    // inflate menu, see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();

    // share article
    shadowOf(activity).clickMenuItem(R.id.menu_share);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu())
            .getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_article));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    Intent actual = shadowOf(activity).getNextStartedActivity();
    assertThat(actual)
            .hasAction(Intent.ACTION_SEND);

    // share item
    shadowOf(activity).clickMenuItem(R.id.menu_share);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu())
            .getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_comments));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    actual = shadowOf(activity).getNextStartedActivity();
    assertThat(actual)
            .hasAction(Intent.ACTION_SEND);
}
 
Example 20
Source File: CardFormTest.java    From android-card-form with MIT License 4 votes vote down vote up
@Before
public void setup() {
    mActivityController = Robolectric.buildActivity(TestActivity.class);
    mActivity = mActivityController.setup().get();
    mCardForm = mActivity.findViewById(android.R.id.custom);
}