-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalTestJava.txt.txt
More file actions
216 lines (204 loc) · 11.1 KB
/
Copy pathCalTestJava.txt.txt
File metadata and controls
216 lines (204 loc) · 11.1 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.example.myapplication2;
import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class CalTestJava {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
// Test No 3 Calculator start, assert result and close and go back to the starting screen
public void calTestJava() {
ViewInteraction viewGroup = onView(
allOf(childAtPosition(
allOf(withId(android.R.id.content),
childAtPosition(
withId(R.id.decor_content_parent),
1)),
0),
isDisplayed()));
viewGroup.check(matches(isDisplayed()));
// Allow
ViewInteraction viewGroup2 = onView(
allOf(childAtPosition(
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.workspace),
0),
0),
isDisplayed()));
viewGroup2.check(matches(isDisplayed()));
// Press button to go to search form
ViewInteraction imageView = onView(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.all_apps_handle), withContentDescription("Apps list"),
childAtPosition(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.page_indicator),
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.drag_layer),
3)),
0),
isDisplayed()));
imageView.check(matches(isDisplayed()));
// App list screen
ViewInteraction editText = onView(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_box_input), withText(" Search Apps"),
childAtPosition(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_container),
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.main_content),
0)),
0),
isDisplayed()));
editText.check(matches(isDisplayed()));
ViewInteraction editText2 = onView(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_box_input), withText(" Search Apps"),
childAtPosition(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_container),
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.main_content),
0)),
0),
isDisplayed()));
editText2.check(matches(withText(" Search Apps")));
// Add search term 'Cal' to search control
ViewInteraction editText3 = onView(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_box_input), withText("Cal"),
childAtPosition(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.search_container),
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.main_content),
0)),
0),
isDisplayed()));
editText3.check(matches(withText("Cal")));
// Calculator app exists
ViewInteraction textView = onView(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.icon), withText("Calculator"), withContentDescription("Calculator"),
childAtPosition(
allOf(withId(com.google.android.apps.nexuslauncher.R.id.apps_list_view),
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.main_content),
2)),
1),
isDisplayed()));
textView.check(matches(isDisplayed()));
// Assert calculator controls
ViewInteraction viewGroup3 = onView(
allOf(withId(com.android.calculator2.R.id.pad_numeric),
childAtPosition(
allOf(withContentDescription("Numbers and basic operations"),
withParent(withId(com.android.calculator2.R.id.pad_pager))),
0),
isDisplayed()));
viewGroup3.check(matches(isDisplayed()));
// Assert cal window
ViewInteraction textView2 = onView(
allOf(withId(com.android.calculator2.R.id.formula), withContentDescription("No formula"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.formula_container),
childAtPosition(
withId(com.android.calculator2.R.id.display),
1)),
0),
isDisplayed()));
textView2.check(matches(isDisplayed()));
// Add 1+1
ViewInteraction textView3 = onView(
allOf(withId(com.android.calculator2.R.id.formula), withText("1+1"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.formula_container),
childAtPosition(
withId(com.android.calculator2.R.id.display),
1)),
0),
isDisplayed()));
textView3.check(matches(withText("1+1")));
// Assert =
ViewInteraction button = onView(
allOf(withId(com.android.calculator2.R.id.eq), withContentDescription("equals"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.pad_numeric),
childAtPosition(
withContentDescription("Numbers and basic operations"),
0)),
11),
isDisplayed()));
button.check(matches(isDisplayed()));
// Assert result
ViewInteraction textView4 = onView(
allOf(withId(com.android.calculator2.R.id.result), withText("2"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.display),
childAtPosition(
withId(com.android.calculator2.R.id.main_calculator),
0)),
2),
isDisplayed()));
textView4.check(matches(withText("2")));
// Confirm result
ViewInteraction textView5 = onView(
allOf(withId(com.android.calculator2.R.id.result), withText("2"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.display),
childAtPosition(
withId(com.android.calculator2.R.id.main_calculator),
0)),
2),
isDisplayed()));
textView5.check(matches(isDisplayed()));
// Clear result
ViewInteraction button2 = onView(
allOf(withId(com.android.calculator2.R.id.clr), withContentDescription("clear"),
childAtPosition(
allOf(withId(com.android.calculator2.R.id.pad_operator),
childAtPosition(
withContentDescription("Numbers and basic operations"),
1)),
0),
isDisplayed()));
button2.check(matches(isDisplayed()));
// Go back to the main screen
ViewInteraction viewGroup4 = onView(
allOf(childAtPosition(
childAtPosition(
withId(com.google.android.apps.nexuslauncher.R.id.workspace),
0),
0),
isDisplayed()));
viewGroup4.check(matches(isDisplayed()));
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}