Commit 87a81f0a authored by Pavel Yatsuk's avatar Pavel Yatsuk Committed by Commit Bot

[GID] Implement tests for IdentityDiscController

The test verifies Identity Disc visibility based on navigation location
(NTP vs. other locations), sign-in, and sync state.

To enable testing I updated the logic to retrieve ProfileSyncService
reference instead of using cached reference. I also updated
FakeProfileSyncService to allow controlling sync state from test.

R=twellington@chromium.org

Change-Id: I8fcc672acd0b0a5e7c46e951e4222e7c9cbb8c7b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1904080Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Pavel Yatsuk <pavely@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714816}
parent 14c0359b
...@@ -493,6 +493,7 @@ chrome_test_java_sources = [ ...@@ -493,6 +493,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/tasks/ReturnToChromeTest.java", "javatests/src/org/chromium/chrome/browser/tasks/ReturnToChromeTest.java",
"javatests/src/org/chromium/chrome/browser/test/CommandLineInitRule.java", "javatests/src/org/chromium/chrome/browser/test/CommandLineInitRule.java",
"javatests/src/org/chromium/chrome/browser/test/ScreenShooter.java", "javatests/src/org/chromium/chrome/browser/test/ScreenShooter.java",
"javatests/src/org/chromium/chrome/browser/toolbar/IdentityDiscControllerTest.java",
"javatests/src/org/chromium/chrome/browser/toolbar/LocationBarModelTest.java", "javatests/src/org/chromium/chrome/browser/toolbar/LocationBarModelTest.java",
"javatests/src/org/chromium/chrome/browser/toolbar/ToolbarProgressBarIntegrationTest.java", "javatests/src/org/chromium/chrome/browser/toolbar/ToolbarProgressBarIntegrationTest.java",
"javatests/src/org/chromium/chrome/browser/toolbar/ToolbarProgressBarTest.java", "javatests/src/org/chromium/chrome/browser/toolbar/ToolbarProgressBarTest.java",
......
...@@ -125,8 +125,8 @@ class IdentityDiscController implements NativeInitObserver, ProfileDataCache.Obs ...@@ -125,8 +125,8 @@ class IdentityDiscController implements NativeInitObserver, ProfileDataCache.Obs
mIsNTPVisible = isNTPVisible; mIsNTPVisible = isNTPVisible;
String accountName = ChromeSigninController.get().getSignedInAccountName(); String accountName = ChromeSigninController.get().getSignedInAccountName();
boolean shouldShowIdentityDisc = boolean shouldShowIdentityDisc = isNTPVisible && accountName != null
isNTPVisible && accountName != null && mProfileSyncService.canSyncFeatureStart(); && ProfileSyncService.get().canSyncFeatureStart();
@IdentityDiscState @IdentityDiscState
int oldState = mState; int oldState = mState;
...@@ -225,18 +225,18 @@ class IdentityDiscController implements NativeInitObserver, ProfileDataCache.Obs ...@@ -225,18 +225,18 @@ class IdentityDiscController implements NativeInitObserver, ProfileDataCache.Obs
@Override @Override
public void onSignedIn() { public void onSignedIn() {
resetIdentityDisc(); resetIdentityDisc();
updateButtonState(mIsNTPVisible); updateButtonState();
} }
@Override @Override
public void onSignedOut() { public void onSignedOut() {
updateButtonState(mIsNTPVisible); updateButtonState();
} }
// ProfileSyncService.SyncStateChangedListener implementation. // ProfileSyncService.SyncStateChangedListener implementation.
@Override @Override
public void syncStateChanged() { public void syncStateChanged() {
updateButtonState(mIsNTPVisible); updateButtonState();
} }
/** /**
......
...@@ -17,6 +17,7 @@ public class FakeProfileSyncService extends ProfileSyncService { ...@@ -17,6 +17,7 @@ public class FakeProfileSyncService extends ProfileSyncService {
private int mNumberOfSyncedDevices; private int mNumberOfSyncedDevices;
private boolean mPassphraseRequiredForPreferredDataTypes; private boolean mPassphraseRequiredForPreferredDataTypes;
private Set<Integer> mChosenTypes = new HashSet<>(); private Set<Integer> mChosenTypes = new HashSet<>();
private boolean mCanSyncFeatureStart;
public FakeProfileSyncService() { public FakeProfileSyncService() {
super(); super();
...@@ -64,4 +65,13 @@ public class FakeProfileSyncService extends ProfileSyncService { ...@@ -64,4 +65,13 @@ public class FakeProfileSyncService extends ProfileSyncService {
boolean passphraseRequiredForPreferredDataTypes) { boolean passphraseRequiredForPreferredDataTypes) {
mPassphraseRequiredForPreferredDataTypes = passphraseRequiredForPreferredDataTypes; mPassphraseRequiredForPreferredDataTypes = passphraseRequiredForPreferredDataTypes;
} }
@Override
public boolean canSyncFeatureStart() {
return mCanSyncFeatureStart;
}
public void setCanSyncFeatureStart(boolean canSyncFeatureStart) {
mCanSyncFeatureStart = canSyncFeatureStart;
}
} }
// Copyright 2019 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.toolbar;
import android.support.test.filters.MediumTest;
import android.view.View;
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.ChromeActivity;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.sync.FakeProfileSyncService;
import org.chromium.chrome.browser.sync.ProfileSyncService;
import org.chromium.chrome.browser.util.UrlConstants;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.signin.SigninTestUtil;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Instrumentation test for Identity Disc.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class IdentityDiscControllerTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
// IdentityDiscController subscribes for sync state change events right after native
// initialization. Currently there is no facility to reliably substitute ProfileSyncService
// for fake one from test before IdentityDiscController gets a reference to it.
// We keep reference to FakeProfileSyncService to affect logic based on sync state. Reference to
// original ProfileSyncService is used for triggering sync state change notifications.
private FakeProfileSyncService mFakeProfileSyncService;
private ProfileSyncService mOriginalProfileSyncService;
@Before
public void setUp() {
SigninTestUtil.setUpAuthForTest();
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
mOriginalProfileSyncService = ProfileSyncService.get();
mFakeProfileSyncService = new FakeProfileSyncService();
ProfileSyncService.overrideForTests(mFakeProfileSyncService);
});
}
@After
public void tearDown() {
SigninTestUtil.tearDownAuthForTest();
}
@Test
@MediumTest
public void testIdentityDiscWithNavigation() {
// User is signed in and syncing.
SigninTestUtil.addAndSignInTestAccount();
mFakeProfileSyncService.setCanSyncFeatureStart(true);
// Identity Disc should be visible on NTP.
mActivityTestRule.startMainActivityWithURL(UrlConstants.NTP_URL);
View experimentalButton =
mActivityTestRule.getActivity().findViewById(R.id.experimental_toolbar_button);
Assert.assertNotNull("IdentityDisc is not inflated", experimentalButton);
Assert.assertEquals(
"IdentityDisc is not visible", View.VISIBLE, experimentalButton.getVisibility());
// Identity Disc should be hidden on navigation away from NTP.
mActivityTestRule.loadUrl(UrlConstants.CHROME_BLANK_URL);
Assert.assertEquals("IdentityDisc is still visible outside of NTP", View.GONE,
experimentalButton.getVisibility());
}
@Test
@MediumTest
public void testIdentityDiscWithSyncState() {
SigninTestUtil.addAndSignInTestAccount();
mFakeProfileSyncService.setCanSyncFeatureStart(false);
// When sync is disabled Identity Disc should not be visible.
mActivityTestRule.startMainActivityWithURL(UrlConstants.NTP_URL);
View experimentalButton =
mActivityTestRule.getActivity().findViewById(R.id.experimental_toolbar_button);
Assert.assertNull("IdentityDisc is visible when sync is disabled", experimentalButton);
// Identity Disc should be shown on sync state change without NTP refresh.
mFakeProfileSyncService.setCanSyncFeatureStart(true);
triggerSyncStateChange();
experimentalButton =
mActivityTestRule.getActivity().findViewById(R.id.experimental_toolbar_button);
Assert.assertNotNull("IdentityDisc is not inflated", experimentalButton);
Assert.assertEquals(
"IdentityDisc is not visible", View.VISIBLE, experimentalButton.getVisibility());
}
@Test
@MediumTest
public void testIdentityDiscWithSignInState() {
// When user is signed out, Identity Disc should not be visible.
mFakeProfileSyncService.setCanSyncFeatureStart(true);
mActivityTestRule.startMainActivityWithURL(UrlConstants.NTP_URL);
View experimentalButton =
mActivityTestRule.getActivity().findViewById(R.id.experimental_toolbar_button);
Assert.assertNull("IdentityDisc is visible for signed out user", experimentalButton);
// Identity Disc should be shown on sign-in state change without NTP refresh.
SigninTestUtil.addAndSignInTestAccount();
triggerSyncStateChange();
experimentalButton =
mActivityTestRule.getActivity().findViewById(R.id.experimental_toolbar_button);
Assert.assertNotNull("IdentityDisc is not inflated", experimentalButton);
Assert.assertEquals(
"IdentityDisc is not visible", View.VISIBLE, experimentalButton.getVisibility());
}
private void triggerSyncStateChange() {
TestThreadUtils.runOnUiThreadBlocking(
() -> { mOriginalProfileSyncService.syncStateChanged(); });
}
}
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