|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package androidx.test.espresso.base; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import android.view.View; |
| 25 | +import android.widget.TextView; |
| 26 | +import androidx.test.espresso.NoMatchingViewException; |
| 27 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 28 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 29 | +import androidx.test.platform.io.PlatformTestStorage; |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import org.hamcrest.BaseMatcher; |
| 34 | +import org.hamcrest.Description; |
| 35 | +import org.hamcrest.Matcher; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Rule; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.mockito.Mock; |
| 41 | +import org.mockito.junit.MockitoJUnit; |
| 42 | +import org.mockito.junit.MockitoRule; |
| 43 | + |
| 44 | +@RunWith(AndroidJUnit4.class) |
| 45 | +public class NoMatchingViewExceptionHandlerTest { |
| 46 | + |
| 47 | + @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 48 | + |
| 49 | + private final AtomicInteger failureCount = new AtomicInteger(); |
| 50 | + private NoMatchingViewExceptionHandler handler; |
| 51 | + private Matcher<View> viewMatcher; |
| 52 | + @Mock private PlatformTestStorage testStorage; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() throws IOException { |
| 56 | + when(testStorage.openOutputFile("view-hierarchy-1.txt")) |
| 57 | + .thenReturn(new ByteArrayOutputStream()); |
| 58 | + handler = |
| 59 | + new NoMatchingViewExceptionHandler( |
| 60 | + testStorage, failureCount, NoMatchingViewException.class); |
| 61 | + viewMatcher = |
| 62 | + new BaseMatcher<View>() { |
| 63 | + @Override |
| 64 | + public boolean matches(Object o) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void describeTo(Description description) { |
| 70 | + description.appendText("A view matcher"); |
| 71 | + } |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void handle_noMatchingViewException() throws IOException { |
| 77 | + NoMatchingViewException noMatchingViewException = |
| 78 | + new NoMatchingViewException.Builder() |
| 79 | + .withViewMatcher(viewMatcher) |
| 80 | + .withRootView( |
| 81 | + new TextView(InstrumentationRegistry.getInstrumentation().getTargetContext())) |
| 82 | + .build(); |
| 83 | + String viewHierarchyMsg = |
| 84 | + "\n\nView Hierarchy:\n" |
| 85 | + + "+>TextView{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false," |
| 86 | + + " has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true," |
| 87 | + + " is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false," |
| 88 | + + " layout-params=null, tag=null, root-is-layout-requested=true," |
| 89 | + + " has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false," |
| 90 | + + " has-links=false} "; |
| 91 | + |
| 92 | + failureCount.incrementAndGet(); |
| 93 | + NoMatchingViewException thrown = |
| 94 | + assertThrows( |
| 95 | + NoMatchingViewException.class, |
| 96 | + () -> handler.handle(noMatchingViewException, viewMatcher)); |
| 97 | + |
| 98 | + assertThat(thrown) |
| 99 | + .hasMessageThat() |
| 100 | + .contains("No views in hierarchy found matching: A view matcher" + viewHierarchyMsg); |
| 101 | + verify(testStorage).openOutputFile(eq("view-hierarchy-1.txt")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void handle_nonNoMatchingViewException() { |
| 106 | + // No-op. No exception should be thrown. |
| 107 | + handler.handle(new Throwable("A random error"), viewMatcher); |
| 108 | + } |
| 109 | +} |
0 commit comments