1 /*
2  * Copyright (C) 2024 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.providers.media.photopicker.ui.settings;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import android.text.format.DateUtils;
22 
23 import androidx.annotation.NonNull;
24 import androidx.test.uiautomator.UiDevice;
25 import androidx.test.uiautomator.UiObject;
26 import androidx.test.uiautomator.UiSelector;
27 
28 public class SettingsTestUtils {
29     private static final long TIMEOUT = 30 * DateUtils.SECOND_IN_MILLIS;
30     private static final String REGEX_PACKAGE_NAME =
31             "com(.google)?.android.providers.media(.module)?";
32     private static final String SETTINGS_ACTIVITY_ROOT_RES_ID =
33             REGEX_PACKAGE_NAME + ":id/settings_activity_root";
34 
35     /**
36      * Verify if the {@link com.android.providers.media.photopicker.PhotoPickerSettingsActivity}
37      * is launched and visible.
38      */
verifySettingsActivityIsVisible(@onNull UiDevice uiDevice)39     static void verifySettingsActivityIsVisible(@NonNull UiDevice uiDevice) {
40         // id/settings_activity_root is the root layout in activity_photo_picker_settings.xml
41         assertWithMessage("Timed out waiting for settings activity to appear")
42                 .that(uiDevice.findObject(
43                         new UiSelector().resourceIdMatches(SETTINGS_ACTIVITY_ROOT_RES_ID))
44                         .waitForExists(TIMEOUT))
45                 .isTrue();
46     }
47 
48     /**
49      * Verify if the account of the selected {@link android.provider.CloudMediaProvider} is visible.
50      */
verifySettingsCloudProviderAccountIsVisible(@onNull UiDevice uiDevice, @NonNull String cloudAccount)51     static void verifySettingsCloudProviderAccountIsVisible(@NonNull UiDevice uiDevice,
52             @NonNull String cloudAccount) {
53         assertWithMessage("Timed out waiting for the selected cloud provider account to appear")
54                 .that(uiDevice.findObject(new UiSelector().textContains(cloudAccount))
55                         .waitForExists(TIMEOUT))
56                 .isTrue();
57     }
58 
59     /**
60      * Find and click the settings provider option extra widget to launch provider settings.
61      */
findAndClickSettingsProviderOptionExtraWidget(@onNull UiDevice uiDevice)62     static void findAndClickSettingsProviderOptionExtraWidget(@NonNull UiDevice uiDevice)
63             throws Exception {
64         final UiObject selectorExtraWidget = uiDevice.findObject(new UiSelector()
65                 .resourceIdMatches(REGEX_PACKAGE_NAME + ":id/selector_extra_widget"));
66 
67         assertWithMessage("Timed out waiting for the provider option extra widget to appear")
68                 .that(selectorExtraWidget.waitForExists(TIMEOUT))
69                 .isTrue();
70 
71         clickAndWait(uiDevice, selectorExtraWidget);
72     }
73 
clickAndWait(@onNull UiDevice uiDevice, @NonNull UiObject uiObject)74     private static void clickAndWait(@NonNull UiDevice uiDevice, @NonNull UiObject uiObject)
75             throws Exception {
76         uiObject.click();
77         uiDevice.waitForIdle();
78     }
79 }
80