Commit 25af1243 authored by Rohit Agarwal's avatar Rohit Agarwal Committed by Commit Bot

Add Mockito tests for LocationBarModel#getProfile.

This CL adds unit tests for LocationBarModel#getProfile and revert the
changes in javatests/*/LocationBarModelTest before crrev/c/2409298

Bug: 1128073
Change-Id: Ib41b443bd9d40ec2559aaff972bda642d999dcda
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2410482
Commit-Queue: Rohit Agarwal <roagarwal@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807593}
parent fc11bcc0
......@@ -234,6 +234,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/tasks/EngagementTimeUtilTest.java",
"junit/src/org/chromium/chrome/browser/tasks/JourneyManagerTest.java",
"junit/src/org/chromium/chrome/browser/toolbar/LocationBarFocusScrimHandlerTest.java",
"junit/src/org/chromium/chrome/browser/toolbar/LocationBarModelTest.java",
"junit/src/org/chromium/chrome/browser/toolbar/ToolbarTabControllerImplTest.java",
"junit/src/org/chromium/chrome/browser/toolbar/load_progress/LoadProgressMediatorTest.java",
"junit/src/org/chromium/chrome/browser/toolbar/menu_button/MenuButtonCoordinatorTest.java",
......
......@@ -7,10 +7,12 @@ package org.chromium.chrome.browser.customtabs;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.CommandLine;
import org.chromium.base.UnownedUserData;
import org.chromium.base.UnownedUserDataKey;
import org.chromium.base.annotations.CheckDiscard;
import org.chromium.chrome.browser.app.ChromeActivity;
import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController;
......@@ -36,6 +38,8 @@ import javax.inject.Inject;
*/
@ActivityScope
public class CustomTabIncognitoManager implements NativeInitObserver, Destroyable, UnownedUserData {
private static CustomTabIncognitoManager sCustomTabIncognitoManagerUsedForTesting;
private static final String TAG = "CctIncognito";
/** The key for accessing this object on an {@link org.chromium.base.UnownedUserDataHost}. */
private static final UnownedUserDataKey<CustomTabIncognitoManager> KEY =
......@@ -69,6 +73,13 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
attach(mWindowAndroid, this);
}
@CheckDiscard("Test-only setter.")
@VisibleForTesting
public static void setCustomTabIncognitoManagerUsedForTesting(
CustomTabIncognitoManager customTabIncognitoManager) {
sCustomTabIncognitoManagerUsedForTesting = customTabIncognitoManager;
}
/**
* Get the Activity's {@link CustomTabIncognitoManager} from the provided {@link
* WindowAndroid}.
......@@ -76,6 +87,10 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
* @return The Activity's {@link CustomTabIncognitoManager}.
*/
public static @Nullable CustomTabIncognitoManager from(WindowAndroid window) {
if (sCustomTabIncognitoManagerUsedForTesting != null) {
return sCustomTabIncognitoManagerUsedForTesting;
}
return KEY.retrieveDataFromHost(window.getUnownedUserDataHost());
}
......
......@@ -4,45 +4,33 @@
package org.chromium.chrome.browser.toolbar;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.incognito.IncognitoDataTestUtils;
import org.chromium.chrome.browser.omnibox.UrlBarData;
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.toolbar.top.ToolbarLayout;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.TimeoutException;
/**
* Tests for LocationBarModel.
*/
......@@ -52,8 +40,10 @@ public class LocationBarModelTest {
@Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
@Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
@Before
public void setUp() throws InterruptedException {
mActivityTestRule.startMainActivityOnBlankPage();
}
/**
* After closing all {@link Tab}s, the {@link LocationBarModel} should know that it is not
......@@ -63,7 +53,6 @@ public class LocationBarModelTest {
@Feature({"Android-Toolbar"})
@MediumTest
public void testClosingLastTabReflectedInModel() {
mActivityTestRule.startMainActivityOnBlankPage();
Assert.assertNotSame("No current tab", Tab.INVALID_TAB_ID,
getCurrentTabId(mActivityTestRule.getActivity()));
ChromeTabUtils.closeCurrentTab(
......@@ -75,12 +64,10 @@ public class LocationBarModelTest {
}
@Test
@MediumTest
@SmallTest
public void testDisplayAndEditText() {
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean incognito = false;
TestLocationBarModel model = new TestLocationBarModel(getMockTab(incognito), incognito);
TestLocationBarModel model = new TestLocationBarModel();
model.mUrl = UrlConstants.NTP_URL;
assertDisplayAndEditText(model, "", null);
......@@ -101,75 +88,6 @@ public class LocationBarModelTest {
});
}
@Test
@MediumTest
@DisabledTest(message = "crbug/1128073")
public void testGetProfileOnNullTabInIncognito() {
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean incognito = true;
TestLocationBarModel model = new TestLocationBarModel(null, incognito);
Profile profile = model.getProfile();
assertTrue(profile.isPrimaryOTRProfile());
});
}
@Test
@MediumTest
@DisabledTest(message = "crbug/1128073")
public void testGetProfileOnMockTabInIncognito() {
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean incognito = true;
TestLocationBarModel model = new TestLocationBarModel(getMockTab(incognito), incognito);
Profile profile = model.getProfile();
assertTrue(profile.isPrimaryOTRProfile());
});
}
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
public void testGetProfileOnMockTabInIncognitoCCT() throws TimeoutException {
IncognitoDataTestUtils.fireAndWaitForCctWarmup();
// Create an launch an incognito CCT.
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getContext(), "about:blank");
intent.putExtra(IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, true);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean incognito = true;
// Setup LocationBarModel
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
TestLocationBarModel model = new TestLocationBarModel(tab, incognito);
Profile profile = model.getProfile();
assertFalse(profile.isPrimaryOTRProfile());
});
}
@Test
@MediumTest
@DisabledTest(message = "crbug/1128073")
public void testGetProfileOnMockTabInRegularCCT() {
// Create an launch a regular CCT.
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getContext(), "about:blank");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean incognito = true;
// Setup LocationBarModel
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
TestLocationBarModel model = new TestLocationBarModel(tab, incognito);
Profile profile = model.getProfile();
assertTrue(profile.isPrimaryOTRProfile());
});
}
private void assertDisplayAndEditText(
ToolbarDataProvider dataProvider, String displayText, String editText) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
......@@ -194,30 +112,27 @@ public class LocationBarModelTest {
return tab != null ? tab.getId() : Tab.INVALID_TAB_ID;
}
public static Tab getMockTab(boolean incognito) {
Tab tab = new MockTab(0, incognito) {
@Override
public boolean isInitialized() {
return true;
}
@Override
public boolean isFrozen() {
return false;
}
};
return tab;
}
private class TestLocationBarModel extends LocationBarModel {
private String mDisplayUrl;
private String mFullUrl;
private String mUrl;
public TestLocationBarModel(Tab tab, boolean incognito) {
public TestLocationBarModel() {
super(ContextUtils.getApplicationContext());
initializeWithNative();
setTab(tab, incognito);
Tab tab = new MockTab(0, false) {
@Override
public boolean isInitialized() {
return true;
}
@Override
public boolean isFrozen() {
return false;
}
};
setTab(tab, false);
}
@Override
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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.toolbar;
import static org.mockito.Mockito.when;
import androidx.test.filters.MediumTest;
import org.junit.After;
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.ContextUtils;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.customtabs.CustomTabIncognitoManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.ui.base.WindowAndroid;
/**
* Unit tests for the LocationBarModel.
*/
@RunWith(BaseRobolectricTestRunner.class)
public class LocationBarModelTest {
@Mock
private Tab mIncognitoTabMock;
@Mock
private Tab mRegularTabMock;
@Mock
private WindowAndroid mWindowAndroidMock;
@Mock
private CustomTabIncognitoManager mCustomTabIncognitoManagerMock;
@Mock
private Profile mRegularProfileMock;
@Mock
private Profile mPrimaryOTRProfileMock;
@Mock
private Profile mNonPrimaryOTRProfileMock;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Profile.setLastUsedProfileForTesting(mRegularProfileMock);
CustomTabIncognitoManager.setCustomTabIncognitoManagerUsedForTesting(
mCustomTabIncognitoManagerMock);
when(mCustomTabIncognitoManagerMock.getProfile()).thenReturn(mNonPrimaryOTRProfileMock);
when(mRegularProfileMock.hasPrimaryOTRProfile()).thenReturn(true);
when(mRegularProfileMock.getPrimaryOTRProfile()).thenReturn(mPrimaryOTRProfileMock);
when(mIncognitoTabMock.getWindowAndroid()).thenReturn(mWindowAndroidMock);
when(mIncognitoTabMock.isIncognito()).thenReturn(true);
}
@After
public void tearDown() {
Profile.setLastUsedProfileForTesting(null);
CustomTabIncognitoManager.setCustomTabIncognitoManagerUsedForTesting(null);
}
private static class TestIncognitoLocationBarModel extends LocationBarModel {
public TestIncognitoLocationBarModel(Tab tab) {
super(ContextUtils.getApplicationContext());
setTab(tab, /*incognito=*/true);
}
}
private static class TestRegularLocationBarModel extends LocationBarModel {
public TestRegularLocationBarModel(Tab tab) {
super(ContextUtils.getApplicationContext());
setTab(tab, /*incognito=*/false);
}
}
@Test
@MediumTest
public void getProfile_IncognitoTab_ReturnsPrimaryOTRProfile() {
when(mCustomTabIncognitoManagerMock.getProfile()).thenReturn(null);
LocationBarModel incognitoLocationBarModel =
new TestIncognitoLocationBarModel(mIncognitoTabMock);
Profile otrProfile = incognitoLocationBarModel.getProfile();
Assert.assertEquals(mPrimaryOTRProfileMock, otrProfile);
}
@Test
@MediumTest
public void getProfile_IncognitoCCT_ReturnsNonPrimaryOTRProfile() {
LocationBarModel incognitoLocationBarModel =
new TestIncognitoLocationBarModel(mIncognitoTabMock);
Profile otrProfile = incognitoLocationBarModel.getProfile();
Assert.assertEquals(mNonPrimaryOTRProfileMock, otrProfile);
}
@Test
@MediumTest
public void getProfile_NullTab_ReturnsPrimaryOTRProfile() {
LocationBarModel incognitoLocationBarModel = new TestIncognitoLocationBarModel(null);
Profile otrProfile = incognitoLocationBarModel.getProfile();
Assert.assertEquals(mPrimaryOTRProfileMock, otrProfile);
}
@Test
@MediumTest
public void getProfile_RegularTab_ReturnsRegularProfile() {
LocationBarModel regularLocationBarModel = new TestRegularLocationBarModel(mRegularTabMock);
Profile profile = regularLocationBarModel.getProfile();
Assert.assertEquals(mRegularProfileMock, profile);
}
@Test
@MediumTest
public void getProfile_NullTab_ReturnsRegularProfile() {
LocationBarModel regularLocationBarModel = new TestRegularLocationBarModel(null);
Profile profile = regularLocationBarModel.getProfile();
Assert.assertEquals(mRegularProfileMock, profile);
}
}
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