@Test public void startActivity_withResult_finishesWithResult() { int givenResultCode = 0x2c001; Intent givenIntent = new HintRetrieveResult.Builder(givenResultCode).build().toResultDataIntent(); Intent intent = FinishWithResultActivity.createIntent( CONTEXT, ActivityResult.of(givenResultCode, givenIntent)); ShadowActivity activity = startActivity(intent); assertThat(activity.isFinishing()).isTrue(); HintRetrieveResult result = CredentialClient.getInstance(CONTEXT) .getHintRetrieveResult(activity.getResultIntent()); assertThat(result.getResultCode()).isEqualTo(givenResultCode); assertThat(activity.getResultCode()).isEqualTo(givenResultCode); }
@Test public void clickBackFinishes() throws IOException { File file = new File(Environment.getExternalStorageDirectory(), "test.jpg"); file.createNewFile(); ActivityController controller = startWithFile(file); Activity activity = (Activity)controller.get(); ShadowActivity shadowActivity = shadowOf(activity); assertTrue(shadowActivity.isFinishing() == false); shadowActivity.clickMenuItem(android.R.id.home); assertTrue(shadowActivity.isFinishing()); // Check that can finish out the lifecycle controller.pause(); controller.stop(); controller.destroy(); // The done menu item was not clicked, so the result should be that // the activity was canceled assertEquals(Activity.RESULT_CANCELED, shadowActivity.getResultCode()); assertNull(shadowActivity.getResultIntent()); }
@Test public void clickDeleteFinishes() throws IOException { File file = new File(Environment.getExternalStorageDirectory(), "test.jpg"); file.createNewFile(); ActivityController controller = startWithFile(file); Activity activity = (Activity)controller.get(); ShadowActivity shadowActivity = shadowOf(activity); assertTrue(shadowActivity.isFinishing() == false); boolean result = shadowActivity.clickMenuItem(R.id.action_delete); assertTrue(result); // Note, cannot finish the test because the v7 AlertDialog // currently does not have shadow support. }
private void checkOpenActivity(ActivityController controller, int viewId, HashMap<String, Integer> items) { Activity activity = (Activity)controller.get(); activity.findViewById(viewId).performClick(); assertTrue(activity.isFinishing() == false); ShadowActivity.IntentForResult next = shadowOf(activity).getNextStartedActivityForResult(); ComponentName componentName = next.intent.getComponent(); String name = componentName.flattenToShortString(); assertEquals("protect.rentalcalc/.ItemizeActivity", name); Bundle extras = next.intent.getExtras(); assertNotNull(extras); assertTrue(extras.containsKey("title")); assertTrue(extras.getInt("title") > 0); assertTrue(extras.containsKey("description")); assertTrue(extras.getInt("description") > 0); assertTrue(extras.containsKey("items")); assertEquals(items, extras.getSerializable("items")); // As the next activity is started, this activity is paused controller.pause(); }
@Test public void clickBackFinishes() { ActivityController controller = startWithMap(new HashMap<String, Integer>()); Activity activity = (Activity)controller.get(); ShadowActivity shadowActivity = shadowOf(activity); assertTrue(shadowActivity.isFinishing() == false); shadowActivity.clickMenuItem(android.R.id.home); assertTrue(shadowActivity.isFinishing()); // Check that can finish out the lifecycle controller.pause(); controller.stop(); controller.destroy(); // The done menu item was not clicked, so the result should be that // the activity was canceled assertEquals(Activity.RESULT_CANCELED, shadowActivity.getResultCode()); assertNull(shadowActivity.getResultIntent()); }
private void checkReturnedMap(Activity activity, Map<String, Integer> expected) { ShadowActivity shadowActivity = shadowOf(activity); assertEquals(Activity.RESULT_OK, shadowActivity.getResultCode()); Intent intent = shadowActivity.getResultIntent(); assertNotNull(intent); Bundle bundle = intent.getExtras(); assertNotNull(bundle); assertTrue(bundle.containsKey("items")); HashMap<String, Integer> map = (HashMap<String, Integer>)bundle.getSerializable("items"); assertNotNull(map); assertEquals(expected.size(), map.size()); for(Map.Entry<String, Integer> item : expected.entrySet()) { assertTrue("Did not contain: " + item.getKey(), map.containsKey(item.getKey())); assertEquals("Value did not match: " + item.getValue(), item.getValue(), map.get(item.getKey())); } }
@Test public void doneNoItems() { HashMap<String, Integer> emptyMap = new HashMap<>(); ActivityController controller = startWithMap(emptyMap); Activity activity = (Activity)controller.get(); ShadowActivity shadowActivity = shadowOf(activity); assertTrue(shadowActivity.isFinishing() == false); shadowActivity.clickMenuItem(R.id.action_done); assertTrue(shadowActivity.isFinishing()); // Check that can finish out the lifecycle controller.pause(); controller.stop(); controller.destroy(); checkReturnedMap(activity, emptyMap); }
private void addItem(Activity activity, String name, Integer value) { ShadowActivity shadowActivity = shadowOf(activity); ListView list = (ListView)activity.findViewById(R.id.list); assertNotNull(list); int initialCount = list.getCount(); shadowActivity.clickMenuItem(R.id.action_add); ItemizationAdapter adapter = (ItemizationAdapter)list.getAdapter(); Itemization listItem = adapter.getItem(initialCount); assertNotNull(listItem); listItem.name = name; listItem.value = value; assertEquals(initialCount+1, list.getCount()); }
@Test public void should_start_url_intent() { // Given String url = "geo:22.5430883,114.1043205"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isTrue(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void should_return_false_when_nothing_on_the_device_can_open_the_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), (ResolveInfo) null); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isFalse(); assertThat(startedIntent).isNull(); }
@Test public void should_start_external_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When Intents.startExternalUrl(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void finish_finishesWithPaymentMethodNonceAndDeviceDataInDropInResult() throws JSONException { CardNonce cardNonce = CardNonce.fromJson( stringFromFixture("responses/visa_credit_card_response.json")); setup(new DropInRequest() .tokenizationKey(TOKENIZATION_KEY) .getIntent(RuntimeEnvironment.application)); mActivity.finish(cardNonce, "device_data"); ShadowActivity shadowActivity = shadowOf(mActivity); assertTrue(mActivity.isFinishing()); assertEquals(Activity.RESULT_OK, shadowActivity.getResultCode()); DropInResult result = shadowActivity.getResultIntent() .getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT); assertNotNull(result); assertEquals(cardNonce.getNonce(), result.getPaymentMethodNonce().getNonce()); assertEquals("device_data", result.getDeviceData()); }
@Test public void finish_finishesWithException() { Exception exception = new Exception("Error message"); setup(new DropInRequest() .tokenizationKey(TOKENIZATION_KEY) .getIntent(RuntimeEnvironment.application)); mActivity.finish(exception); ShadowActivity shadowActivity = shadowOf(mActivity); assertTrue(mActivity.isFinishing()); assertEquals(Activity.RESULT_FIRST_USER, shadowActivity.getResultCode()); Exception error = (Exception) shadowActivity.getResultIntent() .getSerializableExtra(DropInActivity.EXTRA_ERROR); assertNotNull(error); assertEquals("Error message", error.getMessage()); }
@Test public void testClickAdd() { ActivityController activityController = Robolectric.buildActivity(BudgetActivity.class).create(); Activity activity = (Activity)activityController.get(); activityController.start(); activityController.resume(); activityController.visible(); shadowOf(activity).clickMenuItem(R.id.action_add); ShadowActivity shadowActivity = shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); ComponentName name = startedIntent.getComponent(); assertNotNull(name); assertEquals("protect.budgetwatch/.BudgetViewActivity", name.flattenToShortString()); Bundle bundle = startedIntent.getExtras(); assertNull(bundle); }
@Test public void startAsViewClickEdit() throws Exception { ActivityController activityController = setupActivity("budget", "", true, false); Activity activity = (Activity)activityController.get(); shadowOf(activity).clickMenuItem(R.id.action_edit); 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); assertEquals(DBHelper.TransactionDbIds.EXPENSE, bundle.getInt("type", -1)); assertEquals(1, bundle.getInt("id", -1)); assertEquals(true, bundle.getBoolean("update", false)); assertEquals(false, bundle.getBoolean("view", false)); }
@Test public void should_start_baggers_list_activity_when_city_is_selected() { // Given City city = new City(); city.name = "Paris"; city.lat = 42d; city.lng = 1d; // When activity.onCitySelected(city); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent intent = shadowActivity.getNextStartedActivity(); assertThat(intent.getComponent().getClassName()).isEqualTo(BaggersListActivity.class.getName()); assertThat(((City) intent.getParcelableExtra("mCity")).name).isEqualTo("Paris"); }
@Test public void testContextMenu1() { adapter.updateDataSet(contacts); RecyclerView.ViewHolder viewHolder = adapter.onCreateViewHolder(recyclerView, 0); adapter.onBindViewHolder(viewHolder, 0); try { viewHolder.itemView.performLongClick(); // Danke Robolectric. NullPointer weil irgendwas mit Menu buggy.. } catch (NullPointerException e) { //Nichts tun. } RoboMenuItem item = new RoboMenuItem(R.id.context_tab_show_in_contacts); item.setGroupId(R.id.context_tab_contact_group); fragment.onContextItemSelected(item); ShadowActivity a = Shadows.shadowOf(activity); Intent i = a.getNextStartedActivityForResult().intent; assertNotNull(i); }
@Test public void givenTheLoaderIsFinishedWhenTheShareButtonIsPressedThenTheLoadedListOfRemindersIsShared() { Reminder reminder = new Reminder(0, "test"); Reminder reminder2 = new Reminder(0, "test2"); Cursor mockCursor = mock(Cursor.class); when(mockCursor.moveToNext()).thenReturn(true).thenReturn(true).thenReturn(false); when(mockCursor.getLong(0)).thenReturn(reminder.getId()).thenReturn(reminder2.getId()); when(mockCursor.getString(1)).thenReturn(reminder.getText()).thenReturn(reminder2.getText()); ShadowCursorWrapper wrapper = new ShadowCursorWrapper(); wrapper.__constructor__(mockCursor); CursorLoader cursorLoader = (CursorLoader)activity.onCreateLoader(0, null); activity.onLoadFinished(cursorLoader, wrapper); ShadowActivity shadowActivity = Shadows.shadowOf(activity); shadowActivity.clickMenuItem(R.id.action_share); ShadowIntent shadowIntent = Shadows.shadowOf(shadowActivity.peekNextStartedActivity()); assertEquals(Intent.ACTION_CHOOSER, shadowIntent.getAction()); ShadowIntent shareIntent = Shadows.shadowOf((Intent) shadowIntent.getParcelableExtra("android.intent.extra.INTENT")); assertEquals("text/plain", shareIntent.getType()); assertEquals("test\ntest2\n", shareIntent.getExtras().getString(Intent.EXTRA_TEXT)); }
@Test public void givenAStoreIsBoundWhenTheStoreViewHolderIsClickedThenTheMapApplicationIsLaunched() { RecyclerView recyclerView = getRecyclerView(); Context context = recyclerView.getContext(); View view = LayoutInflater.from(context).inflate(R.layout.store_viewholder, recyclerView, false); double latitude = 0.0; double longitude = 1.0; GroceryStore store = new GroceryStore(ARBITRARY_STORE_NAME, 2414.02, latitude, longitude); GroceryStoreListViewHolder viewHolder = new GroceryStoreListViewHolder(view); viewHolder.bind(store); viewHolder.onClick(view); ShadowActivity shadowActivity = Shadows.shadowOf(activity); ShadowIntent shadowIntent = Shadows.shadowOf(shadowActivity.peekNextStartedActivity()); assertTrue(shadowIntent.getData().toString().contains("geo:" + latitude + "," + longitude)); }
@Test public void givenStoresAreLoadedWhenAStoreIsClickedThenTheMapApplicationIsLaunched() { ShadowCursorWrapper wrapper = createCursorWithDefaultReminder(); GroceryStoreManagerInterface groceryStoreManagerMock = getTestReminderModule().getGroceryStoreManager(); when(groceryStoreManagerMock.getCurrentLocation()).thenReturn(new Location(LocationManager.PASSIVE_PROVIDER)); CursorLoader cursorLoader = (CursorLoader)activity.onCreateLoader(0, null); activity.onLoadFinished(cursorLoader, wrapper); GroceryStoreListFragment groceryStoreListFragment = getGroceryStoreListFragment(); assertNotNull(groceryStoreListFragment); RecyclerView listView = getRecyclerView(groceryStoreListFragment, R.id.stores_recycler_view); listView.findViewHolderForAdapterPosition(0).itemView.performClick(); ShadowActivity shadowActivity = Shadows.shadowOf(activity); ShadowIntent shadowIntent = Shadows.shadowOf(shadowActivity.peekNextStartedActivity()); assertEquals("geo:0.0,1.0?q=0.0,1.0(test)", shadowIntent.getData().toString()); }