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