@Test public void clickOn_shouldCallClickListener() throws Exception { View view = new View(context); shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); OnClickListener testOnClickListener = mock(OnClickListener.class); view.setOnClickListener(testOnClickListener); ShadowView.clickOn(view); verify(testOnClickListener).onClick(view); }
@Test public void checkedClick_shouldClickOnView() throws Exception { OnClickListener clickListener = mock(OnClickListener.class); shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); view.setOnClickListener(clickListener); shadowOf(view).checkedPerformClick(); verify(clickListener).onClick(view); }
@Test public void performLongClick_shouldClickOnView() throws Exception { OnLongClickListener clickListener = mock(OnLongClickListener.class); shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); view.setOnLongClickListener(clickListener); view.performLongClick(); verify(clickListener).onLongClick(view); }
@Test public void itKnowsIfTheViewIsShown() { shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); // a view is only considered visible if it is added to a view root view.setVisibility(View.VISIBLE); assertThat(view.isShown()).isTrue(); }
@Test public void shouldKnowIfThisOrAncestorsAreVisible() throws Exception { assertThat(view.isShown()).named("view isn't considered shown unless it has a view root").isFalse(); shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); assertThat(view.isShown()).isTrue(); shadowOf(view).setMyParent(null); ViewGroup parent = new LinearLayout(context); parent.addView(view); ViewGroup grandParent = new LinearLayout(context); grandParent.addView(parent); grandParent.setVisibility(View.GONE); assertFalse(view.isShown()); }
@Test public void getPositionForView_shouldReturnInvalidPositionForViewThatIsNotFound() throws Exception { prepareWithListAdapter(); View view = new View(context); shadowOf(view).setMyParent(ReflectionHelpers.createNullProxy(ViewParent.class)); // Android implementation requires the item have a parent assertThat(listView.getPositionForView(view)).isEqualTo(AdapterView.INVALID_POSITION); }
@Test public void shouldFocus() throws Exception { final List<String> transcript = new ArrayList<>(); view.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { transcript.add(hasFocus ? "Gained focus" : "Lost focus"); } }); assertFalse(view.isFocused()); assertFalse(view.hasFocus()); assertThat(transcript).isEmpty(); view.requestFocus(); assertFalse(view.isFocused()); assertFalse(view.hasFocus()); assertThat(transcript).isEmpty(); view.setFocusable(true); view.requestFocus(); assertTrue(view.isFocused()); assertTrue(view.hasFocus()); assertThat(transcript).containsExactly("Gained focus"); transcript.clear(); shadowOf(view) .setMyParent(new LinearLayout(context)); // we can never lose focus unless a parent can // take it view.clearFocus(); assertFalse(view.isFocused()); assertFalse(view.hasFocus()); assertThat(transcript).containsExactly("Lost focus"); }