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

The following examples show how to use org.robolectric.Robolectric#setupActivity() . 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: MainActivityMockPresenterTest.java    From DaggerMock with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCreate() {
    final MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) {
            activity.showText("Hello mocked world");
            return null;
        }
    }).when(presenter).loadData();


    activity.findViewById(R.id.reload).performClick();

    TextView textView = (TextView) activity.findViewById(R.id.text);
    assertThat(textView.getText()).isEqualTo("Hello mocked world");
}
 
Example 2
Source File: NavigationViewClickListenerTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
private void showEditDialog() {
    List<EnvironmentModel> environments = Lists.newArrayList(
        newEnvironmentModel(801, "A-environment"),
        newEnvironmentModel(802, "B-environment"),
        environmentSelected,
        newEnvironmentModel(804, "D-environment"),
        newEnvironmentModel(805, "E-environment"));

    when(environmentService.findAll()).thenReturn(Observable.just(environments));
    when(environmentService.findById(MENU_ENVIRONMENT_ID)).thenReturn(Observable.just(environmentSelected));

    activity = Robolectric.setupActivity(MainActivity.class);
    ButterKnife.bind(this, activity);

    MenuItem itemSelected = activity.navigationView.getMenu().findItem(MENU_ENVIRONMENT_ID);
    View viewEdit = itemSelected.getActionView().findViewById(R.id.imageViewDrawerMenuEnvironmentEdit);
    viewEdit.performClick();
}
 
Example 3
Source File: FragmentsSelectTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void bleEnabled_shouldReturnITagsFragment() {
    mockBluetoothAdapter =  Mockito.mock(BluetoothAdapter.class);
    when(mockBluetoothAdapter.isEnabled()).thenReturn(true);
    when(mockBluetoothManager.getAdapter()).thenReturn(mockBluetoothAdapter);
    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
    final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content);
    Assert.assertTrue(fragment instanceof ITagsFragment);
}
 
Example 4
Source File: BudgetActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void clickAddLaunchesBudgetViewActivity()
{
    final Activity activity = Robolectric.setupActivity(BudgetActivity.class);

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

    Intent intent = shadowOf(activity).peekNextStartedActivityForResult().intent;

    assertEquals(new ComponentName(activity, BudgetViewActivity.class), intent.getComponent());
    assertNull(intent.getExtras());
}
 
Example 5
Source File: SDK21IntTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setupActivity() {
    Application application = ApplicationProvider.getApplicationContext();
    ShadowApplication shadowApplication = shadowOf(application);
    shadowApplication.grantPermissions(Manifest.permission.ACCESS_FINE_LOCATION);
    Intent intent = new Intent(application, ITagsService.class);
    ITagsService iTagsService = new ITagsService();
    shadowApplication.setComponentNameAndServiceForBindServiceForIntent(
            intent,
            new ComponentName(ITagApplication.class.getPackage().getName(),ITagsService.class.getName()),
            iTagsService.onBind(null)
    );
    mainActivity = Robolectric.setupActivity(MainActivity.class);
}
 
Example 6
Source File: RepoListFragmentTest.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    component.inject(this);

    repoListFragment = spy(new RepoListFragment());
    activity = Robolectric.setupActivity(MainActivity.class);
    repoListFragment.onAttach(activity);
    doAnswer(invocation -> TestConst.TEST_OWNER)
            .when(repoListFragment)
            .getUserName();

}
 
Example 7
Source File: BehaviorTest.java    From simple-view-behavior with MIT License 5 votes vote down vote up
@Before
public void setup() {
    Activity activity = Robolectric.setupActivity(Activity.class);
    activity.setTheme(R.style.Theme_AppCompat);
    coordinatorLayout = new CoordinatorLayout(activity);
    activity.setContentView(coordinatorLayout);
    firstView = new View(activity);
    secondView = new TestPercentageChildView(activity);

    CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200);
    coordinatorLayout.addView(firstView, params);
    coordinatorLayout.addView(secondView, params);
}
 
