Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa27a344 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add test to verify external account settings is shown."

parents 373a33bb f2ecc4bf
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -53,6 +53,15 @@
            <meta-data android:name="com.android.settings.summary" android:resource="@string/manufacturer_settings_summary" />
            <meta-data android:name="com.android.settings.icon" android:resource="@drawable/ic_settings_applications" />
        </activity>

        <service android:name="com.android.settings.accounts.TestAuthService">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                       android:resource="@xml/authenticator" />
        </service>

    </application>

    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+3 −0
Original line number Diff line number Diff line
@@ -28,4 +28,7 @@
    <string name="manufacturer_hello" translatable="false">Hello Manufacturer!</string>
    <string name="manufacturer_settings_title" translatable="false">Manufacturer</string>
    <string name="manufacturer_settings_summary" translatable="false">Manufacturer hook that can be used to start activity of choice</string>
    <string name="account_auth_label" translatable="false">Settings Test Account</string>
    <string name="account_type" translatable="false">com.settingstest.account-prefs</string>
    <string name="account_pref_title" translatable="false">Test preference for external account</string>
</resources>
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:key="account_test_switch"
        android:title="@string/account_pref_title" />

</PreferenceScreen>
+22 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/account_auth_label"
    android:icon="@drawable/ic_settings_applications"
    android:accountType="@string/account_type"
    android:accountPreferences="@xml/account_preferences" />
+90 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
Loading