Skip to content

Commit

Permalink
Require a Profile in MockTab.
Browse files Browse the repository at this point in the history
This enables assertions in Tab that the Profile is non-null.

Bug: 1494442
Change-Id: Ifb20fad36f63b098acf657fdf154c689c85a84ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4977739
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Calder Kitagawa <ckitagawa@chromium.org>
Auto-Submit: Ted Choc <tedchoc@chromium.org>
Commit-Queue: Calder Kitagawa <ckitagawa@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1215655}
  • Loading branch information
Ted Choc authored and Chromium LUCI CQ committed Oct 26, 2023
1 parent 0d243b7 commit e6619a9
Show file tree
Hide file tree
Showing 60 changed files with 572 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ public class StartSurfaceCoordinatorUnitTestRule implements TestRule {
private static class MockTabModelFilterProvider extends TabModelFilterProvider {
public MockTabModelFilterProvider(Activity activity) {
List<TabModel> tabModels = new ArrayList<>();
tabModels.add(new MockTabModel(false, null));
MockTabModel tabModel = new MockTabModel(true, null);
tabModel.setAsActiveModelForTesting();
tabModels.add(tabModel);
tabModels.add(new MockTabModel(Profile.getLastUsedRegularProfile(), null));
MockTabModel incognitoTabModel =
new MockTabModel(
Profile.getLastUsedRegularProfile().getPrimaryOTRProfile(true), null);
incognitoTabModel.setAsActiveModelForTesting();
tabModels.add(incognitoTabModel);

init(new ChromeTabModelFilterFactory(activity), tabModels);
}
Expand Down Expand Up @@ -168,7 +170,11 @@ public StartSurfaceCoordinator getCoordinator() {
}

private void initJniMocks() {
Profile incognitoProfile = Mockito.mock(Profile.class);
Mockito.when(incognitoProfile.isOffTheRecord()).thenReturn(true);
Profile profile = Mockito.mock(Profile.class);
Mockito.when(profile.getPrimaryOTRProfile(Mockito.anyBoolean()))
.thenReturn(incognitoProfile);
PrefService prefService = Mockito.mock(PrefService.class);
Profile.setLastUsedProfileForTesting(profile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ public void hideSingleTabSwitcherWhenCurrentSelectedTabIsNTP() {
doReturn(TabSwitcherType.SINGLE)
.when(mCarouselOrSingleTabSwitcherModuleController)
.getTabSwitcherType();
MockTab regularTab = new MockTab(1, false);
MockTab regularTab = new MockTab(1, mProfile);
regularTab.setGurlOverrideForTesting(JUnitTestGURLs.NTP_URL);
when(mTabModelSelector.getCurrentTab()).thenReturn(regularTab);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ public void setUpTest() throws Exception {
doReturn(false).when(mProfile).isOffTheRecord();
LevelDBPersistedDataStorage.setSkipNativeAssertionsForTesting(true);

// TODO(crbug/1494442): Remove when MockTab requires a Profile reference that can avoid
// TabHelpers from needing to use getLastUsedRegularProfile.
Profile.setLastUsedProfileForTesting(mProfile);
PriceTrackingFeatures.setPriceTrackingEnabledForTesting(false);

Expand Down Expand Up @@ -861,7 +859,7 @@ public void testSetCreateGroupListener() {
@MediumTest
@UiThreadTest
public void testPriceStringPriceDrop() {
Tab tab = MockTab.createAndInitialize(1, false);
Tab tab = MockTab.createAndInitialize(1, mProfile);
MockShoppingPersistedTabDataFetcher fetcher = new MockShoppingPersistedTabDataFetcher(tab);
fetcher.setPriceStrings(EXPECTED_PRICE_STRING, EXPECTED_PREVIOUS_PRICE_STRING);
testPriceString(
Expand All @@ -872,7 +870,7 @@ public void testPriceStringPriceDrop() {
@MediumTest
@UiThreadTest
public void testPriceStringNullPriceDrop() {
Tab tab = MockTab.createAndInitialize(1, false);
Tab tab = MockTab.createAndInitialize(1, mProfile);
MockShoppingPersistedTabDataFetcher fetcher = new MockShoppingPersistedTabDataFetcher(tab);
fetcher.setNullPriceDrop();
testPriceString(
Expand All @@ -883,7 +881,7 @@ public void testPriceStringNullPriceDrop() {
@MediumTest
@UiThreadTest
public void testPriceStringPriceDropThenNull() {
Tab tab = MockTab.createAndInitialize(1, false);
Tab tab = MockTab.createAndInitialize(1, mProfile);
MockShoppingPersistedTabDataFetcher fetcher = new MockShoppingPersistedTabDataFetcher(tab);
fetcher.setPriceStrings(EXPECTED_PRICE_STRING, EXPECTED_PREVIOUS_PRICE_STRING);
testPriceString(
Expand All @@ -897,7 +895,7 @@ public void testPriceStringPriceDropThenNull() {
@MediumTest
@UiThreadTest
public void testPriceStringTurnFeatureOff() {
Tab tab = MockTab.createAndInitialize(1, false);
Tab tab = MockTab.createAndInitialize(1, mProfile);
MockShoppingPersistedTabDataFetcher fetcher = new MockShoppingPersistedTabDataFetcher(tab);
fetcher.setPriceStrings(EXPECTED_PRICE_STRING, EXPECTED_PREVIOUS_PRICE_STRING);
testPriceString(
Expand Down Expand Up @@ -982,7 +980,7 @@ public void testPriceDropEndToEnd() {
mockUrlUtilities();
mockOptimizationGuideResponse(
OptimizationGuideDecision.TRUE, ANY_PRICE_TRACKING_DATA);
MockTab tab = MockTab.createAndInitialize(1, false);
MockTab tab = MockTab.createAndInitialize(1, mProfile);
tab.setGurlOverrideForTesting(TEST_GURL);
tab.setIsInitialized(true);
tab.setTimestampMillis(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.MockTab;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabImpl;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class PseudoTabUnitTest {
@Mock TabModelFilter mTabModelFilter2;
@Mock TabModelSelector mTabModelSelector;
@Mock TabModelFilterProvider mTabModelFilterProvider;
@Mock Profile mProfile;

private TabImpl mTab1;
private TabImpl mTab2;
Expand Down Expand Up @@ -413,7 +415,7 @@ public void getRelatedTabs_provider_incognito() {

@Test
public void testTabDestroyedTitle() {
Tab tab = new MockTab(TAB4_ID, false);
Tab tab = new MockTab(TAB4_ID, mProfile);
PseudoTab pseudoTab = PseudoTab.fromTab(tab);
tab.destroy();
// Title was not set. Without the isInitialized() check,
Expand All @@ -424,7 +426,7 @@ public void testTabDestroyedTitle() {

@Test
public void testTabDestroyedUrl() {
Tab tab = new MockTab(TAB4_ID, false);
Tab tab = new MockTab(TAB4_ID, mProfile);
PseudoTab pseudoTab = PseudoTab.fromTab(tab);
tab.destroy();
// Url was not set. Without the isInitialized() check,
Expand All @@ -435,7 +437,7 @@ public void testTabDestroyedUrl() {

@Test
public void testTabDestroyedRootId() {
Tab tab = new MockTab(TAB4_ID, false);
Tab tab = new MockTab(TAB4_ID, mProfile);
PseudoTab pseudoTab = PseudoTab.fromTab(tab);
tab.destroy();
// Root ID was not set. Without the isInitialized() check,
Expand All @@ -446,7 +448,7 @@ public void testTabDestroyedRootId() {

@Test
public void testTabDestroyedTimestamp() {
Tab tab = new MockTab(TAB4_ID, false);
Tab tab = new MockTab(TAB4_ID, mProfile);
PseudoTab pseudoTab = PseudoTab.fromTab(tab);
tab.destroy();
// Timestamp was not set. Without the isInitialized() check,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2990,7 +2990,7 @@ public void testUpdateFaviconFetcherForGroup_StaleIndex_Reset() {

@Test(expected = AssertionError.class)
public void testGetDomainOnDestroyedTab() {
Tab tab = new MockTab(TAB1_ID, false);
Tab tab = new MockTab(TAB1_ID, mProfile);
tab.destroy();
TabListMediator.getDomain(tab);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.bookmarks.BookmarkModel;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tasks.tab_management.TabSelectionEditorAction.ActionDelegate;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class TabSelectionEditorBookmarkActionUnitTest {
@Mock private ActionDelegate mDelegate;
@Mock private SnackbarManager mSnackbarManager;
@Mock private BookmarkModel mBookmarkModel;
@Mock private Profile mProfile;

@Mock
private TabSelectionEditorBookmarkActionDelegate mTabSelectionEditorBookmarkActionDelegate;
Expand All @@ -77,7 +79,7 @@ public void setUp() {
(TabSelectionEditorBookmarkAction)
TabSelectionEditorBookmarkAction.createAction(
mActivity, ShowMode.MENU_ONLY, ButtonType.TEXT, IconPosition.START);
mTabModel = spy(new MockTabModel(false, null));
mTabModel = spy(new MockTabModel(mProfile, null));
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
mAction.configure(mTabModelSelector, mSelectionDelegate, mDelegate, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelFilterProvider;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class TabSelectionEditorCloseActionUnitTest {
@Mock private TabGroupModelFilter mGroupFilter;
@Mock private SelectionDelegate<Integer> mSelectionDelegate;
@Mock private ActionDelegate mDelegate;
@Mock private Profile mProfile;
private MockTabModel mTabModel;
private TabSelectionEditorAction mAction;

Expand All @@ -69,7 +71,7 @@ public void setUp() {
ShowMode.MENU_ONLY,
ButtonType.TEXT,
IconPosition.START);
mTabModel = spy(new MockTabModel(false, null));
mTabModel = spy(new MockTabModel(mProfile, null));
when(mTabModelFilterProvider.getCurrentTabModelFilter()).thenReturn(mGroupFilter);
when(mTabModelSelector.getTabModelFilterProvider()).thenReturn(mTabModelFilterProvider);
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelFilterProvider;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class TabSelectionEditorGroupActionUnitTest {
@Mock private TabModelFilterProvider mTabModelFilterProvider;
@Mock private TabGroupModelFilter mGroupFilter;
@Mock private ActionDelegate mDelegate;
@Mock private Profile mProfile;
private MockTabModel mTabModel;
private TabSelectionEditorAction mAction;

Expand All @@ -68,7 +70,7 @@ public void setUp() {
ShowMode.MENU_ONLY,
ButtonType.TEXT,
IconPosition.START);
mTabModel = spy(new MockTabModel(false, null));
mTabModel = spy(new MockTabModel(mProfile, null));
when(mTabModelFilterProvider.getCurrentTabModelFilter()).thenReturn(mGroupFilter);
when(mTabModelSelector.getTabModelFilterProvider()).thenReturn(mTabModelFilterProvider);
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.robolectric.annotation.Config;

import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tasks.tab_management.TabSelectionEditorAction.ActionDelegate;
import org.chromium.chrome.browser.tasks.tab_management.TabSelectionEditorAction.ButtonType;
Expand All @@ -43,6 +44,7 @@ public class TabSelectionEditorSelectionActionUnitTest {
@Mock private TabModelSelector mTabModelSelector;
@Mock private SelectionDelegate<Integer> mSelectionDelegate;
@Mock private ActionDelegate mDelegate;
@Mock private Profile mProfile;
private MockTabModel mTabModel;
private TabSelectionEditorAction mAction;
private Context mContext;
Expand All @@ -55,7 +57,7 @@ public void setUp() {
mAction =
TabSelectionEditorSelectionAction.createAction(
mContext, ShowMode.IF_ROOM, ButtonType.ICON_AND_TEXT, IconPosition.END);
mTabModel = spy(new MockTabModel(false, null));
mTabModel = spy(new MockTabModel(mProfile, null));
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
// TODO(ckitagawa): Add tests for when this is true.
mAction.configure(mTabModelSelector, mSelectionDelegate, mDelegate, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.MockTab;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelFilterProvider;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class TabSelectionEditorShareActionUnitTest {
@Mock private SelectionDelegate<Integer> mSelectionDelegate;
@Mock private ActionDelegate mDelegate;
@Mock private DomDistillerUrlUtilsJni mDomDistillerUrlUtilsJni;
@Mock private Profile mProfile;
@Mock private Profile mIncognitoProfile;
private Context mContext;
private MockTabModel mTabModel;
private TabSelectionEditorShareAction mAction;
Expand Down Expand Up @@ -99,15 +102,17 @@ public void setUp() {
mTabModel =
spy(
new MockTabModel(
false,
mProfile,
new MockTabModel.MockTabModelDelegate() {
@Override
public MockTab createTab(int id, boolean incognito) {
MockTab tab = new MockTab(id, incognito);
Profile profile = incognito ? mIncognitoProfile : mProfile;
MockTab tab = new MockTab(id, profile);
tab.setGurlOverrideForTesting(mIdUrlMap.get(id));
return tab;
}
}));
when(mIncognitoProfile.isOffTheRecord()).thenReturn(true);
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
when(mTabModelSelector.getTabModelFilterProvider()).thenReturn(mTabModelFilterProvider);
when(mTabModelFilterProvider.getTabModelFilter(false)).thenReturn(mTabModelFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelFilterProvider;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class TabSelectionEditorUngroupActionUnitTest {
@Mock private TabModelFilterProvider mTabModelFilterProvider;
@Mock private TabGroupModelFilter mGroupFilter;
@Mock private ActionDelegate mDelegate;
@Mock private Profile mProfile;
private MockTabModel mTabModel;
private TabSelectionEditorAction mAction;

Expand All @@ -66,7 +68,7 @@ public void setUp() {
ShowMode.MENU_ONLY,
ButtonType.TEXT,
IconPosition.START);
mTabModel = spy(new MockTabModel(false, null));
mTabModel = spy(new MockTabModel(mProfile, null));
when(mTabModelFilterProvider.getCurrentTabModelFilter()).thenReturn(mGroupFilter);
when(mTabModelSelector.getTabModelFilterProvider()).thenReturn(mTabModelFilterProvider);
when(mTabModelSelector.getCurrentModel()).thenReturn(mTabModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setUp() {
@Test
@SmallTest
public void testGetTabsSearchableDataProto() throws InterruptedException {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);
for (int i = 0; i < 200; i++) {
MockTab tab = mockTabModel.addTab(i);
tab.setGurlOverrideForTesting(new GURL(TAB_URL + Integer.toString(i)));
Expand Down Expand Up @@ -153,7 +153,7 @@ public void testGetBookmarksSearchableDataProto_failureToParse() {
@Test
@SmallTest
public void testTabHasNullTitle() {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);

// Add a normal tab
MockTab tab = mockTabModel.addTab(0);
Expand All @@ -180,7 +180,7 @@ public void testTabHasNullTitle() {
@Test
@SmallTest
public void testTabHasEmptyTitle() {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);

// Add a normal tab
MockTab tab = mockTabModel.addTab(0);
Expand All @@ -207,7 +207,7 @@ public void testTabHasEmptyTitle() {
@Test
@SmallTest
public void testTabHasNullUrl() {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);

// Add a normal tab
MockTab tab = mockTabModel.addTab(0);
Expand All @@ -234,7 +234,7 @@ public void testTabHasNullUrl() {
@Test
@SmallTest
public void testTabHasInvalidlUrl() {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);

// Add a normal tab
MockTab tab = mockTabModel.addTab(0);
Expand All @@ -261,7 +261,7 @@ public void testTabHasInvalidlUrl() {
@Test
@SmallTest
public void testGetTabsSearchableDataProtoAsync() {
MockTabModel mockTabModel = new MockTabModel(false, null);
MockTabModel mockTabModel = new MockTabModel(mProfile, null);
ArrayList<Tab> tabList = new ArrayList<>();
// Create 200 tabs with different timestamps(from 0 to 199), and only the newest 100 tabs
// should be returned from 'getTabsSearchableDataProtoAsync'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.chromium.chrome.browser.media.ui.MediaSessionTabHelper;
import org.chromium.chrome.browser.password_check.PasswordCheckUkmRecorder;
import org.chromium.chrome.browser.price_tracking.PriceTrackingFeatures;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.state.ShoppingPersistedTabData;

/**
Expand Down Expand Up @@ -45,10 +44,9 @@ static void initTabHelpers(Tab tab, Tab parentTab) {
// we should only do it if the user is eligible for the feature (e.g. has sync enabled).
if (!tab.isIncognito()
&& !((TabImpl) tab).isCustomTab()
&& PriceTrackingFeatures.isPriceTrackingEligible(
Profile.getLastUsedRegularProfile())
&& PriceTrackingFeatures.isPriceTrackingEligible(tab.getProfile())
&& ShoppingPersistedTabData.isPriceTrackingWithOptimizationGuideEnabled(
Profile.getLastUsedRegularProfile())) {
tab.getProfile())) {
ShoppingPersistedTabData.initialize(tab);
}
}
Expand Down

0 comments on commit e6619a9

Please sign in to comment.