Commit 497ef2a5 authored by Alice Wang's avatar Alice Wang Committed by Commit Bot

[Android][Test] Enable flaky NTP test with FakeAccountManagerFacade

This CL fixes one flaky NTP test with the new FakeAccountManagerFacade
implementation and removes unused code in AccountManagerTestRule.

Bug: 996716
Change-Id: Ifb6662bcc572ad8dab959d6b8aa18d4dae8578c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2220050Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarAlex Ilin <alexilin@chromium.org>
Commit-Queue: Alice Wang <aliceywang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774564}
parent 4549e461
...@@ -51,7 +51,6 @@ import org.chromium.base.test.params.ParameterizedRunner; ...@@ -51,7 +51,6 @@ import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.FlakyTest;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches; import org.chromium.chrome.browser.flags.ChromeSwitches;
...@@ -141,7 +140,15 @@ public class FeedNewTabPageTest { ...@@ -141,7 +140,15 @@ public class FeedNewTabPageTest {
mMostVisitedSites = new FakeMostVisitedSites(); mMostVisitedSites = new FakeMostVisitedSites();
mMostVisitedSites.setTileSuggestions(mSiteSuggestions); mMostVisitedSites.setTileSuggestions(mSiteSuggestions);
mSuggestionsDeps.getFactory().mostVisitedSites = mMostVisitedSites; mSuggestionsDeps.getFactory().mostVisitedSites = mMostVisitedSites;
}
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
FeedProcessScopeFactory.setTestNetworkClient(null);
}
private void openNewTabPage() {
mActivityTestRule.loadUrl(UrlConstants.NTP_URL); mActivityTestRule.loadUrl(UrlConstants.NTP_URL);
mTab = mActivityTestRule.getActivity().getActivityTab(); mTab = mActivityTestRule.getActivity().getActivityTab();
NewTabPageTestUtils.waitForNtpLoaded(mTab); NewTabPageTestUtils.waitForNtpLoaded(mTab);
...@@ -152,12 +159,6 @@ public class FeedNewTabPageTest { ...@@ -152,12 +159,6 @@ public class FeedNewTabPageTest {
Assert.assertEquals(mSiteSuggestions.size(), mTileGridLayout.getChildCount()); Assert.assertEquals(mSiteSuggestions.size(), mTileGridLayout.getChildCount());
} }
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
FeedProcessScopeFactory.setTestNetworkClient(null);
}
private void waitForPopup(Matcher<View> matcher) { private void waitForPopup(Matcher<View> matcher) {
View mainDecorView = mActivityTestRule.getActivity().getWindow().getDecorView(); View mainDecorView = mActivityTestRule.getActivity().getWindow().getDecorView();
onView(isRoot()) onView(isRoot())
...@@ -169,6 +170,7 @@ public class FeedNewTabPageTest { ...@@ -169,6 +170,7 @@ public class FeedNewTabPageTest {
@MediumTest @MediumTest
@Feature({"FeedNewTabPage"}) @Feature({"FeedNewTabPage"})
public void testSignInPromo() { public void testSignInPromo() {
openNewTabPage();
SignInPromo.SigninObserver signinObserver = mNtp.getCoordinatorForTesting() SignInPromo.SigninObserver signinObserver = mNtp.getCoordinatorForTesting()
.getMediatorForTesting() .getMediatorForTesting()
.getSignInPromoForTesting() .getSignInPromoForTesting()
...@@ -220,6 +222,7 @@ public class FeedNewTabPageTest { ...@@ -220,6 +222,7 @@ public class FeedNewTabPageTest {
@Feature({"FeedNewTabPage"}) @Feature({"FeedNewTabPage"})
@DisabledTest(message = "https://crbug.com/1046822") @DisabledTest(message = "https://crbug.com/1046822")
public void testSignInPromo_DismissBySwipe() { public void testSignInPromo_DismissBySwipe() {
openNewTabPage();
boolean dismissed = SharedPreferencesManager.getInstance().readBoolean( boolean dismissed = SharedPreferencesManager.getInstance().readBoolean(
ChromePreferenceKeys.SIGNIN_PROMO_NTP_PROMO_DISMISSED, false); ChromePreferenceKeys.SIGNIN_PROMO_NTP_PROMO_DISMISSED, false);
if (dismissed) { if (dismissed) {
...@@ -255,18 +258,17 @@ public class FeedNewTabPageTest { ...@@ -255,18 +258,17 @@ public class FeedNewTabPageTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"FeedNewTabPage"}) @Feature({"FeedNewTabPage"})
@FlakyTest(message = "https://crbug.com/996716")
@AccountManagerTestRule.BlockGetAccounts
public void testSignInPromo_AccountsNotReady() { public void testSignInPromo_AccountsNotReady() {
mAccountManagerTestRule.setIsCachePopulated(false);
openNewTabPage();
// Check that the sign-in promo is not shown if accounts are not ready. // Check that the sign-in promo is not shown if accounts are not ready.
onView(instanceOf(RecyclerView.class)) onView(instanceOf(RecyclerView.class))
.perform(RecyclerViewActions.scrollToPosition(SIGNIN_PROMO_POSITION)); .perform(RecyclerViewActions.scrollToPosition(SIGNIN_PROMO_POSITION));
onView(withId(R.id.signin_promo_view_container)).check(doesNotExist()); onView(withId(R.id.signin_promo_view_container)).check(doesNotExist());
// Wait for accounts cache population to finish and reload ntp. mAccountManagerTestRule.setIsCachePopulated(true);
mAccountManagerTestRule.unblockGetAccountsAndWaitForAccountsPopulated(); TestThreadUtils.runOnUiThreadBlocking(mTab::reload);
TestThreadUtils.runOnUiThreadBlocking(() -> mTab.reload()); ChromeTabUtils.waitForTabPageLoaded(mTab, mTab.getUrlString());
NewTabPageTestUtils.waitForNtpLoaded(mTab);
// Check that the sign-in promo is displayed this time. // Check that the sign-in promo is displayed this time.
onView(instanceOf(RecyclerView.class)) onView(instanceOf(RecyclerView.class))
...@@ -280,6 +282,7 @@ public class FeedNewTabPageTest { ...@@ -280,6 +282,7 @@ public class FeedNewTabPageTest {
@Feature({"NewTabPage", "FeedNewTabPage"}) @Feature({"NewTabPage", "FeedNewTabPage"})
@ParameterAnnotations.UseMethodParameter(SigninPromoParams.class) @ParameterAnnotations.UseMethodParameter(SigninPromoParams.class)
public void testArticleSectionHeaderWithMenu(boolean disableSigninPromoCard) throws Exception { public void testArticleSectionHeaderWithMenu(boolean disableSigninPromoCard) throws Exception {
openNewTabPage();
// Scroll to the article section header in case it is not visible. // Scroll to the article section header in case it is not visible.
onView(instanceOf(RecyclerView.class)) onView(instanceOf(RecyclerView.class))
.perform(RecyclerViewActions.scrollToPosition(ARTICLE_SECTION_HEADER_POSITION)); .perform(RecyclerViewActions.scrollToPosition(ARTICLE_SECTION_HEADER_POSITION));
...@@ -318,6 +321,7 @@ public class FeedNewTabPageTest { ...@@ -318,6 +321,7 @@ public class FeedNewTabPageTest {
@Feature({"FeedNewTabPage"}) @Feature({"FeedNewTabPage"})
@DisabledTest(message = "https://crbug.com/914068") @DisabledTest(message = "https://crbug.com/914068")
public void testArticleSectionHeader() throws Exception { public void testArticleSectionHeader() throws Exception {
openNewTabPage();
final int expectedCountWhenCollapsed = 2; final int expectedCountWhenCollapsed = 2;
final int expectedCountWhenExpanded = 4; // 3 header views and the empty view. final int expectedCountWhenExpanded = 4; // 3 header views and the empty view.
...@@ -383,6 +387,7 @@ public class FeedNewTabPageTest { ...@@ -383,6 +387,7 @@ public class FeedNewTabPageTest {
@Feature({"FeedNewTabPage"}) @Feature({"FeedNewTabPage"})
@DisabledTest(message = "crbug.com/1064388") @DisabledTest(message = "crbug.com/1064388")
public void testFeedDisabledByPolicy() throws Exception { public void testFeedDisabledByPolicy() throws Exception {
openNewTabPage();
final boolean pref = TestThreadUtils.runOnUiThreadBlocking( final boolean pref = TestThreadUtils.runOnUiThreadBlocking(
() ()
-> PrefServiceBridge.getInstance().getBoolean( -> PrefServiceBridge.getInstance().getBoolean(
......
...@@ -21,6 +21,7 @@ import org.chromium.chrome.browser.signin.SigninManager.SignInAllowedObserver; ...@@ -21,6 +21,7 @@ import org.chromium.chrome.browser.signin.SigninManager.SignInAllowedObserver;
import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver;
import org.chromium.chrome.browser.signin.SigninPreferencesManager; import org.chromium.chrome.browser.signin.SigninPreferencesManager;
import org.chromium.chrome.browser.signin.SigninPromoController; import org.chromium.chrome.browser.signin.SigninPromoController;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider; import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountsChangeObserver; import org.chromium.components.signin.AccountsChangeObserver;
import org.chromium.components.signin.metrics.SigninAccessPoint; import org.chromium.components.signin.metrics.SigninAccessPoint;
...@@ -188,17 +189,20 @@ public abstract class SignInPromo extends OptionalLeaf { ...@@ -188,17 +189,20 @@ public abstract class SignInPromo extends OptionalLeaf {
public class SigninObserver implements SignInStateObserver, SignInAllowedObserver, public class SigninObserver implements SignInStateObserver, SignInAllowedObserver,
ProfileDataCache.Observer, AccountsChangeObserver { ProfileDataCache.Observer, AccountsChangeObserver {
private final SigninManager mSigninManager; private final SigninManager mSigninManager;
private final AccountManagerFacade mAccountManagerFacade;
/** Guards {@link #unregister()}, which can be called multiple times. */ /** Guards {@link #unregister()}, which can be called multiple times. */
private boolean mUnregistered; private boolean mUnregistered;
private SigninObserver(SigninManager signinManager) { private SigninObserver(SigninManager signinManager) {
mSigninManager = signinManager; mSigninManager = signinManager;
mAccountManagerFacade = AccountManagerFacadeProvider.getInstance();
mSigninManager.addSignInAllowedObserver(this); mSigninManager.addSignInAllowedObserver(this);
mSigninManager.addSignInStateObserver(this); mSigninManager.addSignInStateObserver(this);
mProfileDataCache.addObserver(this); mProfileDataCache.addObserver(this);
AccountManagerFacadeProvider.getInstance().addObserver(this); mAccountManagerFacade.addObserver(this);
} }
private void unregister() { private void unregister() {
...@@ -208,7 +212,7 @@ public abstract class SignInPromo extends OptionalLeaf { ...@@ -208,7 +212,7 @@ public abstract class SignInPromo extends OptionalLeaf {
mSigninManager.removeSignInAllowedObserver(this); mSigninManager.removeSignInAllowedObserver(this);
mSigninManager.removeSignInStateObserver(this); mSigninManager.removeSignInStateObserver(this);
mProfileDataCache.removeObserver(this); mProfileDataCache.removeObserver(this);
AccountManagerFacadeProvider.getInstance().removeObserver(this); mAccountManagerFacade.removeObserver(this);
} }
// SignInAllowedObserver implementation. // SignInAllowedObserver implementation.
...@@ -237,7 +241,7 @@ public abstract class SignInPromo extends OptionalLeaf { ...@@ -237,7 +241,7 @@ public abstract class SignInPromo extends OptionalLeaf {
// AccountsChangeObserver implementation. // AccountsChangeObserver implementation.
@Override @Override
public void onAccountsChanged() { public void onAccountsChanged() {
mAccountsReady = AccountManagerFacadeProvider.getInstance().isCachePopulated(); mAccountsReady = mAccountManagerFacade.isCachePopulated();
// We don't change the visibility here to avoid the promo popping up in the feed // We don't change the visibility here to avoid the promo popping up in the feed
// unexpectedly. If accounts are ready, the promo will be shown up on the next reload. // unexpectedly. If accounts are ready, the promo will be shown up on the next reload.
notifyDataChanged(); notifyDataChanged();
......
...@@ -16,28 +16,12 @@ import org.chromium.components.signin.AccountManagerFacadeProvider; ...@@ -16,28 +16,12 @@ import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountUtils; import org.chromium.components.signin.AccountUtils;
import org.chromium.components.signin.ProfileDataSource; import org.chromium.components.signin.ProfileDataSource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* JUnit4 rule for overriding behaviour of {@link AccountManagerFacade} for tests. * JUnit4 rule for overriding behaviour of {@link AccountManagerFacade} for tests.
*/ */
public class AccountManagerTestRule implements TestRule { public class AccountManagerTestRule implements TestRule {
private final FakeAccountManagerFacade mFakeAccountManagerFacade; private final FakeAccountManagerFacade mFakeAccountManagerFacade;
/**
* Test method annotation signaling that the account population should be blocked until {@link
* #unblockGetAccountsAndWaitForAccountsPopulated()} is called.
*
* TODO(https://crbug.com/1078342) Cleanup this interface
* We should be able to mock isCachePopulated to simulate signin promo shown status.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BlockGetAccounts {}
public AccountManagerTestRule(@Nullable FakeProfileDataSource fakeProfileDataSource) { public AccountManagerTestRule(@Nullable FakeProfileDataSource fakeProfileDataSource) {
mFakeAccountManagerFacade = new FakeAccountManagerFacade(fakeProfileDataSource); mFakeAccountManagerFacade = new FakeAccountManagerFacade(fakeProfileDataSource);
} }
...@@ -62,13 +46,10 @@ public class AccountManagerTestRule implements TestRule { ...@@ -62,13 +46,10 @@ public class AccountManagerTestRule implements TestRule {
} }
/** /**
* Unblocks all get accounts requests to {@link AccountManagerFacade}. * Sets the boolean for whether the account cache has already been populated.
* Has no effect in tests that are not annotated with {@link BlockGetAccounts}.
*
* TODO(https://crbug.com/1078342) Cleanup this method.
*/ */
public void unblockGetAccountsAndWaitForAccountsPopulated() { public void setIsCachePopulated(boolean isCachePopulated) {
AccountManagerFacadeProvider.getInstance().tryGetGoogleAccounts(); mFakeAccountManagerFacade.setIsCachePopulated(isCachePopulated);
} }
/** /**
......
...@@ -33,6 +33,9 @@ public class FakeAccountManagerFacade implements AccountManagerFacade { ...@@ -33,6 +33,9 @@ public class FakeAccountManagerFacade implements AccountManagerFacade {
@GuardedBy("mLock") @GuardedBy("mLock")
private final Set<AccountHolder> mAccountHolders = new LinkedHashSet<>(); private final Set<AccountHolder> mAccountHolders = new LinkedHashSet<>();
@GuardedBy("mLock")
private boolean mIsCachePopulated = true;
private final @Nullable FakeProfileDataSource mFakeProfileDataSource; private final @Nullable FakeProfileDataSource mFakeProfileDataSource;
/** /**
...@@ -63,7 +66,9 @@ public class FakeAccountManagerFacade implements AccountManagerFacade { ...@@ -63,7 +66,9 @@ public class FakeAccountManagerFacade implements AccountManagerFacade {
@Override @Override
public boolean isCachePopulated() { public boolean isCachePopulated() {
return true; synchronized (mLock) {
return mIsCachePopulated;
}
} }
@Override @Override
...@@ -113,6 +118,15 @@ public class FakeAccountManagerFacade implements AccountManagerFacade { ...@@ -113,6 +118,15 @@ public class FakeAccountManagerFacade implements AccountManagerFacade {
return true; return true;
} }
/**
* Sets the boolean for whether the account cache has already been populated.
*/
void setIsCachePopulated(boolean isCachePopulated) {
synchronized (mLock) {
mIsCachePopulated = isCachePopulated;
}
}
void addAccount(Account account) { void addAccount(Account account) {
AccountHolder accountHolder = AccountHolder.builder(account).alwaysAccept(true).build(); AccountHolder accountHolder = AccountHolder.builder(account).alwaysAccept(true).build();
// As this class is accessed both from UI thread and worker threads, we lock the access // As this class is accessed both from UI thread and worker threads, we lock the access
......
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