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