1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.app.appmenu;
6 
7 import static org.mockito.ArgumentMatchers.any;
8 import static org.mockito.ArgumentMatchers.anyBoolean;
9 import static org.mockito.Mockito.doReturn;
10 import static org.mockito.Mockito.when;
11 
12 import android.content.Context;
13 import android.view.Menu;
14 import android.view.SubMenu;
15 import android.view.View;
16 import android.widget.PopupMenu;
17 
18 import org.hamcrest.Matchers;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.TestRule;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.robolectric.annotation.Config;
30 
31 import org.chromium.base.ContextUtils;
32 import org.chromium.base.FeatureList;
33 import org.chromium.base.ThreadUtils;
34 import org.chromium.base.supplier.ObservableSupplierImpl;
35 import org.chromium.base.supplier.OneshotSupplierImpl;
36 import org.chromium.base.test.BaseRobolectricTestRunner;
37 import org.chromium.base.test.util.JniMocker;
38 import org.chromium.chrome.R;
39 import org.chromium.chrome.browser.ActivityTabProvider;
40 import org.chromium.chrome.browser.app.appmenu.AppMenuPropertiesDelegateImpl.AppMenuSimilarSelectionType;
41 import org.chromium.chrome.browser.app.appmenu.AppMenuPropertiesDelegateImpl.MenuGroup;
42 import org.chromium.chrome.browser.banners.AppBannerManager;
43 import org.chromium.chrome.browser.bookmarks.BookmarkBridge;
44 import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior;
45 import org.chromium.chrome.browser.flags.CachedFeatureFlags;
46 import org.chromium.chrome.browser.flags.ChromeFeatureList;
47 import org.chromium.chrome.browser.multiwindow.MultiWindowModeStateDispatcher;
48 import org.chromium.chrome.browser.omaha.UpdateMenuItemHelper;
49 import org.chromium.chrome.browser.preferences.Pref;
50 import org.chromium.chrome.browser.profiles.Profile;
51 import org.chromium.chrome.browser.tab.Tab;
52 import org.chromium.chrome.browser.tabmodel.TabModel;
53 import org.chromium.chrome.browser.tabmodel.TabModelSelector;
54 import org.chromium.chrome.browser.toolbar.ToolbarManager;
55 import org.chromium.chrome.browser.util.ChromeAccessibilityUtil;
56 import org.chromium.chrome.test.util.browser.Features;
57 import org.chromium.components.embedder_support.util.UrlConstants;
58 import org.chromium.components.prefs.PrefService;
59 import org.chromium.components.user_prefs.UserPrefs;
60 import org.chromium.components.user_prefs.UserPrefsJni;
61 import org.chromium.content.browser.ContentFeatureListImpl;
62 import org.chromium.content.browser.ContentFeatureListImplJni;
63 import org.chromium.content_public.browser.ContentFeatureList;
64 import org.chromium.content_public.browser.NavigationController;
65 import org.chromium.content_public.browser.WebContents;
66 import org.chromium.ui.modaldialog.ModalDialogManager;
67 
68 import java.util.ArrayList;
69 import java.util.List;
70 
71 /**
72  * Unit tests for {@link AppMenuPropertiesDelegateImpl}.
73  */
74 @RunWith(BaseRobolectricTestRunner.class)
75 public class AppMenuPropertiesDelegateUnitTest {
76     @Rule
77     public TestRule mProcessor = new Features.JUnitProcessor();
78 
79     @Rule
80     public JniMocker mJniMocker = new JniMocker();
81 
82     @Mock
83     private ActivityTabProvider mActivityTabProvider;
84     @Mock
85     private Tab mTab;
86     @Mock
87     private WebContents mWebContents;
88     @Mock
89     private NavigationController mNavigationController;
90     @Mock
91     private MultiWindowModeStateDispatcher mMultiWindowModeStateDispatcher;
92     @Mock
93     private TabModelSelector mTabModelSelector;
94     @Mock
95     private TabModel mTabModel;
96     @Mock
97     private TabModel mIncognitoTabModel;
98     @Mock
99     private ToolbarManager mToolbarManager;
100     @Mock
101     private View mDecorView;
102     @Mock
103     private OverviewModeBehavior mOverviewModeBehavior;
104     @Mock
105     private UpdateMenuItemHelper mUpdateMenuItemHelper;
106     @Mock
107     private ContentFeatureListImpl.Natives mContentFeatureListJniMock;
108     @Mock
109     private UserPrefs.Natives mUserPrefsJniMock;
110     @Mock
111     private Profile mProfile;
112     @Mock
113     private PrefService mPrefService;
114     @Mock
115     private ModalDialogManager mModalDialogManager;
116 
117     private OneshotSupplierImpl<OverviewModeBehavior> mOverviewModeSupplier =
118             new OneshotSupplierImpl<>();
119     private ObservableSupplierImpl<BookmarkBridge> mBookmarkBridgeSupplier =
120             new ObservableSupplierImpl<>();
121 
122     private AppMenuPropertiesDelegateImpl mAppMenuPropertiesDelegate;
123 
124     private UpdateMenuItemHelper.MenuUiState mMenuUiState;
125 
126     @Before
setUp()127     public void setUp() {
128         MockitoAnnotations.initMocks(this);
129 
130         mOverviewModeSupplier.set(mOverviewModeBehavior);
131         when(mTab.getWebContents()).thenReturn(mWebContents);
132         when(mWebContents.getNavigationController()).thenReturn(mNavigationController);
133         when(mNavigationController.getUseDesktopUserAgent()).thenReturn(false);
134         when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
135         when(mTabModelSelector.getModel(false)).thenReturn((mTabModel));
136         when(mTabModelSelector.getModel(true)).thenReturn((mIncognitoTabModel));
137         when(mTabModel.isIncognito()).thenReturn(false);
138         when(mIncognitoTabModel.isIncognito()).thenReturn(true);
139 
140         UpdateMenuItemHelper.setInstanceForTesting(mUpdateMenuItemHelper);
141         mMenuUiState = new UpdateMenuItemHelper.MenuUiState();
142         doReturn(mMenuUiState).when(mUpdateMenuItemHelper).getUiState();
143 
144         mJniMocker.mock(ContentFeatureListImplJni.TEST_HOOKS, mContentFeatureListJniMock);
145         mJniMocker.mock(UserPrefsJni.TEST_HOOKS, mUserPrefsJniMock);
146         Profile.setLastUsedProfileForTesting(mProfile);
147         Mockito.when(mUserPrefsJniMock.get(mProfile)).thenReturn(mPrefService);
148         FeatureList.setTestCanUseDefaultsForTesting();
149 
150         mAppMenuPropertiesDelegate = Mockito.spy(new AppMenuPropertiesDelegateImpl(
151                 ContextUtils.getApplicationContext(), mActivityTabProvider,
152                 mMultiWindowModeStateDispatcher, mTabModelSelector, mToolbarManager, mDecorView,
153                 mOverviewModeSupplier, mBookmarkBridgeSupplier, mModalDialogManager));
154     }
155 
156     @After
tearDown()157     public void tearDown() {
158         ThreadUtils.setThreadAssertsDisabledForTesting(false);
159         ChromeAccessibilityUtil.get().setAccessibilityEnabledForTesting(false);
160     }
161 
162     @Test
163     @Config(qualifiers = "sw320dp")
testShouldShowPageMenu_Phone()164     public void testShouldShowPageMenu_Phone() {
165         setUpMocksForPageMenu();
166         Assert.assertTrue(mAppMenuPropertiesDelegate.shouldShowPageMenu());
167         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
168     }
169 
170     @Test
171     @Config(qualifiers = "sw600dp")
testShouldShowPageMenu_Tablet()172     public void testShouldShowPageMenu_Tablet() {
173         when(mOverviewModeBehavior.overviewVisible()).thenReturn(false);
174         when(mTabModel.getCount()).thenReturn(1);
175         Assert.assertTrue(mAppMenuPropertiesDelegate.shouldShowPageMenu());
176         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
177     }
178 
179     @Test
180     @Config(qualifiers = "sw600dp")
testShouldShowOverviewMenu_Tablet()181     public void testShouldShowOverviewMenu_Tablet() {
182         when(mOverviewModeBehavior.overviewVisible()).thenReturn(true);
183         when(mTabModel.getCount()).thenReturn(1);
184         Assert.assertFalse(mAppMenuPropertiesDelegate.shouldShowPageMenu());
185         Assert.assertEquals(
186                 MenuGroup.OVERVIEW_MODE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
187     }
188 
189     @Test
190     @Config(qualifiers = "sw320dp")
testShouldShowIconRow_Phone()191     public void testShouldShowIconRow_Phone() {
192         Assert.assertTrue(mAppMenuPropertiesDelegate.shouldShowIconRow());
193     }
194 
195     @Test
196     @Config(qualifiers = "sw600dp")
testShouldShowIconRow_Tablet()197     public void testShouldShowIconRow_Tablet() {
198         when(mDecorView.getWidth())
199                 .thenReturn((int) (600
200                         * ContextUtils.getApplicationContext()
201                                   .getResources()
202                                   .getDisplayMetrics()
203                                   .density));
204         Assert.assertFalse(mAppMenuPropertiesDelegate.shouldShowIconRow());
205     }
206 
207     @Test
208     @Config(qualifiers = "sw600dp")
testShouldShowIconRow_TabletNarrow()209     public void testShouldShowIconRow_TabletNarrow() {
210         when(mDecorView.getWidth())
211                 .thenReturn((int) (100
212                         * ContextUtils.getApplicationContext()
213                                   .getResources()
214                                   .getDisplayMetrics()
215                                   .density));
216         Assert.assertTrue(mAppMenuPropertiesDelegate.shouldShowIconRow());
217     }
218 
219     @Test
220     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_Ntp()221     public void testPageMenuItems_Phone_Ntp() {
222         setUpMocksForPageMenu();
223         when(mTab.getUrlString()).thenReturn(UrlConstants.NTP_URL);
224         when(mTab.isNativePage()).thenReturn(true);
225         doReturn(false)
226                 .when(mAppMenuPropertiesDelegate)
227                 .shouldShowTranslateMenuItem(any(Tab.class));
228 
229         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
230         Menu menu = createTestMenu();
231         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
232 
233         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
234                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
235                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
236                 R.id.request_desktop_site_row_menu_id, R.id.preferences_id, R.id.help_id};
237         assertMenuItemsAreEqual(menu, expectedItems);
238     }
239 
240     @Test
241     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage()242     public void testPageMenuItems_Phone_RegularPage() {
243         setUpMocksForPageMenu();
244         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, false /*showUpdate*/,
245                 false /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
246                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
247 
248         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
249         Menu menu = createTestMenu();
250         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
251 
252         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
253                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
254                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
255                 R.id.translate_id, R.id.share_row_menu_id, R.id.find_in_page_id,
256                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
257                 R.id.preferences_id, R.id.help_id};
258         Integer[] expectedTitles = {0, R.string.menu_new_tab, R.string.menu_new_incognito_tab,
259                 R.string.menu_bookmarks, R.string.menu_recent_tabs, R.string.menu_history,
260                 R.string.menu_downloads, R.string.menu_translate, 0, R.string.menu_find_in_page,
261                 R.string.menu_add_to_homescreen, 0, R.string.menu_settings, R.string.menu_help};
262         Integer[] expectedActionBarItems = {R.id.forward_menu_id, R.id.bookmark_this_page_id,
263                 R.id.offline_page_id, R.id.info_menu_id, R.id.reload_menu_id};
264         assertMenuItemsAreEqual(menu, expectedItems);
265         assertMenuTitlesAreEqual(menu, expectedTitles);
266         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
267     }
268 
269     @Test
270     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_WithPwa()271     public void testPageMenuItems_Phone_RegularPage_WithPwa() {
272         setUpMocksForPageMenu();
273         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, false /*showUpdate*/,
274                 false /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
275                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
276         doReturn(new AppBannerManager.InstallStringPair(R.string.menu_add_to_homescreen_install,
277                          R.string.menu_add_to_homescreen_install))
278                 .when(mAppMenuPropertiesDelegate)
279                 .getAddToHomeScreenTitle(mTab);
280 
281         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
282         Menu menu = createTestMenu();
283         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
284 
285         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
286                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
287                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
288                 R.id.translate_id, R.id.share_row_menu_id, R.id.find_in_page_id,
289                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
290                 R.id.preferences_id, R.id.help_id};
291         Integer[] expectedTitles = {0, R.string.menu_new_tab, R.string.menu_new_incognito_tab,
292                 R.string.menu_bookmarks, R.string.menu_recent_tabs, R.string.menu_history,
293                 R.string.menu_downloads, R.string.menu_translate, 0, R.string.menu_find_in_page,
294                 R.string.menu_add_to_homescreen_install, 0, R.string.menu_settings,
295                 R.string.menu_help};
296         Integer[] expectedActionBarItems = {R.id.forward_menu_id, R.id.bookmark_this_page_id,
297                 R.id.offline_page_id, R.id.info_menu_id, R.id.reload_menu_id};
298         assertMenuItemsAreEqual(menu, expectedItems);
299         assertMenuTitlesAreEqual(menu, expectedTitles);
300         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
301     }
302 
303     @Test
304     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_enterprise_user()305     public void testPageMenuItems_Phone_RegularPage_enterprise_user() {
306         setUpMocksForPageMenu();
307         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, false /*showUpdate*/,
308                 false /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
309                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
310         doReturn(true).when(mAppMenuPropertiesDelegate).shouldShowManagedByMenuItem(any(Tab.class));
311 
312         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
313         Menu menu = createTestMenu();
314         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
315 
316         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
317                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
318                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
319                 R.id.translate_id, R.id.share_row_menu_id, R.id.find_in_page_id,
320                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
321                 R.id.preferences_id, R.id.help_id, R.id.managed_by_menu_id};
322         assertMenuItemsAreEqual(menu, expectedItems);
323     }
324 
325     @Test
326     @Config(qualifiers = "sw320dp")
testPageMenuItemsIcons_Phone_RegularPage_iconsAfterMenuItems()327     public void testPageMenuItemsIcons_Phone_RegularPage_iconsAfterMenuItems() {
328         setUpMocksForPageMenu();
329         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
330                 true /*showMoveToOtherWindow*/, true /*showReaderModePrefs*/,
331                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
332 
333         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
334         Menu menu = createTestMenu();
335         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
336 
337         Integer[] expectedItems = {R.id.update_menu_id, R.id.reader_mode_prefs_id};
338         assertMenuItemsHaveIcons(menu, expectedItems);
339     }
340 
341     @Test
342     @Config(qualifiers = "sw320dp")
testPageMenuItemsIcons_Phone_RegularPage_iconsBeforeMenuItems()343     public void testPageMenuItemsIcons_Phone_RegularPage_iconsBeforeMenuItems() {
344         setUpMocksForPageMenu();
345         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
346                 false /*showMoveToOtherWindow*/, true /*showReaderModePrefs*/,
347                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
348         doReturn(true).when(mAppMenuPropertiesDelegate).shouldShowIconBeforeItem();
349 
350         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
351         Menu menu = createTestMenu();
352         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
353 
354         Integer[] expectedItems = {R.id.update_menu_id, R.id.new_tab_menu_id,
355                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
356                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
357                 R.id.translate_id, R.id.find_in_page_id, R.id.add_to_homescreen_id,
358                 R.id.reader_mode_prefs_id, R.id.preferences_id, R.id.help_id};
359         assertMenuItemsHaveIcons(menu, expectedItems);
360     }
361 
362     @Test
363     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_regroup()364     public void testPageMenuItems_Phone_RegularPage_regroup() {
365         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
366         setUpMocksForPageMenu();
367         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
368                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
369                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
370 
371         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
372         Menu menu = createTestMenu();
373         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
374 
375         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id,
376                 R.id.move_to_other_window_menu_id, R.id.new_tab_menu_id,
377                 R.id.new_incognito_tab_menu_id, R.id.divider_line_id, R.id.open_history_menu_id,
378                 R.id.downloads_row_menu_id, R.id.all_bookmarks_row_menu_id,
379                 R.id.recent_tabs_menu_id, R.id.divider_line_id, R.id.share_row_menu_id,
380                 R.id.paint_preview_show_id, R.id.find_in_page_id, R.id.translate_id,
381                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
382                 R.id.divider_line_id, R.id.preferences_id, R.id.help_id};
383         Integer[] expectedActionBarItems = {R.id.forward_menu_id, R.id.bookmark_this_page_id,
384                 R.id.offline_page_id, R.id.info_menu_id, R.id.reload_menu_id};
385         assertMenuItemsAreEqual(menu, expectedItems);
386         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
387     }
388 
389     @Test
390     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_backward_button_action_bar()391     public void testPageMenuItems_Phone_RegularPage_backward_button_action_bar() {
392         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
393         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("backward_button");
394         setUpMocksForPageMenu();
395         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, false /*showUpdate*/,
396                 false /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
397                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
398 
399         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
400         Menu menu = createTestMenu();
401         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
402 
403         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
404                 R.id.new_incognito_tab_menu_id, R.id.divider_line_id, R.id.open_history_menu_id,
405                 R.id.downloads_row_menu_id, R.id.all_bookmarks_row_menu_id,
406                 R.id.recent_tabs_menu_id, R.id.divider_line_id, R.id.share_row_menu_id,
407                 R.id.find_in_page_id, R.id.translate_id, R.id.add_to_homescreen_id,
408                 R.id.request_desktop_site_row_menu_id, R.id.divider_line_id, R.id.preferences_id,
409                 R.id.info_id, R.id.help_id};
410         Integer[] expectedActionBarItems = {R.id.backward_menu_id, R.id.forward_menu_id,
411                 R.id.offline_page_id, R.id.bookmark_this_page_id, R.id.reload_menu_id};
412         assertMenuItemsAreEqual(menu, expectedItems);
413         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
414     }
415 
416     @Test
417     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_share_button_action_bar()418     public void testPageMenuItems_Phone_RegularPage_share_button_action_bar() {
419         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
420         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("share_button");
421         setUpMocksForPageMenu();
422         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, false /*showUpdate*/,
423                 false /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
424                 true /*showAddToHomeScreen*/, false /*showPaintPreview*/);
425 
426         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
427         Menu menu = createTestMenu();
428         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
429 
430         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
431                 R.id.new_incognito_tab_menu_id, R.id.divider_line_id, R.id.open_history_menu_id,
432                 R.id.downloads_row_menu_id, R.id.all_bookmarks_row_menu_id,
433                 R.id.recent_tabs_menu_id, R.id.divider_line_id, R.id.find_in_page_id,
434                 R.id.translate_id, R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
435                 R.id.divider_line_id, R.id.preferences_id, R.id.info_id, R.id.help_id};
436         Integer[] expectedActionBarItems = {R.id.forward_menu_id, R.id.bookmark_this_page_id,
437                 R.id.offline_page_id, R.id.share_menu_button_id, R.id.reload_menu_id};
438         assertMenuItemsAreEqual(menu, expectedItems);
439         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
440     }
441 
442     @Test
443     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_threebutton_actionbar()444     public void testPageMenuItems_Phone_RegularPage_threebutton_actionbar() {
445         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
446         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("");
447         CachedFeatureFlags.setForTesting(
448                 ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_THREE_BUTTON_ACTIONBAR, true);
449         AppMenuPropertiesDelegateImpl.THREE_BUTTON_ACTION_BAR_VARIATION.setForTesting(
450                 "action_chip_view");
451         setUpMocksForPageMenu();
452         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
453                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
454                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
455 
456         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
457         Menu menu = createTestMenu();
458         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
459 
460         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id, R.id.new_tab_menu_id,
461                 R.id.new_incognito_tab_menu_id, R.id.move_to_other_window_menu_id,
462                 R.id.divider_line_id, R.id.open_history_menu_id, R.id.downloads_row_menu_id,
463                 R.id.all_bookmarks_row_menu_id, R.id.recent_tabs_menu_id, R.id.divider_line_id,
464                 R.id.share_row_menu_id, R.id.paint_preview_show_id, R.id.find_in_page_id,
465                 R.id.translate_id, R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
466                 R.id.divider_line_id, R.id.preferences_id, R.id.help_id};
467         Integer[] expectedActionBarItems = {
468                 R.id.forward_menu_id, R.id.info_menu_id, R.id.reload_menu_id};
469         assertMenuItemsAreEqual(menu, expectedItems);
470         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
471     }
472 
473     @Test
474     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_threebutton_actionbar_backward_actionbar()475     public void testPageMenuItems_Phone_RegularPage_threebutton_actionbar_backward_actionbar() {
476         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
477         CachedFeatureFlags.setForTesting(
478                 ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_THREE_BUTTON_ACTIONBAR, true);
479         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("backward_button");
480         AppMenuPropertiesDelegateImpl.THREE_BUTTON_ACTION_BAR_VARIATION.setForTesting(
481                 "action_chip_view");
482         setUpMocksForPageMenu();
483         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
484                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
485                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
486 
487         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
488         Menu menu = createTestMenu();
489         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
490 
491         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id, R.id.new_tab_menu_id,
492                 R.id.new_incognito_tab_menu_id, R.id.move_to_other_window_menu_id,
493                 R.id.divider_line_id, R.id.open_history_menu_id, R.id.downloads_row_menu_id,
494                 R.id.all_bookmarks_row_menu_id, R.id.recent_tabs_menu_id, R.id.divider_line_id,
495                 R.id.share_row_menu_id, R.id.paint_preview_show_id, R.id.find_in_page_id,
496                 R.id.translate_id, R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
497                 R.id.divider_line_id, R.id.preferences_id, R.id.info_id, R.id.help_id};
498         Integer[] expectedActionBarItems = {
499                 R.id.backward_menu_id, R.id.forward_menu_id, R.id.reload_menu_id};
500         assertMenuItemsAreEqual(menu, expectedItems);
501     }
502 
503     @Test
504     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_threebutton_actionbar_share_actionbar()505     public void testPageMenuItems_Phone_RegularPage_threebutton_actionbar_share_actionbar() {
506         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
507         CachedFeatureFlags.setForTesting(
508                 ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_THREE_BUTTON_ACTIONBAR, true);
509         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("share_button");
510         AppMenuPropertiesDelegateImpl.THREE_BUTTON_ACTION_BAR_VARIATION.setForTesting(
511                 "destination_chip_view");
512         setUpMocksForPageMenu();
513         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
514                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
515                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
516 
517         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
518         Menu menu = createTestMenu();
519         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
520 
521         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id, R.id.new_tab_menu_id,
522                 R.id.new_incognito_tab_menu_id, R.id.move_to_other_window_menu_id,
523                 R.id.divider_line_id, R.id.open_history_menu_id, R.id.downloads_row_menu_id,
524                 R.id.all_bookmarks_row_menu_id, R.id.recent_tabs_menu_id, R.id.divider_line_id,
525                 R.id.paint_preview_show_id, R.id.find_in_page_id, R.id.translate_id,
526                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
527                 R.id.divider_line_id, R.id.preferences_id, R.id.info_id, R.id.help_id};
528         Integer[] expectedActionBarItems = {
529                 R.id.forward_menu_id, R.id.share_menu_button_id, R.id.reload_menu_id};
530         assertMenuItemsAreEqual(menu, expectedItems);
531         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
532     }
533 
534     @Test
535     @Config(qualifiers = "sw320dp")
testPageMenuItems_Phone_RegularPage_threebutton_actionbar_add_to_menuitem()536     public void testPageMenuItems_Phone_RegularPage_threebutton_actionbar_add_to_menuitem() {
537         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
538         CachedFeatureFlags.setForTesting(
539                 ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_THREE_BUTTON_ACTIONBAR, true);
540         CachedFeatureFlags.setForTesting(ChromeFeatureList.READ_LATER, true);
541         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("share_button");
542         AppMenuPropertiesDelegateImpl.THREE_BUTTON_ACTION_BAR_VARIATION.setForTesting(
543                 "add_to_option");
544         setUpMocksForPageMenu();
545         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
546                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
547                 true /*showAddToHomeScreen*/, true /*showPaintPreview*/);
548 
549         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
550         Menu menu = createTestMenu();
551         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
552 
553         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id, R.id.new_tab_menu_id,
554                 R.id.new_incognito_tab_menu_id, R.id.move_to_other_window_menu_id,
555                 R.id.divider_line_id, R.id.open_history_menu_id, R.id.downloads_row_menu_id,
556                 R.id.all_bookmarks_row_menu_id, R.id.recent_tabs_menu_id,
557                 R.id.add_to_divider_line_id, R.id.add_to_menu_id, R.id.divider_line_id,
558                 R.id.paint_preview_show_id, R.id.find_in_page_id, R.id.translate_id,
559                 R.id.request_desktop_site_row_menu_id, R.id.divider_line_id, R.id.preferences_id,
560                 R.id.info_id, R.id.help_id};
561         Integer[] expectedActionBarItems = {
562                 R.id.forward_menu_id, R.id.share_menu_button_id, R.id.reload_menu_id};
563         Integer[] expectedAddToItems = {R.id.add_to_bookmarks_menu_id,
564                 R.id.add_to_reading_list_menu_id, R.id.add_to_downloads_menu_id,
565                 R.id.add_to_homescreen_menu_id};
566         assertMenuItemsAreEqual(menu, expectedItems);
567         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
568         assertAddToItemsAreEqual(menu, expectedAddToItems);
569     }
570 
571     @Test
572     @Config(qualifiers = "sw320dp")
573     public void
testPageMenuItems_Phone_RegularPage_threebutton_actionbar_add_to_menuitem_intall_app()574     testPageMenuItems_Phone_RegularPage_threebutton_actionbar_add_to_menuitem_intall_app() {
575         CachedFeatureFlags.setForTesting(ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_REGROUP, true);
576         CachedFeatureFlags.setForTesting(
577                 ChromeFeatureList.TABBED_APP_OVERFLOW_MENU_THREE_BUTTON_ACTIONBAR, true);
578         AppMenuPropertiesDelegateImpl.ACTION_BAR_VARIATION.setForTesting("share_button");
579         AppMenuPropertiesDelegateImpl.THREE_BUTTON_ACTION_BAR_VARIATION.setForTesting(
580                 "add_to_option");
581         setUpMocksForPageMenu();
582         setMenuOptions(false /*isNativePage*/, true /*showTranslate*/, true /*showUpdate*/,
583                 true /*showMoveToOtherWindow*/, false /*showReaderModePrefs*/,
584                 false /*showAddToHomeScreen*/, true /*showPaintPreview*/);
585         doReturn(new AppBannerManager.InstallStringPair(
586                          R.string.menu_add_to_homescreen_install, R.string.app_banner_install))
587                 .when(mAppMenuPropertiesDelegate)
588                 .getAddToHomeScreenTitle(mTab);
589 
590         Assert.assertEquals(MenuGroup.PAGE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
591         Menu menu = createTestMenu();
592         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
593 
594         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.update_menu_id, R.id.new_tab_menu_id,
595                 R.id.new_incognito_tab_menu_id, R.id.move_to_other_window_menu_id,
596                 R.id.divider_line_id, R.id.open_history_menu_id, R.id.downloads_row_menu_id,
597                 R.id.all_bookmarks_row_menu_id, R.id.recent_tabs_menu_id,
598                 R.id.add_to_divider_line_id, R.id.add_to_menu_id, R.id.install_app_id,
599                 R.id.divider_line_id, R.id.paint_preview_show_id, R.id.find_in_page_id,
600                 R.id.translate_id, R.id.request_desktop_site_row_menu_id, R.id.divider_line_id,
601                 R.id.preferences_id, R.id.info_id, R.id.help_id};
602         Integer[] expectedActionBarItems = {
603                 R.id.forward_menu_id, R.id.share_menu_button_id, R.id.reload_menu_id};
604         Integer[] expectedAddToItems = {
605                 R.id.add_to_bookmarks_menu_id, R.id.add_to_downloads_menu_id};
606         assertMenuItemsAreEqual(menu, expectedItems);
607         assertActionBarItemsAreEqual(menu, expectedActionBarItems);
608         assertAddToItemsAreEqual(menu, expectedAddToItems);
609     }
610 
611     @Test
612     @Config(qualifiers = "sw320dp")
testOverviewMenuItems_Phone()613     public void testOverviewMenuItems_Phone() {
614         setUpMocksForOverviewMenu();
615         when(mIncognitoTabModel.getCount()).thenReturn(0);
616 
617         Assert.assertFalse(mAppMenuPropertiesDelegate.shouldShowPageMenu());
618         Assert.assertEquals(
619                 MenuGroup.OVERVIEW_MODE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
620 
621         Menu menu = createTestMenu();
622         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
623 
624         Integer[] expectedItems = {R.id.new_tab_menu_id, R.id.new_incognito_tab_menu_id,
625                 R.id.close_all_tabs_menu_id, R.id.preferences_id};
626         assertMenuItemsAreEqual(menu, expectedItems);
627     }
628 
629     @Test
630     @Config(qualifiers = "sw600dp")
testOverviewMenuItems_Tablet_NoTabs()631     public void testOverviewMenuItems_Tablet_NoTabs() {
632         setUpIncognitoMocks();
633         when(mOverviewModeBehavior.overviewVisible()).thenReturn(false);
634         when(mTabModel.getCount()).thenReturn(0);
635 
636         Assert.assertEquals(
637                 MenuGroup.TABLET_EMPTY_MODE_MENU, mAppMenuPropertiesDelegate.getMenuGroup());
638         Assert.assertFalse(mAppMenuPropertiesDelegate.shouldShowPageMenu());
639 
640         Menu menu = createTestMenu();
641         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
642 
643         Integer[] expectedItems = {
644                 R.id.new_tab_menu_id, R.id.new_incognito_tab_menu_id, R.id.preferences_id};
645         assertMenuItemsAreEqual(menu, expectedItems);
646     }
647 
648     @Test
testMenuItems_Accessibility()649     public void testMenuItems_Accessibility() {
650         setUpMocksForPageMenu();
651         when(mTab.getUrlString()).thenReturn("https://google.com");
652         when(mTab.isNativePage()).thenReturn(false);
653         doReturn(false)
654                 .when(mAppMenuPropertiesDelegate)
655                 .shouldShowPaintPreview(anyBoolean(), any(Tab.class), anyBoolean());
656         doReturn(false)
657                 .when(mAppMenuPropertiesDelegate)
658                 .shouldShowTranslateMenuItem(any(Tab.class));
659         doReturn(new AppBannerManager.InstallStringPair(
660                          R.string.menu_add_to_homescreen, R.string.add))
661                 .when(mAppMenuPropertiesDelegate)
662                 .getAddToHomeScreenTitle(mTab);
663 
664         // Ensure the get image descriptions option is shown as needed
665         when(mContentFeatureListJniMock.isEnabled(
666                      ContentFeatureList.EXPERIMENTAL_ACCESSIBILITY_LABELS))
667                 .thenReturn(true);
668         when(mPrefService.getBoolean(Pref.ACCESSIBILITY_IMAGE_LABELS_ENABLED_ANDROID))
669                 .thenReturn(false);
670 
671         // Test specific setup
672         ThreadUtils.setThreadAssertsDisabledForTesting(true);
673         ChromeAccessibilityUtil.get().setAccessibilityEnabledForTesting(true);
674 
675         Menu menu = createTestMenu();
676         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
677 
678         Integer[] expectedItems = {R.id.icon_row_menu_id, R.id.new_tab_menu_id,
679                 R.id.new_incognito_tab_menu_id, R.id.all_bookmarks_menu_id,
680                 R.id.recent_tabs_menu_id, R.id.open_history_menu_id, R.id.downloads_menu_id,
681                 R.id.share_row_menu_id, R.id.get_image_descriptions_id, R.id.find_in_page_id,
682                 R.id.add_to_homescreen_id, R.id.request_desktop_site_row_menu_id,
683                 R.id.preferences_id, R.id.help_id};
684 
685         assertMenuItemsAreEqual(menu, expectedItems);
686 
687         // Ensure the text of the menu item is correct
688         Assert.assertEquals(
689                 "Get image descriptions", menu.findItem(R.id.get_image_descriptions_id).getTitle());
690 
691         // Enable the feature and ensure text changes
692         when(mPrefService.getBoolean(Pref.ACCESSIBILITY_IMAGE_LABELS_ENABLED_ANDROID))
693                 .thenReturn(true);
694 
695         mAppMenuPropertiesDelegate.prepareMenu(menu, null);
696         Assert.assertEquals("Stop image descriptions",
697                 menu.findItem(R.id.get_image_descriptions_id).getTitle());
698     }
699 
700     @Test
testMenuItems_AppMenuSimilarSelectionChecker()701     public void testMenuItems_AppMenuSimilarSelectionChecker() {
702         Assert.assertEquals("No match for bookmark page then all bookmarks",
703                 AppMenuSimilarSelectionType.BOOKMARK_PAGE_THEN_ALL_BOOKMARKS,
704                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
705                         R.id.bookmark_this_page_id, R.id.all_bookmarks_menu_id));
706         Assert.assertEquals("No match for bookmark page then all bookmarks",
707                 AppMenuSimilarSelectionType.BOOKMARK_PAGE_THEN_ALL_BOOKMARKS,
708                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
709                         R.id.bookmark_this_page_chip_id, R.id.all_bookmarks_menu_id));
710         Assert.assertEquals("No match for bookmark page then all bookmarks",
711                 AppMenuSimilarSelectionType.BOOKMARK_PAGE_THEN_ALL_BOOKMARKS,
712                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
713                         R.id.add_to_bookmarks_menu_id, R.id.all_bookmarks_menu_id));
714         Assert.assertTrue("Should return true for bookmark page then all bookmarks",
715                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
716                         R.id.bookmark_this_page_id, R.id.all_bookmarks_menu_id));
717         Assert.assertTrue("Should return true for bookmark page then all bookmarks",
718                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
719                         R.id.bookmark_this_page_chip_id, R.id.all_bookmarks_menu_id));
720         Assert.assertTrue("Should return true for bookmark page then all bookmarks",
721                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
722                         R.id.add_to_bookmarks_menu_id, R.id.all_bookmarks_menu_id));
723 
724         Assert.assertEquals("No match for all bookmarks then bookmark page",
725                 AppMenuSimilarSelectionType.ALL_BOOKMARKS_THEN_BOOKMARK_PAGE,
726                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
727                         R.id.all_bookmarks_menu_id, R.id.bookmark_this_page_id));
728         Assert.assertEquals("No match for all bookmarks then bookmark page",
729                 AppMenuSimilarSelectionType.ALL_BOOKMARKS_THEN_BOOKMARK_PAGE,
730                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
731                         R.id.all_bookmarks_menu_id, R.id.bookmark_this_page_chip_id));
732         Assert.assertEquals("No match for all bookmarks then bookmark page",
733                 AppMenuSimilarSelectionType.ALL_BOOKMARKS_THEN_BOOKMARK_PAGE,
734                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
735                         R.id.all_bookmarks_menu_id, R.id.add_to_bookmarks_menu_id));
736         Assert.assertTrue("Should return true for all bookmarks then bookmark page",
737                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
738                         R.id.all_bookmarks_menu_id, R.id.bookmark_this_page_id));
739         Assert.assertTrue("Should return true for all bookmarks then bookmark page",
740                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
741                         R.id.all_bookmarks_menu_id, R.id.bookmark_this_page_chip_id));
742         Assert.assertTrue("Should return true for all bookmarks then bookmark page",
743                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
744                         R.id.all_bookmarks_menu_id, R.id.add_to_bookmarks_menu_id));
745 
746         Assert.assertEquals("No match for download page then all downloads",
747                 AppMenuSimilarSelectionType.DOWNLOAD_PAGE_THEN_ALL_DOWNLOADS,
748                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
749                         R.id.offline_page_id, R.id.downloads_menu_id));
750         Assert.assertEquals("No match for download page then all downloads",
751                 AppMenuSimilarSelectionType.DOWNLOAD_PAGE_THEN_ALL_DOWNLOADS,
752                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
753                         R.id.offline_page_chip_id, R.id.downloads_menu_id));
754         Assert.assertEquals("No match for download page then all downloads",
755                 AppMenuSimilarSelectionType.DOWNLOAD_PAGE_THEN_ALL_DOWNLOADS,
756                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
757                         R.id.add_to_downloads_menu_id, R.id.downloads_menu_id));
758         Assert.assertTrue("Should return true for download page then all downloads",
759                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
760                         R.id.offline_page_id, R.id.downloads_menu_id));
761         Assert.assertTrue("Should return true for download page then all downloads",
762                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
763                         R.id.offline_page_chip_id, R.id.downloads_menu_id));
764         Assert.assertTrue("Should return true for download page then all downloads",
765                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
766                         R.id.add_to_downloads_menu_id, R.id.downloads_menu_id));
767 
768         Assert.assertEquals("No match for all downloads then download page",
769                 AppMenuSimilarSelectionType.ALL_DOWNLOADS_THEN_DOWNLOAD_PAGE,
770                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
771                         R.id.downloads_menu_id, R.id.offline_page_id));
772         Assert.assertEquals("No match for all downloads then download page",
773                 AppMenuSimilarSelectionType.ALL_DOWNLOADS_THEN_DOWNLOAD_PAGE,
774                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
775                         R.id.downloads_menu_id, R.id.offline_page_chip_id));
776         Assert.assertEquals("No match for all downloads then download page",
777                 AppMenuSimilarSelectionType.ALL_DOWNLOADS_THEN_DOWNLOAD_PAGE,
778                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
779                         R.id.downloads_menu_id, R.id.add_to_downloads_menu_id));
780         Assert.assertTrue("Should return true for all downloads then download page",
781                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
782                         R.id.downloads_menu_id, R.id.offline_page_id));
783         Assert.assertTrue("Should return true for all downloads then download page",
784                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
785                         R.id.downloads_menu_id, R.id.offline_page_chip_id));
786         Assert.assertTrue("Should return true for all downloads then download page",
787                 mAppMenuPropertiesDelegate.recordAppMenuSimilarSelectionIfNeeded(
788                         R.id.downloads_menu_id, R.id.add_to_downloads_menu_id));
789 
790         Assert.assertEquals("Should no match for all downloads then all bookmarks",
791                 AppMenuSimilarSelectionType.NO_MATCH,
792                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
793                         R.id.downloads_menu_id, R.id.all_bookmarks_menu_id));
794         Assert.assertEquals("Should no match for all bookmarks then share",
795                 AppMenuSimilarSelectionType.NO_MATCH,
796                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
797                         R.id.all_bookmarks_menu_id, R.id.share_menu_button_id));
798         Assert.assertEquals("Should no match for new tab then find in page",
799                 AppMenuSimilarSelectionType.NO_MATCH,
800                 mAppMenuPropertiesDelegate.findSimilarSelectionPattern(
801                         R.id.new_tab_menu_id, R.id.find_in_page_id));
802     }
803 
setUpMocksForPageMenu()804     private void setUpMocksForPageMenu() {
805         when(mActivityTabProvider.get()).thenReturn(mTab);
806         when(mOverviewModeBehavior.overviewVisible()).thenReturn(false);
807         doReturn(false).when(mAppMenuPropertiesDelegate).shouldCheckBookmarkStar(any(Tab.class));
808         doReturn(false).when(mAppMenuPropertiesDelegate).shouldEnableDownloadPage(any(Tab.class));
809         doReturn(false).when(mAppMenuPropertiesDelegate).shouldShowReaderModePrefs(any(Tab.class));
810         doReturn(false)
811                 .when(mAppMenuPropertiesDelegate)
812                 .shouldShowManagedByMenuItem(any(Tab.class));
813         setUpIncognitoMocks();
814     }
815 
setUpMocksForOverviewMenu()816     private void setUpMocksForOverviewMenu() {
817         when(mOverviewModeBehavior.overviewVisible()).thenReturn(true);
818         when(mTabModelSelector.getTotalTabCount()).thenReturn(1);
819         setUpIncognitoMocks();
820     }
821 
setUpIncognitoMocks()822     private void setUpIncognitoMocks() {
823         doReturn(true).when(mAppMenuPropertiesDelegate).isIncognitoEnabled();
824     }
825 
createTestMenu()826     private Menu createTestMenu() {
827         PopupMenu tempMenu = new PopupMenu(ContextUtils.getApplicationContext(), mDecorView);
828         tempMenu.inflate(mAppMenuPropertiesDelegate.getAppMenuLayoutId());
829         Menu menu = tempMenu.getMenu();
830         return menu;
831     }
832 
assertMenuItemsAreEqual(Menu menu, Integer... expectedItems)833     private void assertMenuItemsAreEqual(Menu menu, Integer... expectedItems) {
834         List<Integer> actualItems = new ArrayList<>();
835         for (int i = 0; i < menu.size(); i++) {
836             if (menu.getItem(i).isVisible()) {
837                 actualItems.add(menu.getItem(i).getItemId());
838             }
839         }
840 
841         Assert.assertThat("Populated menu items were:" + getMenuTitles(menu), actualItems,
842                 Matchers.containsInAnyOrder(expectedItems));
843     }
844 
assertMenuTitlesAreEqual(Menu menu, Integer... expectedTitles)845     private void assertMenuTitlesAreEqual(Menu menu, Integer... expectedTitles) {
846         Context context = ContextUtils.getApplicationContext();
847         int expectedIndex = 0;
848         for (int i = 0; i < menu.size(); i++) {
849             if (menu.getItem(i).isVisible()) {
850                 Assert.assertEquals(expectedTitles[expectedIndex] == 0
851                                 ? null
852                                 : context.getString(expectedTitles[expectedIndex]),
853                         menu.getItem(i).getTitle());
854                 expectedIndex++;
855             }
856         }
857     }
858 
assertActionBarItemsAreEqual(Menu menu, Integer... expectedItems)859     private void assertActionBarItemsAreEqual(Menu menu, Integer... expectedItems) {
860         SubMenu actionBar = menu.findItem(R.id.icon_row_menu_id).getSubMenu();
861         List<Integer> actualItems = new ArrayList<>();
862         for (int i = 0; i < actionBar.size(); i++) {
863             if (actionBar.getItem(i).isVisible()) {
864                 actualItems.add(actionBar.getItem(i).getItemId());
865             }
866         }
867 
868         Assert.assertThat("Populated action bar items were:" + getMenuTitles(actionBar),
869                 actualItems, Matchers.containsInAnyOrder(expectedItems));
870     }
871 
assertMenuItemsHaveIcons(Menu menu, Integer... expectedItems)872     private void assertMenuItemsHaveIcons(Menu menu, Integer... expectedItems) {
873         List<Integer> actualItems = new ArrayList<>();
874         for (int i = 0; i < menu.size(); i++) {
875             if (menu.getItem(i).isVisible() && menu.getItem(i).getIcon() != null) {
876                 actualItems.add(menu.getItem(i).getItemId());
877             }
878         }
879 
880         Assert.assertThat("menu items with icons were:" + getMenuTitles(menu), actualItems,
881                 Matchers.containsInAnyOrder(expectedItems));
882     }
883 
assertAddToItemsAreEqual(Menu menu, Integer... expectedItems)884     private void assertAddToItemsAreEqual(Menu menu, Integer... expectedItems) {
885         SubMenu addToSubMenu = menu.findItem(R.id.add_to_menu_id).getSubMenu();
886         List<Integer> actualItems = new ArrayList<>();
887         for (int i = 0; i < addToSubMenu.size(); i++) {
888             if (addToSubMenu.getItem(i).isVisible()) {
889                 actualItems.add(addToSubMenu.getItem(i).getItemId());
890             }
891         }
892 
893         Assert.assertThat("Populated add to items were:" + getMenuTitles(addToSubMenu), actualItems,
894                 Matchers.containsInAnyOrder(expectedItems));
895     }
896 
getMenuTitles(Menu menu)897     private String getMenuTitles(Menu menu) {
898         StringBuilder items = new StringBuilder();
899         for (int i = 0; i < menu.size(); i++) {
900             if (menu.getItem(i).isVisible()) {
901                 items.append("\n").append(menu.getItem(i).getTitle());
902             }
903         }
904         return items.toString();
905     }
906 
setMenuOptions(boolean isNativePage, boolean showTranslate, boolean showUpdate, boolean showMoveToOtherWindow, boolean showReaderModePrefs, boolean showAddToHomeScreen, boolean showPaintPreview)907     private void setMenuOptions(boolean isNativePage, boolean showTranslate, boolean showUpdate,
908             boolean showMoveToOtherWindow, boolean showReaderModePrefs, boolean showAddToHomeScreen,
909             boolean showPaintPreview) {
910         when(mTab.getUrlString()).thenReturn("https://google.com");
911         when(mTab.isNativePage()).thenReturn(isNativePage);
912         doReturn(showTranslate)
913                 .when(mAppMenuPropertiesDelegate)
914                 .shouldShowTranslateMenuItem(any(Tab.class));
915         doReturn(showUpdate).when(mAppMenuPropertiesDelegate).shouldShowUpdateMenuItem();
916         doReturn(showMoveToOtherWindow)
917                 .when(mAppMenuPropertiesDelegate)
918                 .shouldShowMoveToOtherWindow();
919         doReturn(showReaderModePrefs)
920                 .when(mAppMenuPropertiesDelegate)
921                 .shouldShowReaderModePrefs(any(Tab.class));
922         if (showAddToHomeScreen) {
923             doReturn(new AppBannerManager.InstallStringPair(
924                              R.string.menu_add_to_homescreen, R.string.add))
925                     .when(mAppMenuPropertiesDelegate)
926                     .getAddToHomeScreenTitle(mTab);
927         }
928         doReturn(showPaintPreview)
929                 .when(mAppMenuPropertiesDelegate)
930                 .shouldShowPaintPreview(anyBoolean(), any(Tab.class), anyBoolean());
931     }
932 }
933