Commit a0a6538d authored by Tanmoy Mollik's avatar Tanmoy Mollik Committed by Commit Bot

[Android] Add tests for GoogleServicesSettings page

Add tests to check "Allow Chrome Sign-in" toggle behavior in
GoogleServicesSettings page. It tests two specific behavior:

1. When the user is signed in without sync consent, toggling the
preference off should sign the user out without showing the sign-out
dialog and should not wipe user data.

2. When the user is signed in with sync consent, toggling the preference
off should launch the sign out dialog to give the user an option to wipe
data.

Bug: 1138167
Change-Id: I481039755bcadd626e9bc0662b29f94b5d4298b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2475059Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Commit-Queue: Tanmoy Mollik <triploblastic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820188}
parent e1ec3380
......@@ -537,6 +537,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/sync/BookmarksTest.java",
"javatests/src/org/chromium/chrome/browser/sync/FakeProfileSyncService.java",
"javatests/src/org/chromium/chrome/browser/sync/FirstRunTest.java",
"javatests/src/org/chromium/chrome/browser/sync/GoogleServicesSettingsTest.java",
"javatests/src/org/chromium/chrome/browser/sync/ManageSyncSettingsTest.java",
"javatests/src/org/chromium/chrome/browser/sync/ManageSyncSettingsWithFakeProfileSyncServiceTest.java",
"javatests/src/org/chromium/chrome/browser/sync/OpenTabsTest.java",
......
......@@ -10,6 +10,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.DialogFragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
......@@ -54,7 +55,8 @@ public class GoogleServicesSettings
private static final String SIGN_OUT_DIALOG_TAG = "sign_out_dialog_tag";
private static final String CLEAR_DATA_PROGRESS_DIALOG_TAG = "clear_data_progress";
private static final String PREF_ALLOW_SIGNIN = "allow_signin";
@VisibleForTesting
public static final String PREF_ALLOW_SIGNIN = "allow_signin";
private static final String PREF_SEARCH_SUGGESTIONS = "search_suggestions";
private static final String PREF_NAVIGATION_ERROR = "navigation_error";
private static final String PREF_SAFE_BROWSING = "safe_browsing";
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.sync;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import androidx.annotation.Nullable;
import androidx.test.filters.LargeTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.SettingsActivityTestRule;
import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.chrome.browser.sync.settings.GoogleServicesSettings;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.signin.AccountManagerTestRule;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.signin.identitymanager.ConsentLevel;
import org.chromium.components.user_prefs.UserPrefs;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Tests for ManageSyncSettings.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class GoogleServicesSettingsTest {
@Rule
public final AccountManagerTestRule mAccountManagerTestRule = new AccountManagerTestRule();
@Rule
public final ChromeTabbedActivityTestRule mActivityTestRule =
new ChromeTabbedActivityTestRule();
@Rule
public final SettingsActivityTestRule<GoogleServicesSettings> mSettingsActivityTestRule =
new SettingsActivityTestRule<>(GoogleServicesSettings.class, true);
private @Nullable ChromeSwitchPreference mAllowChromeSignin;
@Before
public void setUp() {
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(
()
-> Assert.assertTrue("SIGNIN_ALLOWED pref should be set by default",
UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(Pref.SIGNIN_ALLOWED)));
}
@After
public void tearDown() {
TestThreadUtils.runOnUiThreadBlocking(
()
-> UserPrefs.get(Profile.getLastUsedRegularProfile())
.setBoolean(Pref.SIGNIN_ALLOWED, true));
}
@Test
@LargeTest
@Features.EnableFeatures(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
public void signOutUserWithoutShowingSignOutDialog() {
mAccountManagerTestRule.addTestAccountThenSignin();
startGoogleServicesSettings();
onView(withText(R.string.offer_chrome_signin_title)).perform(click());
TestThreadUtils.runOnUiThreadBlocking(
()
-> Assert.assertNull("Account should be signed out!",
IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile())
.getPrimaryAccountInfo(ConsentLevel.NOT_REQUIRED)));
TestThreadUtils.runOnUiThreadBlocking(
()
-> Assert.assertFalse("SIGNIN_ALLOWED pref should be unset",
UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(Pref.SIGNIN_ALLOWED)));
Assert.assertFalse("Chrome Signin should not be allowed", mAllowChromeSignin.isChecked());
}
@Test
@LargeTest
@Features.EnableFeatures(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
public void showSignOutDialogBeforeSigningUserOut() {
mAccountManagerTestRule.addTestAccountThenSigninAndEnableSync();
startGoogleServicesSettings();
onView(withText(R.string.offer_chrome_signin_title)).perform(click());
// Accept the sign out Dialog
onView(withText(R.string.continue_button)).inRoot(isDialog()).perform(click());
TestThreadUtils.runOnUiThreadBlocking(
()
-> Assert.assertFalse(
"Accepting the sign-out dialog should set SIGNIN_ALLOWED to false",
UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(Pref.SIGNIN_ALLOWED)));
Assert.assertFalse("Chrome Signin should not be allowed", mAllowChromeSignin.isChecked());
}
private void startGoogleServicesSettings() {
mSettingsActivityTestRule.startSettingsActivity();
GoogleServicesSettings googleServicesSettings = mSettingsActivityTestRule.getFragment();
mAllowChromeSignin = (ChromeSwitchPreference) googleServicesSettings.findPreference(
GoogleServicesSettings.PREF_ALLOW_SIGNIN);
Assert.assertTrue("Chrome Signin should be allowed", mAllowChromeSignin.isChecked());
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment