summaryrefslogtreecommitdiff
path: root/espresso/espresso-sample/src/androidTest/java/com/google/android/apps/common/testing/ui/testapp/AdapterViewTest.java
blob: fd531ec19084f8472b2d4bbb38e5e3fb497189a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.common.testing.ui.testapp;

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static com.google.android.apps.common.testing.ui.testapp.LongListMatchers.isFooter;
import static com.google.android.apps.common.testing.ui.testapp.LongListMatchers.withItemContent;
import static com.google.android.apps.common.testing.ui.testapp.LongListMatchers.withItemSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
 * Demonstrates the usage of
 * {@link com.google.android.apps.common.testing.ui.espresso.Espresso#onData(org.hamcrest.Matcher)}
 * to match data within list views.
 */
@LargeTest
public class AdapterViewTest extends ActivityInstrumentationTestCase2<LongListActivity> {

  @SuppressWarnings("deprecation")
  public AdapterViewTest() {
    // This constructor was deprecated - but we want to support lower API levels.
    super("com.google.android.apps.common.testing.ui.testapp", LongListActivity.class);
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    getActivity();
  }

  public void testClickOnItem50() {
    // The text view "item: 50" may not exist if we haven't scrolled to it.
    // By using onData api we tell Espresso to look into the Adapter for an item matching
    // the matcher we provide it. Espresso will then bring that item into the view hierarchy
    // and we can click on it.

    onData(withItemContent("item: 50"))
      .perform(click());

    onView(withId(R.id.selection_row_value))
      .check(matches(withText("50")));
  }

  public void testClickOnSpecificChildOfRow60() {
    onData(withItemContent("item: 60"))
      .onChildView(withId(R.id.item_size)) // resource id of second column from xml layout
      .perform(click());

    onView(withId(R.id.selection_row_value))
      .check(matches(withText("60")));

    onView(withId(R.id.selection_column_value))
      .check(matches(withText("2")));
  }

  public void testClickOnFirstAndFifthItemOfLength8() {
    onData(is(withItemSize(8)))
      .atPosition(0)
      .perform(click());

    onView(withId(R.id.selection_row_value))
      .check(matches(withText("10")));

    onData(is(withItemSize(8)))
      .atPosition(4)
      .perform(click());

    onView(withId(R.id.selection_row_value))
      .check(matches(withText("14")));
  }

  @SuppressWarnings("unchecked")
  public void testClickFooter() {
    onData(isFooter())
      .perform(click());

    onView(withId(R.id.selection_row_value))
      .check(matches(withText("100")));
  }

  @SuppressWarnings("unchecked")
  public void testDataItemNotInAdapter(){
    onView(withId(R.id.list))
      .check(matches(not(withAdaptedData(withItemContent("item: 168")))));
  }

  private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
    return new TypeSafeMatcher<View>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("with class name: ");
        dataMatcher.describeTo(description);
      }

      @Override
      public boolean matchesSafely(View view) {
        if (!(view instanceof AdapterView)) {
          return false;
        }
        @SuppressWarnings("rawtypes")
        Adapter adapter = ((AdapterView) view).getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
          if (dataMatcher.matches(adapter.getItem(i))) {
            return true;
          }
        }
        return false;
      }
    };
  }
}