• 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 
17 package android.system.helpers;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.content.Context;
22 import android.support.test.uiautomator.By;
23 import android.support.test.uiautomator.UiDevice;
24 import android.support.test.uiautomator.UiObject2;
25 import android.support.test.uiautomator.Until;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import junit.framework.Assert;
30 
31 import java.util.regex.Pattern;
32 
33 /** Implement common helper methods for account. */
34 public class AccountHelper {
35     private static final String TAG = AccountHelper.class.getSimpleName();
36     public static final int TIMEOUT = 1000;
37     private static AccountHelper sInstance = null;
38     private Context mContext = null;
39     private UiDevice mDevice = null;
40     private ActivityHelper mActivityHelper = null;
41     private DeviceHelper mDeviceHelper = null;
42 
AccountHelper()43     public AccountHelper() {
44         mContext = InstrumentationRegistry.getTargetContext();
45         mActivityHelper = ActivityHelper.getInstance();
46         mDeviceHelper = DeviceHelper.getInstance();
47         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
48     }
49 
getInstance()50     public static AccountHelper getInstance() {
51         if (sInstance == null) {
52             sInstance = new AccountHelper();
53         }
54         return sInstance;
55     }
56 
57     /**
58      * Checks whether a google account has been enabled in device for backup
59      * @return true/false
60      * @throws InterruptedException
61      */
hasDeviceBackupAccount()62     public boolean hasDeviceBackupAccount() throws InterruptedException {
63         mActivityHelper.launchIntent(android.provider.Settings.ACTION_PRIVACY_SETTINGS);
64         dismissInitalDialogs();
65         Pattern pattern = Pattern.compile("Backup account", Pattern.CASE_INSENSITIVE);
66         if (mDeviceHelper.isNexusExperienceDevice()) {
67           pattern = Pattern.compile("Device backup", Pattern.CASE_INSENSITIVE);
68         }
69         UiObject2 deviceBackup = mDevice.wait(Until.findObject(By.text(pattern)),
70                 TIMEOUT * 5);
71         if (deviceBackup!=null){
72             String backupAcct = deviceBackup.getParent().getChildren().get(1).getText();
73             if (backupAcct.equals(getRegisteredGoogleAccountOnDevice())) {
74                 return true;
75             }
76         }
77         return false;
78     }
79 
80     /**
81      * Get registered accounts ensures there is at least one account registered returns the google
82      * account name
83      * @return The registered gogole/gmail account on device
84      */
getRegisteredGoogleAccountOnDevice()85     public String getRegisteredGoogleAccountOnDevice() {
86         Account[] accounts = AccountManager.get(mContext).getAccounts();
87         Assert.assertTrue("Device doesn't have any account registered", accounts.length >= 1);
88         for (int i = 0; i < accounts.length; ++i) {
89             if (accounts[i].type.equals("com.google")) {
90                 return accounts[i].name;
91             }
92         }
93         throw new RuntimeException("The device is not registered with a google account");
94     }
95 
dismissInitalDialogs()96     private void dismissInitalDialogs() throws InterruptedException{
97         UiObject2 backupDialog = mDevice.wait(
98                 Until.findObject(By.text("Backup & reset")),
99                 TIMEOUT);
100         if (backupDialog!=null){
101             backupDialog.click();
102             Thread.sleep(TIMEOUT);
103             UiObject2 alwaysBtn = mDevice.wait(
104                     Until.findObject(By.res("android","button_always")),
105                     TIMEOUT);
106             if (alwaysBtn!=null){
107                 alwaysBtn.click();
108             }
109         }
110         Thread.sleep(TIMEOUT);
111     }
112 }
113