Example 8
Source File: MainActivityTest.java    From robolectric-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainActivity() {
    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
    mainActivity.findViewById(R.id.textView1).performClick();
    
    Intent expectedIntent = new Intent(mainActivity, SecondActivity.class);
    ShadowActivity shadowActivity = Shadows.shadowOf(mainActivity);
    Intent actualIntent = shadowActivity.getNextStartedActivity();
    Assert.assertEquals(expectedIntent, actualIntent);
}
 
Example 9
Source File: MyActivityTest.java    From gradle-android-aspectj-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void greet() throws Exception {
    MyActivity underTest = Robolectric.setupActivity(MyActivity.class);

    TextView greetingView = (TextView) underTest.findViewById(R.id.greeting);
    assertThat(greetingView).hasText("Hello, aspect!");
}
 
Example 10
Source File: CircularRevealHelperTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  activity = Robolectric.setupActivity(Activity.class);

  delegate = new TestDelegate(activity);
  delegate.measure(
      MeasureSpec.makeMeasureSpec(DELEGATE_WIDTH, MeasureSpec.EXACTLY),
      MeasureSpec.makeMeasureSpec(DELEGATE_HEIGHT, MeasureSpec.EXACTLY));
  delegate.layout(0, 0, DELEGATE_WIDTH, DELEGATE_HEIGHT);

  helper = new CircularRevealHelper(delegate);
  canvas = spy(new Canvas());
}
 
Example 11
Source File: ImportExportActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testImportApplicationOption()
{
    for(boolean isInstalled : new Boolean[]{false, true})
    {
        int visibility = isInstalled ? View.VISIBLE : View.GONE;

        if(isInstalled)
        {
            registerIntentHandler(Intent.ACTION_GET_CONTENT);
        }

        Activity activity = Robolectric.setupActivity(ImportExportActivity.class);

        checkVisibility(activity, visibility, R.id.dividerImportApplication,
                R.id.importOptionApplicationTitle, R.id.importOptionApplicationExplanation,
                null, null, R.id.importOptionApplicationButton);

        // Should always be gone, as its provider is never installed
        checkVisibility(activity, View.GONE, R.id.dividerImportFilesystem,
                R.id.importOptionFilesystemTitle, R.id.importOptionFilesystemExplanation,
                null, null, R.id.importOptionFilesystemButton);

        // Import from file system should always be present

        checkVisibility(activity, View.VISIBLE, R.id.dividerImportFixed,
                R.id.importOptionFixedTitle, R.id.importOptionFixedExplanation,
                R.id.importOptionFixedFileFormatSpinnerLabel, R.id.importFileFormatSpinner,
                R.id.importOptionFixedButton);
    }
}
 
Example 12
Source File: MainActivityTest.java    From gift-card-guard with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void initiallyNoGiftCards() throws Exception
{
    Activity activity = Robolectric.setupActivity(MainActivity.class);
    assertTrue(activity != null);

    TextView helpText = (TextView)activity.findViewById(R.id.helpText);
    assertEquals(View.VISIBLE, helpText.getVisibility());

    ListView list = (ListView)activity.findViewById(R.id.list);
    assertEquals(View.GONE, list.getVisibility());
}
 
Example 13
Source File: SettingsActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void clickBackFinishes()
{
    final Activity activity = Robolectric.setupActivity(SettingsActivity.class);

    shadowOf(activity).clickMenuItem(android.R.id.home);
    assertTrue(shadowOf(activity).isFinishing());
}
 
Example 14
Source File: BudgetActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void onCreateShouldInflateMenu() throws Exception
{
    final Activity activity = Robolectric.setupActivity(BudgetActivity.class);

    final Menu menu = shadowOf(activity).getOptionsMenu();
    assertTrue(menu != null);

    // The settings and add button should be present
    assertEquals(menu.size(), 2);

    assertEquals("Add", menu.findItem(R.id.action_add).getTitle().toString());
    assertEquals("Select Dates", menu.findItem(R.id.action_calendar).getTitle().toString());
}
 
Example 15
Source File: HomeActivityUnitTest.java    From android-clean-code with MIT License 5 votes vote down vote up
@Test
public void HomeActivity_ShouldNOT_be_Null(){
    //Given
    HomeActivity activity = Robolectric.setupActivity(HomeActivity.class);
    //When

    // Then
    Assert.assertNotNull(activity);
}
 
