forked from chromium/chromium
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added controller for reader mode CPA
This CL adds a button controller for reader mode as a contextual page actions. Bug: 1373898 Change-Id: I35aaa9fa719d333a7dcdad303211ef0d4b8a409b Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3940208 Reviewed-by: Tommy Nyquist <[email protected]> Reviewed-by: Shakti Sahu <[email protected]> Commit-Queue: Salvador Guerrero Ramos <[email protected]> Cr-Commit-Position: refs/heads/main@{#1059371}
- Loading branch information
Salvador Guerrero
authored and
Chromium LUCI CQ
committed
Oct 14, 2022
1 parent
c4275b4
commit acad206
Showing
14 changed files
with
318 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...java/src/org/chromium/chrome/browser/dom_distiller/ReaderModeToolbarButtonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// 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.dom_distiller; | ||
|
||
import android.graphics.drawable.Drawable; | ||
import android.view.View; | ||
|
||
import org.chromium.base.supplier.Supplier; | ||
import org.chromium.chrome.R; | ||
import org.chromium.chrome.browser.tab.Tab; | ||
import org.chromium.chrome.browser.toolbar.BaseButtonDataProvider; | ||
import org.chromium.chrome.browser.toolbar.adaptive.AdaptiveToolbarFeatures.AdaptiveToolbarButtonVariant; | ||
import org.chromium.chrome.browser.user_education.IPHCommandBuilder; | ||
import org.chromium.components.feature_engagement.FeatureConstants; | ||
import org.chromium.ui.modaldialog.ModalDialogManager; | ||
|
||
/** | ||
* Responsible for providing UI resources for showing a reader mode button on toolbar. | ||
*/ | ||
public class ReaderModeToolbarButtonController extends BaseButtonDataProvider { | ||
/** | ||
* Creates a new instance of {@code ReaderModeToolbarButtonController}. | ||
* | ||
* @param activeTabSupplier Supplier for the current active tab. | ||
* @param modalDialogManager Modal dialog manager, used to disable the button when a dialog is | ||
* visible. Can be null to disable this behavior. | ||
* @param buttonDrawable Drawable for the button icon. | ||
*/ | ||
public ReaderModeToolbarButtonController(Supplier<Tab> activeTabSupplier, | ||
ModalDialogManager modalDialogManager, Drawable buttonDrawable) { | ||
super(activeTabSupplier, modalDialogManager, buttonDrawable, R.string.reader_view_text_alt, | ||
/* contentDescriptionResId= */ R.string.reader_mode_message_title, | ||
/* supportsTinting= */ true, /* iphCommandBuilder= */ null, | ||
AdaptiveToolbarButtonVariant.READER_MODE); | ||
} | ||
|
||
@Override | ||
public void onClick(View view) { | ||
Tab currentTab = mActiveTabSupplier.get(); | ||
if (currentTab == null) return; | ||
|
||
ReaderModeManager readerModeManager = | ||
currentTab.getUserDataHost().getUserData(ReaderModeManager.class); | ||
if (readerModeManager == null) return; | ||
|
||
readerModeManager.activateReaderMode(); | ||
} | ||
|
||
@Override | ||
protected IPHCommandBuilder getIphCommandBuilder(Tab tab) { | ||
IPHCommandBuilder iphCommandBuilder = new IPHCommandBuilder(tab.getContext().getResources(), | ||
FeatureConstants.CONTEXTUAL_PAGE_ACTIONS_QUIET_VARIANT, | ||
/* stringId = */ R.string.reader_mode_message_title, | ||
/* accessibilityStringId = */ R.string.reader_view_text_alt); | ||
return iphCommandBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
.../src/org/chromium/chrome/browser/dom_distiller/ReaderModeToolbarButtonControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// 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.dom_distiller; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.graphics.drawable.Drawable; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import org.chromium.base.FeatureList; | ||
import org.chromium.base.FeatureList.TestValues; | ||
import org.chromium.base.UserDataHost; | ||
import org.chromium.base.supplier.Supplier; | ||
import org.chromium.base.test.BaseRobolectricTestRunner; | ||
import org.chromium.chrome.browser.flags.ChromeFeatureList; | ||
import org.chromium.chrome.browser.tab.Tab; | ||
import org.chromium.chrome.browser.toolbar.ButtonData; | ||
import org.chromium.ui.modaldialog.ModalDialogManager; | ||
|
||
/** This class tests the behavior of the {@link ReaderModeToolbarButtonController}. */ | ||
@RunWith(BaseRobolectricTestRunner.class) | ||
public class ReaderModeToolbarButtonControllerTest { | ||
@Mock | ||
private Tab mMockTab; | ||
@Mock | ||
private ReaderModeManager mMockReaderModeManager; | ||
@Mock | ||
private Supplier<Tab> mMockTabSupplier; | ||
@Mock | ||
private ModalDialogManager mMockModalDialogManager; | ||
private UserDataHost mUserDataHost; | ||
private TestValues mTestValues; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
mUserDataHost = new UserDataHost(); | ||
|
||
Context mockContext = mock(Context.class); | ||
Resources mockResources = mock(Resources.class); | ||
|
||
doReturn(mockResources).when(mockContext).getResources(); | ||
doReturn(mockContext).when(mMockTab).getContext(); | ||
doReturn(mMockTab).when(mMockTabSupplier).get(); | ||
doReturn(mUserDataHost).when(mMockTab).getUserDataHost(); | ||
mUserDataHost.setUserData(ReaderModeManager.USER_DATA_KEY, mMockReaderModeManager); | ||
|
||
mTestValues = new TestValues(); | ||
mTestValues.addFeatureFlagOverride(ChromeFeatureList.CONTEXTUAL_PAGE_ACTIONS, true); | ||
mTestValues.addFeatureFlagOverride( | ||
ChromeFeatureList.CONTEXTUAL_PAGE_ACTION_READER_MODE, true); | ||
mTestValues.addFeatureFlagOverride( | ||
ChromeFeatureList.ADAPTIVE_BUTTON_IN_TOP_TOOLBAR_CUSTOMIZATION_V2, true); | ||
|
||
FeatureList.setTestValues(mTestValues); | ||
} | ||
|
||
private ReaderModeToolbarButtonController createController() { | ||
return new ReaderModeToolbarButtonController( | ||
mMockTabSupplier, mMockModalDialogManager, mock(Drawable.class)); | ||
} | ||
|
||
@Test | ||
public void testButtonClickEnablesReaderMode() { | ||
ReaderModeToolbarButtonController controller = createController(); | ||
|
||
ButtonData readerModeButton = controller.get(mMockTab); | ||
readerModeButton.getButtonSpec().getOnClickListener().onClick(null); | ||
|
||
verify(mMockReaderModeManager).activateReaderMode(); | ||
} | ||
|
||
@Test | ||
public void testSwapButtonModeSetsIphCommandBuilder() { | ||
ReaderModeToolbarButtonController controller = createController(); | ||
|
||
ButtonData readerModeButton = controller.get(mMockTab); | ||
|
||
Assert.assertNotNull(readerModeButton.getButtonSpec().getIPHCommandBuilder()); | ||
} | ||
|
||
@Test | ||
public void testActionChipModeSetsNoIphCommandBuilder() { | ||
// Set field trial param to use action chip variant. | ||
mTestValues.addFeatureFlagOverride( | ||
ChromeFeatureList.ADAPTIVE_BUTTON_IN_TOP_TOOLBAR_CUSTOMIZATION_V2, true); | ||
mTestValues.addFieldTrialParamOverride( | ||
ChromeFeatureList.CONTEXTUAL_PAGE_ACTIONS, "action_chip", "true"); | ||
|
||
ReaderModeToolbarButtonController controller = createController(); | ||
|
||
ButtonData readerModeButton = controller.get(mMockTab); | ||
|
||
// IPH command builder should not be set on action chip variant. | ||
Assert.assertNull(readerModeButton.getButtonSpec().getIPHCommandBuilder()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.