From f2ecc4bf269a8d73901bf8e5fd36012b0f671fe1 Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Fri, 9 Feb 2018 14:27:06 -0800 Subject: [PATCH] Add test to verify external account settings is shown. - add a test service that provides a test authenticator and a test preference screen. Creates corresponding test account and verifies that the preferences defined in the authenticator is displayed from the account detail page. Change-Id: Iab87177ae500d0a36272cf8ecf464139a3dc07cd Fixes: 35353708 Test: make SettingsUnitTests --- tests/unit/AndroidManifest.xml | 9 ++ tests/unit/res/values/strings.xml | 3 + tests/unit/res/xml/account_preferences.xml | 23 +++++ tests/unit/res/xml/authenticator.xml | 22 +++++ .../accounts/AccountsSettingsTest.java | 90 +++++++++++++++++++ .../settings/accounts/Authenticator.java | 74 +++++++++++++++ .../settings/accounts/TestAuthService.java | 36 ++++++++ 7 files changed, 257 insertions(+) create mode 100644 tests/unit/res/xml/account_preferences.xml create mode 100644 tests/unit/res/xml/authenticator.xml create mode 100644 tests/unit/src/com/android/settings/accounts/AccountsSettingsTest.java create mode 100644 tests/unit/src/com/android/settings/accounts/Authenticator.java create mode 100644 tests/unit/src/com/android/settings/accounts/TestAuthService.java diff --git a/tests/unit/AndroidManifest.xml b/tests/unit/AndroidManifest.xml index b22c01bde6e..eccbac36486 100644 --- a/tests/unit/AndroidManifest.xml +++ b/tests/unit/AndroidManifest.xml @@ -53,6 +53,15 @@ + + + + + + + + Hello Manufacturer! Manufacturer Manufacturer hook that can be used to start activity of choice + Settings Test Account + com.settingstest.account-prefs + Test preference for external account diff --git a/tests/unit/res/xml/account_preferences.xml b/tests/unit/res/xml/account_preferences.xml new file mode 100644 index 00000000000..b3917593b11 --- /dev/null +++ b/tests/unit/res/xml/account_preferences.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/tests/unit/res/xml/authenticator.xml b/tests/unit/res/xml/authenticator.xml new file mode 100644 index 00000000000..92bf774669b --- /dev/null +++ b/tests/unit/res/xml/authenticator.xml @@ -0,0 +1,22 @@ + + + + diff --git a/tests/unit/src/com/android/settings/accounts/AccountsSettingsTest.java b/tests/unit/src/com/android/settings/accounts/AccountsSettingsTest.java new file mode 100644 index 00000000000..7218efe0a3e --- /dev/null +++ b/tests/unit/src/com/android/settings/accounts/AccountsSettingsTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.accounts; + +import static com.google.common.truth.Truth.assertThat; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.content.Context; +import android.content.Intent; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObject; +import android.support.test.uiautomator.UiSelector; +import android.support.test.uiautomator.UiScrollable; +import android.support.test.uiautomator.UiObjectNotFoundException; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class AccountsSettingsTest { + + private static final String ACCOUNTS = "Accounts"; + private static final String ACCOUNT_TYPE = "com.settingstest.account-prefs"; + private static final String PREF_TITLE = "Test preference for external account"; + + private UiDevice mDevice; + private Context mContext; + private String mTargetPackage; + + @Before + public void setUp() { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + mContext = InstrumentationRegistry.getTargetContext(); + mTargetPackage = mContext.getPackageName(); + } + + @Test + public void testExternalAccountInfoExists() throws UiObjectNotFoundException { + // add a test account + final String testAccountName = "Test Account"; + final Account account = new Account(testAccountName, ACCOUNT_TYPE); + final AccountManager accountManager = AccountManager.get(mContext); + final boolean accountAdded = + accountManager.addAccountExplicitly(account, null /* password */, null /* userdata */); + assertThat(accountAdded).isTrue(); + + // launch Accounts Settings and select the test account + launchAccountsSettings(); + mDevice.findObject(new UiSelector().text(testAccountName)).click(); + final UiObject testPreference = mDevice.findObject(new UiSelector().text(PREF_TITLE)); + // remove test account + accountManager.removeAccountExplicitly(account); + + assertThat(testPreference.exists()).isTrue(); + } + + private void launchAccountsSettings() throws UiObjectNotFoundException { + // launch settings + Intent settingsIntent = new Intent(Intent.ACTION_MAIN) + .addCategory(Intent.CATEGORY_LAUNCHER) + .setPackage(mTargetPackage) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + mContext.startActivity(settingsIntent); + // selects Accounts + final UiScrollable settings = new UiScrollable( + new UiSelector().packageName(mTargetPackage).scrollable(true)); + final String titleAccounts = ACCOUNTS; + settings.scrollTextIntoView(titleAccounts); + mDevice.findObject(new UiSelector().text(titleAccounts)).click(); + } +} diff --git a/tests/unit/src/com/android/settings/accounts/Authenticator.java b/tests/unit/src/com/android/settings/accounts/Authenticator.java new file mode 100644 index 00000000000..a2413dc86ec --- /dev/null +++ b/tests/unit/src/com/android/settings/accounts/Authenticator.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.accounts; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.AccountManager; +import android.accounts.NetworkErrorException; +import android.content.Context; +import android.os.Bundle; + +public class Authenticator extends AbstractAccountAuthenticator { + + public Authenticator(Context context) { + super(context); + } + + @Override + public Bundle editProperties(AccountAuthenticatorResponse r, String s) { + return null; + } + + @Override + public Bundle addAccount(AccountAuthenticatorResponse r, String s, String s2, String[] strings, + Bundle bundle) throws NetworkErrorException { + final Bundle result = new Bundle(); + result.putString(AccountManager.KEY_ACCOUNT_NAME, "Test Account"); + result.putString(AccountManager.KEY_ACCOUNT_TYPE, s); + return result; + } + + @Override + public Bundle confirmCredentials(AccountAuthenticatorResponse r, Account account, Bundle bundle) + throws NetworkErrorException { + return null; + } + + @Override + public Bundle getAuthToken(AccountAuthenticatorResponse r, Account account, String s, + Bundle bundle) throws NetworkErrorException { + return null; + } + + @Override + public String getAuthTokenLabel(String s) { + return s; + } + + @Override + public Bundle updateCredentials(AccountAuthenticatorResponse r, Account account, String s, + Bundle bundle) throws NetworkErrorException { + return null; + } + + @Override + public Bundle hasFeatures(AccountAuthenticatorResponse r, Account account, String[] strings) + throws NetworkErrorException { + return null; + } +} diff --git a/tests/unit/src/com/android/settings/accounts/TestAuthService.java b/tests/unit/src/com/android/settings/accounts/TestAuthService.java new file mode 100644 index 00000000000..574e9b5cf26 --- /dev/null +++ b/tests/unit/src/com/android/settings/accounts/TestAuthService.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.accounts; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +public class TestAuthService extends Service { + + private Authenticator mAuthenticator; + + @Override + public void onCreate() { + mAuthenticator = new Authenticator(this); + } + + @Override + public IBinder onBind(Intent intent) { + return mAuthenticator.getIBinder(); + } + +}