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 
17 package com.android.bedstead.testapp;
18 
19 import static com.android.queryable.queries.ActivityQuery.activity;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.testng.Assert.assertThrows;
24 
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 import java.util.HashSet;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34 
35 @RunWith(JUnit4.class)
36 public class TestAppProviderTest {
37 
38     // Expects that this package name matches an actual test app
39     private static final String EXISTING_PACKAGENAME = "com.android.bedstead.testapp.EmptyTestApp";
40 
41     // Expects that this package name does not match an actual test app
42     private static final String NOT_EXISTING_PACKAGENAME = "not.existing.test.app";
43 
44     // Expected to be a class name which is used in a test app
45     private static final String KNOWN_EXISTING_TESTAPP_ACTIVITY_CLASSNAME =
46             "android.testapp.activity";
47 
48     private static final String QUERY_ONLY_TEST_APP_PACKAGE_NAME = "com.android.RemoteDPC";
49 
50     private static final String PERMISSION_DECLARED_BY_TESTAPP = "android.permission.READ_CALENDAR";
51 
52     private static final String METADATA_KEY = "test-metadata-key";
53     private static final String METADATA_VALUE = "test-metadata-value";
54 
55     private TestAppProvider mTestAppProvider;
56 
57     @Before
setup()58     public void setup() {
59         mTestAppProvider = new TestAppProvider();
60     }
61 
62     @Test
get_queryMatches_returnsTestApp()63     public void get_queryMatches_returnsTestApp() {
64         TestAppQueryBuilder query = mTestAppProvider.query()
65                 .wherePackageName().isEqualTo(EXISTING_PACKAGENAME);
66 
67         assertThat(query.get()).isNotNull();
68     }
69 
70     @Test
get_queryMatches_packageNameIsSet()71     public void get_queryMatches_packageNameIsSet() {
72         TestAppQueryBuilder query = mTestAppProvider.query()
73                 .wherePackageName().isEqualTo(EXISTING_PACKAGENAME);
74 
75         assertThat(query.get().packageName()).isEqualTo(EXISTING_PACKAGENAME);
76     }
77 
78     @Test
get_queryDoesNotMatch_throwsException()79     public void get_queryDoesNotMatch_throwsException() {
80         TestAppQueryBuilder query = mTestAppProvider.query()
81                 .wherePackageName().isEqualTo(NOT_EXISTING_PACKAGENAME);
82 
83         assertThrows(NotFoundException.class, query::get);
84     }
85 
86     @Test
any_returnsTestApp()87     public void any_returnsTestApp() {
88         assertThat(mTestAppProvider.any()).isNotNull();
89     }
90 
91     @Test
any_returnsDifferentTestApps()92     public void any_returnsDifferentTestApps() {
93         assertThat(mTestAppProvider.any()).isNotEqualTo(mTestAppProvider.any());
94     }
95 
96     @Test
query_onlyReturnsTestAppOnce()97     public void query_onlyReturnsTestAppOnce() {
98         mTestAppProvider.query().wherePackageName().isEqualTo(EXISTING_PACKAGENAME).get();
99 
100         TestAppQueryBuilder query = mTestAppProvider.query().wherePackageName().isEqualTo(EXISTING_PACKAGENAME);
101 
102         assertThrows(NotFoundException.class, query::get);
103     }
104 
105     @Test
any_doesNotReturnPackageQueryOnlyTestApps()106     public void any_doesNotReturnPackageQueryOnlyTestApps() {
107         Set<String> testAppPackageNames = new HashSet<>();
108 
109         while (true) {
110             try {
111                 testAppPackageNames.add(mTestAppProvider.any().packageName());
112             } catch (NotFoundException e) {
113                 // Expected when we run out of test apps
114                 break;
115             }
116         }
117 
118         assertThat(testAppPackageNames).doesNotContain(QUERY_ONLY_TEST_APP_PACKAGE_NAME);
119     }
120 
121     @Test
query_queryByPackageName_doesReturnPackageQueryOnlyTestApps()122     public void query_queryByPackageName_doesReturnPackageQueryOnlyTestApps() {
123         assertThat(
124                 mTestAppProvider.query()
125                         .wherePackageName().isEqualTo(QUERY_ONLY_TEST_APP_PACKAGE_NAME)
126                         .get()).isNotNull();
127     }
128 
129     @Test
query_byFeature_returnsDifferentTestAppsForSameQuery()130     public void query_byFeature_returnsDifferentTestAppsForSameQuery() {
131         TestApp firstResult = mTestAppProvider.query()
132                 .whereTestOnly().isFalse()
133                 .get();
134         TestApp secondResult = mTestAppProvider.query()
135                 .whereTestOnly().isFalse()
136                 .get();
137 
138         assertThat(firstResult).isNotEqualTo(secondResult);
139     }
140 
141     @Test
query_testOnly_returnsMatching()142     public void query_testOnly_returnsMatching() {
143         TestApp testApp = mTestAppProvider.query()
144                 .whereTestOnly().isTrue()
145                 .get();
146 
147         assertThat(testApp.testOnly()).isTrue();
148     }
149 
150     @Test
query_notTestOnly_returnsMatching()151     public void query_notTestOnly_returnsMatching() {
152         TestApp testApp = mTestAppProvider.query()
153                 .whereTestOnly().isFalse()
154                 .get();
155 
156         assertThat(testApp.testOnly()).isFalse();
157     }
158 
159     @Test
query_minSdkVersion_returnsMatching()160     public void query_minSdkVersion_returnsMatching() {
161         TestApp testApp = mTestAppProvider.query()
162                 .whereMinSdkVersion().isGreaterThanOrEqualTo(28)
163                 .get();
164 
165         assertThat(testApp.minSdkVersion()).isAtLeast(28);
166     }
167 
168     @Test
query_targetSdkVersion_returnsMatching()169     public void query_targetSdkVersion_returnsMatching() {
170         TestApp testApp = mTestAppProvider.query()
171                 .whereTargetSdkVersion().isGreaterThanOrEqualTo(28)
172                 .get();
173 
174         assertThat(testApp.targetSdkVersion()).isAtLeast(28);
175     }
176 
177     @Test
query_withPermission_returnsMatching()178     public void query_withPermission_returnsMatching() {
179         TestApp testApp = mTestAppProvider.query()
180                 .wherePermissions().contains(PERMISSION_DECLARED_BY_TESTAPP)
181                 .get();
182 
183         assertThat(testApp.permissions()).contains(PERMISSION_DECLARED_BY_TESTAPP);
184     }
185 
186     @Test
query_withoutPermission_returnsMatching()187     public void query_withoutPermission_returnsMatching() {
188         TestApp testApp = mTestAppProvider.query()
189                 .wherePermissions().doesNotContain(PERMISSION_DECLARED_BY_TESTAPP)
190                 .get();
191 
192         assertThat(testApp.permissions()).doesNotContain(PERMISSION_DECLARED_BY_TESTAPP);
193     }
194 
195     @Test
query_metadata_returnsMatching()196     public void query_metadata_returnsMatching() {
197         TestApp testApp = mTestAppProvider.query()
198                 .whereMetadata().key(METADATA_KEY).stringValue().isEqualTo(METADATA_VALUE)
199                 .get();
200 
201         assertThat(testApp.metadata().get(METADATA_KEY)).isEqualTo(METADATA_VALUE);
202     }
203 
204     @Test
query_withExistingActivity_returnsMatching()205     public void query_withExistingActivity_returnsMatching() {
206         TestApp testApp = mTestAppProvider.query()
207                 .whereActivities().contains(
208                         activity().activityClass()
209                             .className().isEqualTo(KNOWN_EXISTING_TESTAPP_ACTIVITY_CLASSNAME)
210                 )
211                 .get();
212 
213         Set<String> activityClassNames = testApp.activities().stream()
214                 .map(a -> a.className()).collect(Collectors.toSet());
215         assertThat(activityClassNames).contains(KNOWN_EXISTING_TESTAPP_ACTIVITY_CLASSNAME);
216     }
217 
218     @Test
query_withAnyActivity_returnsMatching()219     public void query_withAnyActivity_returnsMatching() {
220         TestApp testApp = mTestAppProvider.query()
221                 .whereActivities().isNotEmpty()
222                 .get();
223 
224         assertThat(testApp.activities()).isNotEmpty();
225     }
226 
227     @Test
query_withNoActivity_returnsMatching()228     public void query_withNoActivity_returnsMatching() {
229         TestApp testApp = mTestAppProvider.query()
230                 .whereActivities().isEmpty()
231                 .get();
232 
233         assertThat(testApp.activities()).isEmpty();
234     }
235 
236     @Test
query_doesNotSpecifySharedUserId_sharedUserIdIsNull()237     public void query_doesNotSpecifySharedUserId_sharedUserIdIsNull() {
238         TestApp testApp = mTestAppProvider.query()
239                 .get();
240 
241         assertThat(testApp.sharedUserId()).isNull();
242     }
243 
244     @Test
245     @Ignore("re-enable when we have a test app which has a shareuserid")
query_doesSpecifySharedUserId_matches()246     public void query_doesSpecifySharedUserId_matches() {
247         TestApp testApp = mTestAppProvider.query()
248                 .whereSharedUserId().isEqualTo("com.android.bedstead")
249                 .get();
250 
251         assertThat(testApp.sharedUserId()).isEqualTo("com.android.bedstead");
252     }
253 
254     @Test
query_specifiesNullSharedUserId_matches()255     public void query_specifiesNullSharedUserId_matches() {
256         TestApp testApp = mTestAppProvider.query()
257                 .whereSharedUserId().isNull()
258                 .get();
259 
260         assertThat(testApp.sharedUserId()).isNull();
261     }
262 }
263