Commit adfbf01a authored by Side Yilmaz's avatar Side Yilmaz Committed by Commit Bot

Clean null profile checks at TabModelSelectorProfileSupplier.

This CL removes the unnecessary profile checks for OTR profile, since
IncognitoTabModelImpl guarantees to return non null profile for
getProfile function with the recent change. Force profile creation is
also not needed, since IncognitoTabModelImpl forces for the creation now.

Bug: 1140892, 1060940, 1126532
Change-Id: Ie07c15399b3270d3b268213f84fc4a96431cd3b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2494889
Commit-Queue: Side YILMAZ <sideyilmaz@chromium.org>
Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821199}
parent 1c74b754
......@@ -18,6 +18,7 @@ import org.chromium.chrome.browser.tab.Tab;
public class TabModelSelectorProfileSupplier
extends ObservableSupplierImpl<Profile> implements TabModelSelectorObserver {
private TabModelSelector mSelector;
private boolean mIsTabStateInitialized;
public TabModelSelectorProfileSupplier(ObservableSupplier<TabModelSelector> selectorSupplier) {
selectorSupplier.addObserver(this::setSelector);
......@@ -31,21 +32,10 @@ public class TabModelSelectorProfileSupplier
@Override
public void onTabModelSelected(TabModel newModel, TabModel oldModel) {
Profile newProfile = newModel.getProfile();
// When switching to an incognito tab model, the corresponding off-the-record profile does
// not necessarily exist yet, but we may be able to force its creation.
if (newProfile == null && newModel.isIncognito()) {
Profile oldProfile = oldModel.getProfile();
assert oldProfile != null;
// If the previous profile is itself off-the-record, we can't derive an
// off-the-record profile from it.
if (oldProfile.isOffTheRecord()) return;
// Forces creation of a primary off-the-record profile. TODO(pnoland): replace this with
// getIncognitoProfile() once multiple OTR profiles are supported on Android.
newProfile = oldProfile.getOffTheRecordProfile();
}
assert !mIsTabStateInitialized || newProfile != null;
// Postpone setting the profile until tab state is initialized.
if (newProfile == null) return;
set(newProfile);
}
......@@ -57,14 +47,9 @@ public class TabModelSelectorProfileSupplier
@Override
public void onTabStateInitialized() {
mIsTabStateInitialized = true;
Profile profile = mSelector.getCurrentModel().getProfile();
if (profile == null) {
// Since only IncognitoTabModelImpl provides null profile, the current model should be
// off-the-record.
assert mSelector.getCurrentModel().isIncognito();
// TODO(https://crbug.com/1060940): Update to cover all OTR profiles.
profile = Profile.getLastUsedRegularProfile().getPrimaryOTRProfile();
}
assert profile != null;
set(profile);
}
......
......@@ -89,15 +89,21 @@ public class TabModelSelectorProfileSupplierTest {
}
@Test
public void tesIncognitoProfileForceCreated() {
doReturn(null).when(mIncognitoTabModel).getProfile();
doReturn(mProfile).when(mTabModel).getProfile();
doReturn(mIncognitoProfile).when(mProfile).getOffTheRecordProfile();
public void tesOTRProfileReturnsForIncognitoTabModel() {
doReturn(mIncognitoProfile).when(mIncognitoTabModel).getProfile();
mSupplier.onTabModelSelected(mIncognitoTabModel, mTabModel);
Assert.assertEquals(mIncognitoProfile, mSupplier.get());
}
@Test
public void tesRegularProfileReturnsForRegularTabModel() {
doReturn(mProfile).when(mTabModel).getProfile();
mSupplier.onTabModelSelected(mTabModel, mIncognitoTabModel);
Assert.assertEquals(mProfile, mSupplier.get());
}
@Test
public void testDestroy() {
mSupplier.destroy();
......
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