• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.users;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.support.test.InstrumentationRegistry;
23 import android.support.test.filters.SmallTest;
24 import android.support.test.runner.AndroidJUnit4;
25 import android.support.test.uiautomator.UiDevice;
26 import android.support.test.uiautomator.UiObject;
27 import android.support.test.uiautomator.UiSelector;
28 import android.support.test.uiautomator.UiScrollable;
29 import android.support.test.uiautomator.UiObjectNotFoundException;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @RunWith(AndroidJUnit4.class)
36 @SmallTest
37 public class UserSettingsTest {
38 
39     private static final String SYSTEM = "System";
40     private static final String ADVANCED = "Advanced";
41     private static final String USERS = "Multiple users";
42     private static final String EMERGNENCY_INFO = "Emergency information";
43     private static final String ADD_USERS_WHEN_LOCKED = "Add users";
44     private static final String SWITCH_USER_BUTTON = "com.android.systemui:id/multi_user_switch";
45     private static final String SETTINGS_BUTTON = "com.android.systemui:id/settings_button";
46     private static final String PRIMARY_USER = "Owner";
47     private static final String GUEST_USER = "Guest";
48     private static final String ADD_GUEST = "Add guest";
49     private static final String CONTINUE = "Yes, continue";
50 
51     private UiDevice mDevice;
52     private Context mContext;
53     private String mTargetPackage;
54 
55     @Before
setUp()56     public void setUp() {
57         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
58         mContext = InstrumentationRegistry.getTargetContext();
59         mTargetPackage = mContext.getPackageName();
60     }
61 
62     @Test
testEmergencyInfoNotExists()63     public void testEmergencyInfoNotExists() throws Exception {
64         launchUserSettings();
65         UiObject emergencyInfoPreference =
66             mDevice.findObject(new UiSelector().text(EMERGNENCY_INFO));
67 
68         assertThat(emergencyInfoPreference.exists()).isFalse();
69     }
70 
71     @Test
testAddUsersWhenLockedNotExists()72     public void testAddUsersWhenLockedNotExists() throws Exception {
73         launchUserSettings();
74         UiObject addUsersPreference =
75             mDevice.findObject(new UiSelector().text(ADD_USERS_WHEN_LOCKED));
76         assertThat(addUsersPreference.exists()).isFalse();
77     }
78 
79     @Test
testUsersExistsOnSecondaryUser()80     public void testUsersExistsOnSecondaryUser() throws Exception {
81         // switch to guest user
82         switchToOrCreateGuest();
83         // launch settings (launch from intent doesn't work, hence launch from quick settings)
84         mDevice.openQuickSettings();
85         mDevice.findObject(new UiSelector().resourceId(SETTINGS_BUTTON)).click();
86         // launch system settings and expand whole screen
87         final UiScrollable settings = new UiScrollable(
88             new UiSelector().packageName(mTargetPackage).scrollable(true));
89         final String titleSystem = SYSTEM;
90         settings.scrollTextIntoView(titleSystem);
91         mDevice.findObject(new UiSelector().text(titleSystem)).click();
92         mDevice.findObject(new UiSelector().text(ADVANCED)).click();
93 
94         final boolean hasUsersSettings = mDevice.findObject(new UiSelector().text(USERS)).exists();
95 
96         // switch back to primary user
97         mDevice.openQuickSettings();
98         mDevice.findObject(new UiSelector().resourceId(SWITCH_USER_BUTTON)).click();
99         mDevice.findObject(new UiSelector().text(PRIMARY_USER)).click();
100 
101         assertThat(hasUsersSettings).isTrue();
102     }
103 
launchSettings()104     private void launchSettings() {
105         Intent settingsIntent = new Intent(Intent.ACTION_MAIN)
106                 .addCategory(Intent.CATEGORY_LAUNCHER)
107                 .setPackage(mTargetPackage)
108                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
109         mContext.startActivity(settingsIntent);
110     }
111 
launchUserSettings()112     private void launchUserSettings() throws Exception  {
113         launchSettings();
114         final UiScrollable settings = new UiScrollable(
115                 new UiSelector().packageName(mTargetPackage).scrollable(true));
116         final String titleSystem = SYSTEM;
117         settings.scrollTextIntoView(titleSystem);
118         mDevice.findObject(new UiSelector().text(titleSystem)).click();
119         mDevice.findObject(new UiSelector().text(ADVANCED)).click();
120         mDevice.findObject(new UiSelector().text(USERS)).click();
121     }
122 
switchToOrCreateGuest()123     private void switchToOrCreateGuest() throws UiObjectNotFoundException {
124         mDevice.openQuickSettings();
125         mDevice.findObject(new UiSelector().resourceId(SWITCH_USER_BUTTON)).click();
126         // if no existing guest user, select "Add guest", otherwise select "Guest"
127         final UiObject addGuest = mDevice.findObject(new UiSelector().text(ADD_GUEST));
128         if (addGuest.exists()) {
129             addGuest.click();
130             mDevice.waitForIdle();
131             mDevice.pressBack();
132         } else {
133             mDevice.findObject(new UiSelector().text(GUEST_USER)).click();
134             mDevice.waitForIdle();
135             mDevice.findObject(new UiSelector().text(CONTINUE)).click();
136             mDevice.waitForIdle();
137         }
138     }
139 }
140