ViewFinderImplTest.java revision f69eb9ac2856f470cb79f57141f711ed3ceed99d
1/* 2 * Copyright (C) 2014 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 17package com.google.android.apps.common.testing.ui.espresso.base; 18 19import static org.hamcrest.MatcherAssert.assertThat; 20import static org.hamcrest.Matchers.sameInstance; 21 22import com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException; 23import com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException; 24import com.google.android.apps.common.testing.ui.espresso.ViewFinder; 25 26import android.test.InstrumentationTestCase; 27import android.test.UiThreadTest; 28import android.view.View; 29import android.widget.RelativeLayout; 30import android.widget.TextView; 31 32import org.hamcrest.Matchers; 33 34import javax.inject.Provider; 35 36/** Unit tests for {@link ViewFinderImpl}. */ 37public class ViewFinderImplTest extends InstrumentationTestCase { 38 private Provider<View> testViewProvider; 39 private RelativeLayout testView; 40 private View child1; 41 private View child2; 42 private View child3; 43 private View child4; 44 private View nestedChild; 45 46 @Override 47 public void setUp() throws Exception { 48 super.setUp(); 49 testView = new RelativeLayout(getInstrumentation().getTargetContext()); 50 child1 = new TextView(getInstrumentation().getTargetContext()); 51 child1.setId(1); 52 child2 = new TextView(getInstrumentation().getTargetContext()); 53 child2.setId(2); 54 child3 = new TextView(getInstrumentation().getTargetContext()); 55 child3.setId(3); 56 child4 = new TextView(getInstrumentation().getTargetContext()); 57 child4.setId(4); 58 nestedChild = new TextView(getInstrumentation().getTargetContext()); 59 nestedChild.setId(5); 60 RelativeLayout nestingLayout = new RelativeLayout(getInstrumentation().getTargetContext()); 61 nestingLayout.addView(nestedChild); 62 testView.addView(child1); 63 testView.addView(child2); 64 testView.addView(nestingLayout); 65 testView.addView(child3); 66 testView.addView(child4); 67 testViewProvider = new Provider<View>() { 68 @Override 69 public View get() { 70 return testView; 71 } 72 73 @Override 74 public String toString() { 75 return "of(" + testView + ")"; 76 } 77 }; 78 } 79 80 @UiThreadTest 81 public void testGetView_present() { 82 ViewFinder finder = new ViewFinderImpl(sameInstance(nestedChild), testViewProvider); 83 assertThat(finder.getView(), sameInstance(nestedChild)); 84 } 85 86 @UiThreadTest 87 public void testGetView_missing() { 88 ViewFinder finder = new ViewFinderImpl(Matchers.<View>nullValue(), testViewProvider); 89 try { 90 finder.getView(); 91 fail("No children should pass that matcher!"); 92 } catch (NoMatchingViewException expected) {} 93 } 94 95 @UiThreadTest 96 public void testGetView_multiple() { 97 ViewFinder finder = new ViewFinderImpl(Matchers.<View>notNullValue(), testViewProvider); 98 try { 99 finder.getView(); 100 fail("All nodes hit that matcher!"); 101 } catch (AmbiguousViewMatcherException expected) {} 102 } 103 104 public void testFind_offUiThread() { 105 ViewFinder finder = new ViewFinderImpl(sameInstance(nestedChild), testViewProvider); 106 try { 107 finder.getView(); 108 fail("not on main thread, should die."); 109 } catch (IllegalStateException expected) {} 110 } 111 112} 113