@Test public void testListMenu() { Bundle args = new Bundle(); args.putString(ListFragment.EXTRA_ITEM_MANAGER, HackerNewsClient.class.getName()); args.putString(ListFragment.EXTRA_FILTER, ItemManager.TOP_FETCH_MODE); activity.getSupportFragmentManager() .beginTransaction() .add(android.R.id.list, Fragment.instantiate(activity, ListFragment.class.getName(), args), ListFragment.class.getName()) .commit(); activity.getSupportFragmentManager().findFragmentByTag(ListFragment.class.getName()) .onOptionsItemSelected(new RoboMenuItem(R.id.menu_list)); assertThat(activity.getSupportFragmentManager()) .hasFragmentWithTag(PopupSettingsFragment.class.getName()); }
@Test public void testFontSizeMenu() { verify(readabilityClient).parse(eq("1"), eq("http://example.com/article.html"), callback.capture()); callback.getValue().onResponse("<div>content</div>"); fragment.onOptionsItemSelected(new RoboMenuItem(R.id.menu_font_options)); assertThat(fragment.getFragmentManager()) .hasFragmentWithTag(PopupSettingsFragment.class.getName()); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putString(activity.getString(R.string.pref_readability_text_size), "3") .apply(); WebView webView = (WebView) activity.findViewById(R.id.web_view); shadowOf(webView).getWebViewClient().onPageFinished(webView, "about:blank"); assertThat(shadowOf(webView).getLastLoadDataWithBaseURL().data).contains("20"); assertEquals(R.style.AppTextSize_XLarge, Preferences.Theme.resolvePreferredReadabilityTextSize(activity)); controller.pause().stop().destroy(); }
@Test public void testDelete() { RecyclerView.ViewHolder holder = shadowAdapter.getViewHolder(0); holder.itemView.performLongClick(); ActionMode actionMode = mock(ActionMode.class); activity.actionModeCallback.onActionItemClicked(actionMode, new RoboMenuItem(R.id.menu_clear)); AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertEquals(2, adapter.getItemCount()); activity.actionModeCallback.onActionItemClicked(actionMode, new RoboMenuItem(R.id.menu_clear)); dialog = ShadowAlertDialog.getLatestAlertDialog(); dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); verify(favoriteManager).remove(any(Context.class), selection.capture()); assertThat(selection.getValue()).contains("1"); verify(actionMode).finish(); when(favoriteManager.getSize()).thenReturn(1); observerCaptor.getValue().onChanged(); assertEquals(1, adapter.getItemCount()); }
@SuppressLint("NewApi") @Test public void testWebControls() { ShadowLocalBroadcastManager.getInstance(activity) .sendBroadcast(new Intent(WebFragment.ACTION_FULLSCREEN) .putExtra(WebFragment.EXTRA_FULLSCREEN, true)); ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor .extract(activity.findViewById(R.id.web_view)); activity.findViewById(R.id.button_more).performClick(); shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_zoom_in)); assertThat(shadowWebView.getZoomDegree()).isEqualTo(1); activity.findViewById(R.id.button_more).performClick(); shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_zoom_out)); assertThat(shadowWebView.getZoomDegree()).isEqualTo(0); activity.findViewById(R.id.button_forward).performClick(); assertThat(shadowWebView.getPageIndex()).isEqualTo(1); activity.findViewById(R.id.button_back).performClick(); assertThat(shadowWebView.getPageIndex()).isEqualTo(0); }
@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 (expected = NullPointerException.class) public void testContextMenu2() { 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. } Contact contact = contacts.get(0); RoboMenuItem item = new RoboMenuItem(R.id.context_tab_contact_delete); item.setGroupId(R.id.context_tab_contact_group); fragment.onContextItemSelected(item); contacts = contactHelper.getContacts(); assertTrue(contacts.contains(contact)); }
@Test (expected = NullPointerException.class) public void testContextMenu3() { 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. } Contact contact = contacts.get(0); RoboMenuItem item = new RoboMenuItem(R.id.context_tab_contact_delete); item.setGroupId(R.id.context_tab_contact_group); contacts = contactHelper.getContacts(); List<Chat> chatList = chatHelper.getChatsByContactID(contact.getId()); for(Chat chat : chatList) { chatHelper.delete(chat); } fragment.onContextItemSelected(item); contacts = contactHelper.getContacts(); assertFalse(contacts.contains(contact)); }
@Test public void testContextMenu() { adapter.updateDataSet(files); long fileToDelete = files.get(0).getId(); FileTabRecyclerItemViewHolder viewHolder = (FileTabRecyclerItemViewHolder) 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_file_delete); item.setGroupId(R.id.context_tab_file_group); fragment.onContextItemSelected(item); // Wirft NullPointerException weil Robolectric das Fragment nicht richtig mocked this.getView ist Null.. assertNull(helper.getFileByID(fileToDelete)); }
@Test public void testlContextDelete() { adapter.updateDataSet(chats); String chatID = chats.get(0).getNetworkChatID(); 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_chat_delete); item.setGroupId(R.id.context_tab_chat_group); fragment.onContextItemSelected(item); assertNull(helper.getChat(chatID)); }
@Test public void testDisplayMenu() { WebItem webItem = mock(WebItem.class); when(webItem.getId()).thenReturn("1"); Bundle args = new Bundle(); args.putParcelable(ItemFragment.EXTRA_ITEM, webItem); Fragment fragment = Fragment.instantiate(RuntimeEnvironment.application, ItemFragment.class.getName(), args); makeVisible(fragment); fragment.onOptionsItemSelected(new RoboMenuItem(R.id.menu_comments)); assertThat(fragment.getFragmentManager()) .hasFragmentWithTag(PopupSettingsFragment.class.getName()); }
@Test public void testWebToggle() { fragment.onOptionsItemSelected(new RoboMenuItem(R.id.menu_readability)); WebView webView = (WebView) activity.findViewById(R.id.web_view); shadowOf(webView).getWebViewClient().onPageFinished(webView, "about:blank"); assertThat(shadowOf(webView).getLastLoadedUrl()).isEqualTo("http://example.com/article.html"); }
@SuppressLint("NewApi") @Test public void testFullscreenMenu() { verify(readabilityClient).parse(eq("1"), eq("http://example.com/article.html"), callback.capture()); callback.getValue().onResponse("<div>content</div>"); ShadowLocalBroadcastManager.getInstance(activity) .sendBroadcast(new Intent(WebFragment.ACTION_FULLSCREEN) .putExtra(WebFragment.EXTRA_FULLSCREEN, true)); activity.findViewById(R.id.button_more).performClick(); shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_font_options)); assertThat(fragment.getFragmentManager()) .hasFragmentWithTag(PopupSettingsFragment.class.getName()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVote() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), voteCallback.capture()); voteCallback.getValue().onDone(true); assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemPromptToLogin() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), voteCallback.capture()); voteCallback.getValue().onDone(false); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemFailed() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), voteCallback.capture()); voteCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testReply() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_comment)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, ComposeActivity.class) .hasExtra(ComposeActivity.EXTRA_PARENT_ID, "1"); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testShare() { TestApplication.addResolver(new Intent(Intent.ACTION_SEND)); viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_share)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasAction(Intent.ACTION_SEND); }
@Test public void testRefresh() { RecyclerView.ViewHolder holder = shadowAdapter.getViewHolder(0); holder.itemView.performLongClick(); ActionMode actionMode = mock(ActionMode.class); activity.actionModeCallback.onActionItemClicked(actionMode, new RoboMenuItem(R.id.menu_refresh)); verify(syncScheduler).scheduleSync(any(), any()); verify(actionMode).finish(); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItem() { shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture()); userServicesCallback.getValue().onDone(true); assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemPromptToLogin() { shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture()); userServicesCallback.getValue().onDone(false); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemFailed() { shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture()); userServicesCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testReply() { shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_comment)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, ComposeActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testShare() { TestApplication.addResolver(new Intent(Intent.ACTION_SEND)); shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_share)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasAction(Intent.ACTION_SEND); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testSaveItem() { ShadowContentObserver observer = shadowOf(shadowOf(activity .getContentResolver()) .getContentObservers(MaterialisticProvider.URI_FAVORITE) .iterator() .next()); verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); assertThat(popupMenu.getMenu().findItem(R.id.menu_contextual_save).isVisible()).isFalse(); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_save)); verify(favoriteManager).add(any(Context.class), eq(item)); observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE .buildUpon() .appendPath("add") .appendPath("1") .build()); assertTrue(item.isFavorite()); View snackbarView = ShadowSnackbar.getLatestView(); assertThat((TextView) snackbarView.findViewById(R.id.snackbar_text)) .isNotNull() .containsText(R.string.toast_saved); snackbarView.findViewById(R.id.snackbar_action).performClick(); verify(favoriteManager).remove(any(Context.class), eq("1")); observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE .buildUpon() .appendPath("remove") .appendPath("1") .build()); assertFalse(item.isFavorite()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testViewUser() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_profile)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, UserActivity.class) .hasExtra(UserActivity.EXTRA_USERNAME, "author"); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItem() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); assertThat(popupMenu.getMenu().findItem(R.id.menu_contextual_vote).isVisible()).isFalse(); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), eq(item.getId()), voteCallback.capture()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemPromptToLogin() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), eq(item.getId()), voteCallback.capture()); voteCallback.getValue().onDone(false); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemFailed() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), eq(item.getId()), voteCallback.capture()); voteCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testReply() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_comment)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, ComposeActivity.class) .hasExtra(ComposeActivity.EXTRA_PARENT_ID, "1"); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testRefresh() { verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); reset(itemManager); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_refresh)); verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), any()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testShare() { TestApplication.addResolver(new Intent(Intent.ACTION_SEND)); verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture()); itemListener.getValue().onResponse(item); adapter.getViewHolder(0).itemView.performLongClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_share)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasAction(Intent.ACTION_SEND); }
@Test public void should_open_filter_drawer_when_clicking_on_menu_icon() { // Given MenuItem item = new RoboMenuItem(R.id.action_filter_baggers); // When activity.onOptionsItemSelected(item); // Then verify(drawer).openDrawer(GravityCompat.END); }
@Test public void should_close_tags_drawer_when_clicking_on_menu_icon_and_drawer_is_already_opened() { // Given MenuItem item = new RoboMenuItem(R.id.action_filter_baggers); when(drawer.isDrawerOpen(GravityCompat.END)).thenReturn(true); // When activity.onOptionsItemSelected(item); // Then verify(drawer).closeDrawer(GravityCompat.END); }
@Test public void testOnCreateOption() { fragment.onCreateOptionsMenu(menu, inflater); MenuItem item = new RoboMenuItem(R.id.action_add); fragment.onOptionsItemSelected(item); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent i = shadowActivity.getNextStartedActivity(); assertEquals(i.getComponent().getClassName(), CreateContactActivity.class.getName()); }
@Test public void testContextMenu4() { 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. } Contact contact = contacts.get(0); RoboMenuItem item = new RoboMenuItem(R.id.context_tab_contact_link_to_contact); item.setGroupId(R.id.context_tab_contact_group); fragment.onContextItemSelected(item); android.support.v7.app.AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog(); ListView listView = dialog.getListView(); Shadows.shadowOf(listView).performItemClick(0); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent i = shadowActivity.getNextStartedActivityForResult().intent; assertNotNull(i); fragment.onContextItemSelected(item); dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog(); listView = dialog.getListView(); Shadows.shadowOf(listView).performItemClick(1); i = shadowActivity.getNextStartedActivityForResult().intent; assertNotNull(i); }
@Test public void testContextMenu2() { adapter.updateDataSet(files); 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_file_rename); item.setGroupId(R.id.context_tab_file_group); fragment.onContextItemSelected(item); android.support.v7.app.AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog(); Button b = dialog.getButton(Dialog.BUTTON_POSITIVE); EditText editText = (EditText) dialog.findViewById(R.id.alter_dialog_edit); editText.setText("neuerName"); b.performClick(); files = helper.getFiles(); String newName = files.get(0).getASName(); assertEquals("neuerName", newName); fragment.onContextItemSelected(item); dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog(); b = dialog.getButton(Dialog.BUTTON_NEGATIVE); b.performClick(); assertFalse(dialog.isShowing()); }
@Test public void testOptionItem() { RoboMenuItem item = new RoboMenuItem(android.R.id.home); ShadowActivity shadowActivity = Shadows.shadowOf(activity); activity.onOptionsItemSelected(item); assertTrue(shadowActivity.isFinishing()); }
@Test // ActionMenuItem... public void testOnCreateOptions() { fragment.onCreateOptionsMenu(menu, inflater); MenuItem item = new RoboMenuItem(R.id.action_add); fragment.onOptionsItemSelected(item); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent i = shadowActivity.getNextStartedActivity(); assertEquals(i.getComponent().getClassName(), ChooseContactActivity.class.getName()); }
@Test public void testContextRename() { adapter.updateDataSet(chats); String newTitle = "NewTitle"; 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_chat_rename); item.setGroupId(R.id.context_tab_chat_group); fragment.onContextItemSelected(item); android.support.v7.app.AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog(); Button b = dialog.getButton(Dialog.BUTTON_POSITIVE); EditText editText = (EditText) dialog.findViewById(R.id.alter_dialog_edit); editText.setText(newTitle); b.performClick(); chats = helper.getChats(); Chat chat = chats.get(0); assertEquals(chat.getTitle(), newTitle); b = dialog.getButton(Dialog.BUTTON_NEGATIVE); b.performClick(); assertFalse(dialog.isShowing()); }
@Test public void testOptionItem(){ RoboMenuItem item = new RoboMenuItem(R.id.action_add); RoboMenuItem item2 = new RoboMenuItem(R.id.action_settings); RoboMenuItem item3 = new RoboMenuItem(android.R.id.home); assertTrue(activity.onOptionsItemSelected(item)); assertTrue(activity.onOptionsItemSelected(item2)); assertTrue(activity.onOptionsItemSelected(item3)); }