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
This commit is contained in:
Doris Ling
2018-02-09 14:27:06 -08:00
parent 131363884b
commit f2ecc4bf26
7 changed files with 257 additions and 0 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}