Commit eb15fe1a authored by Wenyu Fu's avatar Wenyu Fu Committed by Chromium LUCI CQ

Add test cases before FirstRun is complete

* Verify certain options in CCT AppMenu and ContextMenu are disabled;
* Verify Contextual Search is disabled.

Bug: 1120853
Change-Id: I37a614f35fb3a5781d483e4266ec89db9198cc3f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2582807
Commit-Queue: Wenyu Fu <wenyufu@chromium.org>
Reviewed-by: default avatarDonn Denman <donnd@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Reviewed-by: default avatarSinan Sahin <sinansahin@google.com>
Cr-Commit-Position: refs/heads/master@{#835866}
parent a693d1db
...@@ -7,6 +7,8 @@ package org.chromium.chrome.browser.contextmenu; ...@@ -7,6 +7,8 @@ package org.chromium.chrome.browser.contextmenu;
import android.util.Pair; import android.util.Pair;
import android.view.View; import android.view.View;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.TimeUtilsJni; import org.chromium.base.TimeUtilsJni;
...@@ -32,7 +34,7 @@ import java.util.concurrent.TimeUnit; ...@@ -32,7 +34,7 @@ import java.util.concurrent.TimeUnit;
* A helper class that handles generating context menus for {@link WebContents}s. * A helper class that handles generating context menus for {@link WebContents}s.
*/ */
public class ContextMenuHelper { public class ContextMenuHelper {
public static Callback<RevampedContextMenuCoordinator> sRevampedContextMenuShownCallback; private static Callback<RevampedContextMenuCoordinator> sMenuShownCallbackForTests;
private static final String TAG = "ContextMenuHelper"; private static final String TAG = "ContextMenuHelper";
...@@ -201,6 +203,9 @@ public class ContextMenuHelper { ...@@ -201,6 +203,9 @@ public class ContextMenuHelper {
List<Pair<Integer, ModelList>> items = mCurrentPopulator.buildContextMenu(); List<Pair<Integer, ModelList>> items = mCurrentPopulator.buildContextMenu();
if (items.isEmpty()) { if (items.isEmpty()) {
PostTask.postTask(UiThreadTaskTraits.DEFAULT, mOnMenuClosed); PostTask.postTask(UiThreadTaskTraits.DEFAULT, mOnMenuClosed);
if (sMenuShownCallbackForTests != null) {
sMenuShownCallbackForTests.onResult(null);
}
return; return;
} }
...@@ -216,8 +221,8 @@ public class ContextMenuHelper { ...@@ -216,8 +221,8 @@ public class ContextMenuHelper {
mCallback, mOnMenuShown, mOnMenuClosed); mCallback, mOnMenuShown, mOnMenuClosed);
} }
if (sRevampedContextMenuShownCallback != null) { if (sMenuShownCallbackForTests != null) {
sRevampedContextMenuShownCallback.onResult(menuCoordinator); sMenuShownCallbackForTests.onResult(menuCoordinator);
} }
} }
...@@ -237,6 +242,12 @@ public class ContextMenuHelper { ...@@ -237,6 +242,12 @@ public class ContextMenuHelper {
} }
} }
@VisibleForTesting
public static void setMenuShownCallbackForTests(
Callback<RevampedContextMenuCoordinator> callback) {
sMenuShownCallbackForTests = callback;
}
@NativeMethods @NativeMethods
interface Natives { interface Natives {
void onContextMenuClosed(long nativeContextMenuHelper, ContextMenuHelper caller); void onContextMenuClosed(long nativeContextMenuHelper, ContextMenuHelper caller);
......
...@@ -342,7 +342,9 @@ public class RevampedContextMenuCoordinator implements ContextMenuUi { ...@@ -342,7 +342,9 @@ public class RevampedContextMenuCoordinator implements ContextMenuUi {
public ListItem findItem(int id) { public ListItem findItem(int id) {
for (int i = 0; i < getCount(); i++) { for (int i = 0; i < getCount(); i++) {
final ListItem item = getItem(i); final ListItem item = getItem(i);
if (item.model.get(MENU_ID) == id) { // If the item is a title/divider, its model does not have MENU_ID as key.
if (item.model.getAllSetProperties().contains(MENU_ID)
&& item.model.get(MENU_ID) == id) {
return item; return item;
} }
} }
......
...@@ -81,6 +81,7 @@ import org.chromium.chrome.browser.contextualsearch.ContextualSearchInternalStat ...@@ -81,6 +81,7 @@ import org.chromium.chrome.browser.contextualsearch.ContextualSearchInternalStat
import org.chromium.chrome.browser.contextualsearch.ResolvedSearchTerm.CardTag; import org.chromium.chrome.browser.contextualsearch.ResolvedSearchTerm.CardTag;
import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl; import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.chrome.browser.findinpage.FindToolbar; import org.chromium.chrome.browser.findinpage.FindToolbar;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
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;
import org.chromium.chrome.browser.gsa.GSAContextDisplaySelection; import org.chromium.chrome.browser.gsa.GSAContextDisplaySelection;
...@@ -3318,6 +3319,33 @@ public class ContextualSearchManagerTest { ...@@ -3318,6 +3319,33 @@ public class ContextualSearchManagerTest {
mManager.onAccessibilityModeChanged(false); mManager.onAccessibilityModeChanged(false);
} }
/**
* Tests when FirstRun is not completed: Tap and Long-press don't activate CS.
*/
@Test
@SmallTest
@Feature({"ContextualSearch"})
@ParameterAnnotations.UseMethodParameter(FeatureParamProvider.class)
public void testFirstRunNotCompleted(@EnabledFeature int enabledFeature) throws Exception {
// Store the original value in a temp, and mark the first run as not completed
// for this test case.
boolean originalIsFirstRunComplete = FirstRunStatus.getFirstRunFlowComplete();
FirstRunStatus.setFirstRunFlowComplete(false);
// Simulate a tap that resolves to show the Bar.
clickNode("intelligence");
assertNoWebContents();
assertNoSearchesLoaded();
// Simulate a Long-press.
longPressNodeWithoutWaiting("states");
assertNoWebContents();
assertNoSearchesLoaded();
// Restore the original shared preference value before this test case ends.
FirstRunStatus.setFirstRunFlowComplete(originalIsFirstRunComplete);
}
//============================================================================================ //============================================================================================
// Internal State Controller tests, which ensure that the internal logic flows as expected for // Internal State Controller tests, which ensure that the internal logic flows as expected for
// each type of triggering gesture. // each type of triggering gesture.
......
...@@ -49,6 +49,7 @@ import android.widget.RemoteViews; ...@@ -49,6 +49,7 @@ import android.widget.RemoteViews;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.content.res.AppCompatResources;
import androidx.browser.customtabs.CustomTabsCallback; import androidx.browser.customtabs.CustomTabsCallback;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
...@@ -161,7 +162,6 @@ import java.util.concurrent.atomic.AtomicReference; ...@@ -161,7 +162,6 @@ import java.util.concurrent.atomic.AtomicReference;
* Instrumentation tests for app menu, context menu, and toolbar of a {@link CustomTabActivity}. * Instrumentation tests for app menu, context menu, and toolbar of a {@link CustomTabActivity}.
*/ */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class CustomTabActivityTest { public class CustomTabActivityTest {
@Rule @Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule(); public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
...@@ -348,6 +348,12 @@ public class CustomTabActivityTest { ...@@ -348,6 +348,12 @@ public class CustomTabActivityTest {
CustomTabsTestUtils.openAppMenuAndAssertMenuShown(mCustomTabActivityTestRule.getActivity()); CustomTabsTestUtils.openAppMenuAndAssertMenuShown(mCustomTabActivityTestRule.getActivity());
} }
private void assertAppMenuItemNotVisible(Menu menu, @IdRes int id, String itemName) {
MenuItem item = menu.findItem(id);
Assert.assertNotNull(String.format("Cannot find <%s>", itemName), item);
Assert.assertFalse(String.format("<%s> is visible", itemName), item.isVisible());
}
/** /**
* @return The number of visible and enabled items in the given menu. * @return The number of visible and enabled items in the given menu.
*/ */
...@@ -498,6 +504,33 @@ public class CustomTabActivityTest { ...@@ -498,6 +504,33 @@ public class CustomTabActivityTest {
Assert.assertNotNull(menu.findItem(R.id.contextmenu_save_video)); Assert.assertNotNull(menu.findItem(R.id.contextmenu_save_video));
} }
@Test
@SmallTest
public void testContextMenuEntriesBeforeFirstRun() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(createMinimalCustomTabIntent());
// Mark the first run as not completed. This has to be done after we start the intent,
// otherwise we are going to hit the FRE.
TestThreadUtils.runOnUiThreadBlocking(() -> FirstRunStatus.setFirstRunFlowComplete(false));
// The context menu for images should not be built when the first run is not completed.
RevampedContextMenuCoordinator imageMenu = RevampedContextMenuUtils.openContextMenu(
mCustomTabActivityTestRule.getActivity().getActivityTab(), "logo");
Assert.assertNull(
"Context menu for images should not be built when first run is not finished.",
imageMenu);
// Options on the context menu for links should be limited when the first run is not
// completed.
RevampedContextMenuCoordinator linkMenu = RevampedContextMenuUtils.openContextMenu(
mCustomTabActivityTestRule.getActivity().getActivityTab(), "aboutLink");
final int expectedMenuItems = 4;
Assert.assertEquals("Menu item count does not match expectation.", expectedMenuItems,
linkMenu.getCount());
Assert.assertNotNull(linkMenu.findItem(R.id.contextmenu_copy_link_text));
Assert.assertNotNull(linkMenu.findItem(R.id.contextmenu_copy_link_address));
}
/** /**
* Test the entries in the app menu. * Test the entries in the app menu.
*/ */
...@@ -605,6 +638,36 @@ public class CustomTabActivityTest { ...@@ -605,6 +638,36 @@ public class CustomTabActivityTest {
Assert.assertNotNull(icon_row_menu.findItem(R.id.reload_menu_id)); Assert.assertNotNull(icon_row_menu.findItem(R.id.reload_menu_id));
} }
@Test
@SmallTest
public void testAppMenuBeforeFirstRun() throws Exception {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(createMinimalCustomTabIntent());
// Mark the first run as not completed. This has to be done after we start the intent,
// otherwise we are going to hit the FRE.
TestThreadUtils.runOnUiThreadBlocking(() -> FirstRunStatus.setFirstRunFlowComplete(false));
openAppMenuAndAssertMenuShown();
Menu menu = mCustomTabActivityTestRule.getMenu();
final int expectedMenuSize = 3;
Assert.assertNotNull("App menu is not initialized: ", menu);
assertEquals(expectedMenuSize, getActualMenuSize(menu));
assertEquals(expectedMenuSize, getVisibleMenuSize(menu));
// Checks the first row (icons).
Assert.assertNotNull(menu.findItem(R.id.forward_menu_id));
Assert.assertNotNull(menu.findItem(R.id.info_menu_id));
Assert.assertNotNull(menu.findItem(R.id.reload_menu_id));
assertAppMenuItemNotVisible(menu, R.id.offline_page_id, "offline_page");
assertAppMenuItemNotVisible(menu, R.id.bookmark_this_page_id, "bookmark_this_page");
// Following rows.
Assert.assertNotNull(menu.findItem(R.id.find_in_page_id));
Assert.assertNotNull(menu.findItem(R.id.request_desktop_site_row_menu_id));
assertAppMenuItemNotVisible(menu, R.id.open_in_browser_id, "open_in_browser");
assertAppMenuItemNotVisible(menu, R.id.add_to_homescreen_id, "add_to_homescreen");
}
/** /**
* Tests if the default share item can be shown in the app menu. * Tests if the default share item can be shown in the app menu.
*/ */
......
...@@ -68,9 +68,9 @@ public class RevampedContextMenuUtils { ...@@ -68,9 +68,9 @@ public class RevampedContextMenuUtils {
private static RevampedContextMenuCoordinator openContextMenuByJs(Tab tab, String jsCode) private static RevampedContextMenuCoordinator openContextMenuByJs(Tab tab, String jsCode)
throws TimeoutException { throws TimeoutException {
final OnContextMenuShownHelper helper = new OnContextMenuShownHelper(); final OnContextMenuShownHelper helper = new OnContextMenuShownHelper();
ContextMenuHelper.sRevampedContextMenuShownCallback = ((coordinator) -> { ContextMenuHelper.setMenuShownCallbackForTests((coordinator) -> {
helper.notifyCalled(coordinator); helper.notifyCalled(coordinator);
ContextMenuHelper.sRevampedContextMenuShownCallback = null; ContextMenuHelper.setMenuShownCallbackForTests(null);
}); });
int callCount = helper.getCallCount(); int callCount = helper.getCallCount();
......
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