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 package com.android.settings.accounts;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.accounts.Account;
21 import android.accounts.AccountManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject;
26 import android.support.test.uiautomator.UiObjectNotFoundException;
27 import android.support.test.uiautomator.UiScrollable;
28 import android.support.test.uiautomator.UiSelector;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 @RunWith(AndroidJUnit4.class)
39 @SmallTest
40 public class AccountsSettingsTest {
41 
42     private static final String ACCOUNTS = "Accounts";
43     private static final String ACCOUNT_TYPE = "com.settingstest.account-prefs";
44     private static final String PREF_TITLE = "Test preference for external account";
45 
46     private UiDevice mDevice;
47     private Context mContext;
48     private String mTargetPackage;
49 
50     @Before
setUp()51     public void setUp() {
52         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
53         mContext = InstrumentationRegistry.getTargetContext();
54         mTargetPackage = mContext.getPackageName();
55     }
56 
57     @Test
testExternalAccountInfoExists()58     public void testExternalAccountInfoExists() throws UiObjectNotFoundException {
59         // add a test account
60         final String testAccountName = "Test Account";
61         final Account account = new Account(testAccountName, ACCOUNT_TYPE);
62         final AccountManager accountManager = AccountManager.get(mContext);
63         final boolean accountAdded =
64             accountManager.addAccountExplicitly(account, null /* password */, null /* userdata */);
65         assertThat(accountAdded).isTrue();
66 
67         // launch Accounts Settings and select the test account
68         launchAccountsSettings();
69         mDevice.findObject(new UiSelector().text(testAccountName)).click();
70         final UiObject testPreference = mDevice.findObject(new UiSelector().text(PREF_TITLE));
71         // remove test account
72         accountManager.removeAccountExplicitly(account);
73 
74         assertThat(testPreference.exists()).isTrue();
75     }
76 
launchAccountsSettings()77     private void launchAccountsSettings() throws UiObjectNotFoundException  {
78         // launch settings
79         Intent settingsIntent = new Intent(Intent.ACTION_MAIN)
80             .addCategory(Intent.CATEGORY_LAUNCHER)
81             .setPackage(mTargetPackage)
82             .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83         mContext.startActivity(settingsIntent);
84         // selects Accounts
85         final UiScrollable settings = new UiScrollable(
86                 new UiSelector().packageName(mTargetPackage).scrollable(true));
87         final String titleAccounts = ACCOUNTS;
88         settings.scrollTextIntoView(titleAccounts);
89         mDevice.findObject(new UiSelector().text(titleAccounts)).click();
90     }
91 }
92