1 /*
2  * Copyright (C) 2018 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.settings.ui.testutils;
18 
19 import static org.junit.Assert.assertNotNull;
20 
21 import android.support.test.uiautomator.By;
22 import android.support.test.uiautomator.Direction;
23 import android.support.test.uiautomator.UiDevice;
24 import android.support.test.uiautomator.UiObject2;
25 import android.support.test.uiautomator.Until;
26 
27 public class SettingsTestUtils {
28 
29     public static final String SETTINGS_PACKAGE = "com.android.settings";
30     public static final int TIMEOUT = 2000;
31 
scrollToTop(UiDevice device)32     private void scrollToTop(UiDevice device) throws Exception {
33         int count = 5;
34         UiObject2 view = null;
35         while (count >= 0) {
36             view = device.wait(
37                     Until.findObject(By.res(SETTINGS_PACKAGE, "main_content")),
38                     TIMEOUT);
39             view.scroll(Direction.UP, 1.0f);
40             count--;
41         }
42     }
43 
assertTitleMatch(UiDevice device, String title)44     public static void assertTitleMatch(UiDevice device, String title) {
45         int maxAttempt = 5;
46         UiObject2 item = null;
47         UiObject2 view = null;
48         while (maxAttempt-- > 0) {
49             item = device.wait(Until.findObject(By.res("android:id/title").text(title)), TIMEOUT);
50             if (item == null) {
51                 view = device.wait(
52                         Until.findObject(By.res(SETTINGS_PACKAGE, "main_content")),
53                         TIMEOUT);
54                 view.scroll(Direction.DOWN, 1.0f);
55             } else {
56                 return;
57             }
58         }
59         assertNotNull(String.format("%s in Setting has not been loaded correctly", title), item);
60     }
61 }
62