Example 16
Source File: QuickMenuTest.java    From QuickMenu with MIT License 4 votes vote down vote up
@Before
public void init() {
    activity = Robolectric.setupActivity(QuickSettingsActivity.class);
}
 
Example 17
Source File: MainActivityTest.java    From openwebnet-android with MIT License 4 votes vote down vote up
@Test
public void shouldInitNavigationDrawerMenu() {
    List<EnvironmentModel> environments = Lists.newArrayList(
        newEnvironmentModel(801, "A-environment"),
        newEnvironmentModel(802, "B-environment"),
        newEnvironmentModel(803, "C-environment"),
        newEnvironmentModel(804, "D-environment"),
        newEnvironmentModel(805, "E-environment"));

    when(environmentService.findAll()).thenReturn(Observable.just(environments));

    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
    ButterKnife.bind(this, mainActivity);

    verify(commonService).initApplication(mainActivity);
    verify(environmentService).findAll();

    Menu menu = navigationView.getMenu();
    assertEquals("invalid menu title", "Favourites", menu.getItem(0).getTitle());
    assertEquals("invalid menu order", 10, menu.getItem(0).getOrder());

    // ordered by name (from repository)
    assertEquals("invalid menu title", "A-environment", menu.getItem(1).getTitle());
    assertEquals("invalid menu order", 100, menu.getItem(1).getOrder());
    assertEquals("invalid menu title", "B-environment", menu.getItem(2).getTitle());
    assertEquals("invalid menu order", 101, menu.getItem(2).getOrder());
    assertEquals("invalid menu title", "C-environment", menu.getItem(3).getTitle());
    assertEquals("invalid menu order", 102, menu.getItem(3).getOrder());
    assertEquals("invalid menu title", "D-environment", menu.getItem(4).getTitle());
    assertEquals("invalid menu order", 103, menu.getItem(4).getOrder());
    assertEquals("invalid menu title", "E-environment", menu.getItem(5).getTitle());
    assertEquals("invalid menu order", 104, menu.getItem(5).getOrder());

    assertEquals("invalid menu title", "Add environment", menu.getItem(6).getTitle());
    assertEquals("invalid menu order", 900, menu.getItem(6).getOrder());
    assertEquals("invalid menu title", "Profiles", menu.getItem(7).getTitle());
    assertEquals("invalid menu order", 900, menu.getItem(7).getOrder());
    assertEquals("invalid menu title", "Settings", menu.getItem(8).getTitle());
    assertEquals("invalid menu order", 900, menu.getItem(8).getOrder());
    assertEquals("invalid menu title", "Donation", menu.getItem(9).getTitle());
    assertEquals("invalid menu order", 900, menu.getItem(9).getOrder());
    assertEquals("invalid menu title", "Info", menu.getItem(10).getTitle());
    assertEquals("invalid menu order", 900, menu.getItem(10).getOrder());
}
 
Example 18
Source File: MainActivityTest.java    From video-transcoder with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void selectMp4SwitchGifInvalidVideoBitrateAttemptEncode() throws Exception
{
    Activity activity = Robolectric.setupActivity(MainActivity.class);

    openFilePickerFromLoadSelectFile(activity, FAKE_MP4_MEDIA_INFO);

    checkSelectedSettings(activity, FAKE_MP4_MEDIA_INFO);

    Spinner containerSpinner = activity.findViewById(R.id.containerSpinner);
    setSpinnerSelection(containerSpinner, MediaContainer.GIF.ffmpegName);

    EditText videoBitrate = activity.findViewById(R.id.videoBitrateValue);
    videoBitrate.setText("Invalid");

    // GIF should ignore the video bitrate, as it is not needed

    activity.findViewById(R.id.encode).performClick();

    // Ensure that encoding was attempted
    assertEquals(mockFFmpegService.encodeAttempts, 1);
}
 
Example 19
Source File: ViewTestBase.java    From line-chart-view with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    activity = Robolectric.setupActivity(Activity.class);
}
 
Example 20
Source File: TestActivityTest.java    From Awesome-WanAndroid with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    ShadowLog.stream = System.out;
    mTestActivity = Robolectric.setupActivity(TestActivity.class);
}