Commit eb8227aa authored by Becky Zhou's avatar Becky Zhou Committed by Commit Bot

[Feed] Update enterprise policy UI code to match infra behavior

Once FeedProcessScope has been torn down because of enterprise policy
within one session, we will not provide Feed on the NTP until the next
restart when enterprise policy is removed within this session.

Bug: 887743
Change-Id: If08e94bb23224e8c0cd2c3e361edef6ed4e0a287
Reviewed-on: https://chromium-review.googlesource.com/1239364
Commit-Queue: Becky Zhou <huayinz@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593768}
parent 81ac7266
......@@ -35,8 +35,6 @@ import org.chromium.chrome.browser.signin.SigninPromoUtil;
*/
class FeedNewTabPageMediator
implements NewTabPageLayout.ScrollDelegate, ContextMenuManager.TouchEnabledDelegate {
private static boolean sOverrideFeedEnabledForTesting;
private final FeedNewTabPage mCoordinator;
private final SnapScrollHelper mSnapScrollHelper;
private final PrefChangeRegistrar mPrefChangeRegistrar;
......@@ -87,13 +85,7 @@ class FeedNewTabPageMediator
/** Update the content based on supervised user or enterprise policy. */
private void updateContent() {
// TODO(huayinz): Replace sOverrideFeedEnabledForTesting with the real preference in test
// once we have a decision for whether to re-enable the Feed on supervised/enterprise
// account removed within a session.
if (!sOverrideFeedEnabledForTesting) {
mFeedEnabled =
PrefServiceBridge.getInstance().getBoolean(Pref.NTP_ARTICLES_SECTION_ENABLED);
}
mFeedEnabled = FeedProcessScopeFactory.isFeedProcessEnabled();
if ((mFeedEnabled && mCoordinator.getStream() != null)
|| (!mFeedEnabled && mCoordinator.getScrollViewForPolicy() != null))
return;
......@@ -328,17 +320,6 @@ class FeedNewTabPageMediator
}
}
@VisibleForTesting
void overrideFeedEnabledForTesting(boolean override) {
sOverrideFeedEnabledForTesting = override;
}
@VisibleForTesting
void updateContentForTesting(boolean feedEnabled) {
mFeedEnabled = feedEnabled;
updateContent();
}
// TODO(huayinz): Return the Model for testing in Coordinator instead once a Model is created.
@VisibleForTesting
SectionHeader getSectionHeaderForTesting() {
......
......@@ -26,6 +26,8 @@ import java.util.concurrent.Executors;
/** Holds singleton {@link FeedProcessScope} and some of the scope's host implementations. */
public class FeedProcessScopeFactory {
private static boolean sIsDisableForPolicy =
!PrefServiceBridge.getInstance().getBoolean(Pref.NTP_ARTICLES_SECTION_ENABLED);
private static PrefChangeRegistrar sPrefChangeRegistrar;
private static FeedProcessScope sFeedProcessScope;
private static FeedScheduler sFeedScheduler;
......@@ -58,11 +60,19 @@ public class FeedProcessScopeFactory {
return sFeedOfflineIndicator;
}
/**
* @return Whether the dependencies provided by this class are allowed to be created. The feed
* process is disabled if supervised user or enterprise policy has once been added
* within the current session.
*/
public static boolean isFeedProcessEnabled() {
return !sIsDisableForPolicy
&& PrefServiceBridge.getInstance().getBoolean(Pref.NTP_ARTICLES_SECTION_ENABLED);
}
private static void initialize() {
assert sFeedProcessScope == null && sFeedScheduler == null && sFeedOfflineIndicator == null;
if (!PrefServiceBridge.getInstance().getBoolean(Pref.NTP_ARTICLES_SECTION_ENABLED)) {
return;
}
if (!isFeedProcessEnabled()) return;
sPrefChangeRegistrar = new PrefChangeRegistrar();
sPrefChangeRegistrar.addObserver(Pref.NTP_ARTICLES_SECTION_ENABLED,
......@@ -151,6 +161,7 @@ public class FeedProcessScopeFactory {
// Should only be subscribed while it was enabled. A change should mean articles are now
// disabled.
assert !PrefServiceBridge.getInstance().getBoolean(Pref.NTP_ARTICLES_SECTION_ENABLED);
sIsDisableForPolicy = true;
destroy();
}
......
......@@ -77,7 +77,7 @@ public class FeedNewTabPageTest {
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityWithURL("about:blank");
FeedNewTabPage.setInTestMode(true);
ThreadUtils.runOnUiThreadBlocking(() -> FeedNewTabPage.setInTestMode(true));
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mSiteSuggestions = NewTabPageTestUtils.createFakeSiteSuggestions(mTestServer);
......@@ -198,11 +198,9 @@ public class FeedNewTabPageTest {
@MediumTest
@Feature({"FeedNewTabPage"})
public void testFeedDisabledByPolicy() throws Exception {
// TODO(huayinz): Re-enable the part commented out on this test and replace
// sOverrideFeedEnabledForTesting with the real preference once we have a decision for
// whether to re-enable the Feed on supervised/enterprise account removed within a session.
FeedNewTabPageMediator mediator = mNtp.getMediatorForTesting();
mediator.overrideFeedEnabledForTesting(true);
final boolean pref = ThreadUtils.runOnUiThreadBlocking(
() -> PrefServiceBridge.getInstance().getBoolean(
Pref.NTP_ARTICLES_SECTION_ENABLED));
// Policy is disabled. Verify the NTP root view contains only the Stream view as child.
ViewGroup rootView = (ViewGroup) mNtp.getView();
......@@ -214,7 +212,8 @@ public class FeedNewTabPageTest {
// Simulate that policy is enabled. Verify the NTP root view contains only the view for
// policy as child.
ThreadUtils.runOnUiThreadBlocking(() -> mediator.updateContentForTesting(false));
ThreadUtils.runOnUiThreadBlocking(() -> PrefServiceBridge.getInstance().setBoolean(
Pref.NTP_ARTICLES_SECTION_ENABLED, false));
ViewUtils.waitForStableView(rootView);
Assert.assertNotNull(mNtp.getScrollViewForPolicy());
Assert.assertNull(mNtp.getStream());
......@@ -233,28 +232,27 @@ public class FeedNewTabPageTest {
Assert.assertEquals(1, rootView2.getChildCount());
Assert.assertEquals(ntp2.getScrollViewForPolicy(), rootView2.getChildAt(0));
/*
// Simulate that policy is disabled. Verify the NTP root view contains only the Stream view
// as child.
// Simulate that policy is disabled. Verify the NTP root view is the view for policy. We
// don't re-enable the Feed until the next restart.
ThreadUtils.runOnUiThreadBlocking(() -> PrefServiceBridge.getInstance().setBoolean(
Pref.NTP_ARTICLES_SECTION_ENABLED, true));
ViewUtils.waitForStableView(rootView2);
Assert.assertNotNull(ntp2.getStream());
Assert.assertNull(ntp2.getScrollViewForPolicy());
Assert.assertNotNull(ntp2.getScrollViewForPolicy());
Assert.assertNull(ntp2.getStream());
Assert.assertEquals(1, rootView2.getChildCount());
Assert.assertEquals(ntp2.getStream().getView(), rootView2.getChildAt(0));
Assert.assertEquals(ntp2.getScrollViewForPolicy(), rootView2.getChildAt(0));
// Switch to the old tab. Verify the NTP root view contains only the Stream view as child.
// Switch to the old tab. Verify the NTP root view is the view for policy.
ChromeTabUtils.switchTabInCurrentTabModel(mActivityTestRule.getActivity(), mTab.getId());
ViewUtils.waitForStableView(rootView);
Assert.assertNotNull(mNtp.getStream());
Assert.assertNull(mNtp.getScrollViewForPolicy());
Assert.assertNotNull(mNtp.getScrollViewForPolicy());
Assert.assertNull(mNtp.getStream());
Assert.assertEquals(1, rootView.getChildCount());
Assert.assertEquals(mNtp.getStream().getView(), rootView.getChildAt(0));
*/
Assert.assertEquals(mNtp.getScrollViewForPolicy(), rootView.getChildAt(0));
// Reset state.
mediator.overrideFeedEnabledForTesting(false);
ThreadUtils.runOnUiThreadBlocking(() -> PrefServiceBridge.getInstance().setBoolean(
Pref.NTP_ARTICLES_SECTION_ENABLED, pref));
}
private boolean getPreferenceForArticleSectionHeader() throws Exception {
......
......@@ -136,7 +136,6 @@ public class NewTabPageTest {
mInterestFeedEnabled = interestFeedEnabled;
if (mInterestFeedEnabled) {
Features.getInstance().enable(ChromeFeatureList.INTEREST_FEED_CONTENT_SUGGESTIONS);
FeedNewTabPage.setInTestMode(true);
} else {
Features.getInstance().disable(ChromeFeatureList.INTEREST_FEED_CONTENT_SUGGESTIONS);
}
......@@ -144,6 +143,11 @@ public class NewTabPageTest {
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityWithURL("about:blank");
if (mInterestFeedEnabled) {
ThreadUtils.runOnUiThreadBlocking(() -> FeedNewTabPage.setInTestMode(true));
}
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mSiteSuggestions = NewTabPageTestUtils.createFakeSiteSuggestions(mTestServer);
......@@ -151,7 +155,7 @@ public class NewTabPageTest {
mMostVisitedSites.setTileSuggestions(mSiteSuggestions);
mSuggestionsDeps.getFactory().mostVisitedSites = mMostVisitedSites;
mActivityTestRule.startMainActivityWithURL(UrlConstants.NTP_URL);
mActivityTestRule.loadUrl(UrlConstants.NTP_URL);
mTab = mActivityTestRule.getActivity().getActivityTab();
NewTabPageTestUtils.waitForNtpLoaded(mTab);
......@@ -165,7 +169,9 @@ public class NewTabPageTest {
@After
public void tearDown() throws Exception {
mTestServer.stopAndDestroyServer();
if (mInterestFeedEnabled) FeedNewTabPage.setInTestMode(false);
if (mInterestFeedEnabled) {
ThreadUtils.runOnUiThreadBlocking(() -> FeedNewTabPage.setInTestMode(false));
}
}
@Test
......
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