Home | History | Annotate | Download | only in base
      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 
     17 package com.google.android.apps.common.testing.ui.espresso.base;
     18 
     19 import static org.hamcrest.MatcherAssert.assertThat;
     20 import static org.hamcrest.Matchers.sameInstance;
     21 
     22 import com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException;
     23 import com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException;
     24 import com.google.android.apps.common.testing.ui.espresso.ViewFinder;
     25 
     26 import android.test.InstrumentationTestCase;
     27 import android.test.UiThreadTest;
     28 import android.view.View;
     29 import android.widget.RelativeLayout;
     30 import android.widget.TextView;
     31 
     32 import org.hamcrest.Matchers;
     33 
     34 import javax.inject.Provider;
     35 
     36 /** Unit tests for {@link ViewFinderImpl}. */
     37 public 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