Commit a507b1a7 authored by David Maunder's avatar David Maunder Committed by Commit Bot

Fix acquisition of root id on PseudoTab

Following moving rootId to a UserData object
(CriticalPersistedTabData) it is now necessary to check if
the Tab is destroyed before attempting to access root id
from the CriticalPersistedTabData object. Previously root
id was available right up until the object no longer
existed.

Bug: 1097009
Change-Id: I78ca61cdff2ccc3275a48425a76514790c59daac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2257500Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: David Maunder <davidjm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781382}
parent 8e8ed621
...@@ -180,7 +180,7 @@ public class PseudoTab { ...@@ -180,7 +180,7 @@ public class PseudoTab {
* @return The root ID * @return The root ID
*/ */
public int getRootId() { public int getRootId() {
if (mTab != null && mTab.get() != null) { if (mTab != null && mTab.get() != null && mTab.get().isInitialized()) {
return CriticalPersistedTabData.from(mTab.get()).getRootId(); return CriticalPersistedTabData.from(mTab.get()).getRootId();
} }
assert mTabId != null; assert mTabId != null;
......
...@@ -20,6 +20,7 @@ import org.mockito.MockitoAnnotations; ...@@ -20,6 +20,7 @@ import org.mockito.MockitoAnnotations;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.tab.MockTab;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabImpl; import org.chromium.chrome.browser.tab.TabImpl;
import org.chromium.chrome.browser.tab.state.CriticalPersistedTabData; import org.chromium.chrome.browser.tab.state.CriticalPersistedTabData;
...@@ -52,6 +53,7 @@ public class PseudoTabUnitTest { ...@@ -52,6 +53,7 @@ public class PseudoTabUnitTest {
private static final int TAB1_ID = 456; private static final int TAB1_ID = 456;
private static final int TAB2_ID = 789; private static final int TAB2_ID = 789;
private static final int TAB3_ID = 123; private static final int TAB3_ID = 123;
private static final int TAB4_ID = 159;
@Mock @Mock
TabModelFilter mTabModelFilter; TabModelFilter mTabModelFilter;
...@@ -423,4 +425,14 @@ public class PseudoTabUnitTest { ...@@ -423,4 +425,14 @@ public class PseudoTabUnitTest {
Assert.assertEquals(TAB2_ID, related.get(1).getId()); Assert.assertEquals(TAB2_ID, related.get(1).getId());
} }
@Test
public void testTabDestroyed() {
Tab tab = new MockTab(TAB4_ID, false);
PseudoTab pseudoTab = PseudoTab.fromTab(tab);
tab.destroy();
// Root ID was not set. Without the isInitialized() check,
// pseudoTab.getRootId() would crash here with
// UnsupportedOperationException
Assert.assertEquals(Tab.INVALID_TAB_ID, pseudoTab.getRootId());
}
} }
...@@ -21,7 +21,9 @@ import org.chromium.url.GURL; ...@@ -21,7 +21,9 @@ import org.chromium.url.GURL;
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public class TabUiUnitTestUtils { public class TabUiUnitTestUtils {
public static TabImpl prepareTab() { public static TabImpl prepareTab() {
return mock(TabImpl.class); TabImpl tab = mock(TabImpl.class);
doReturn(true).when(tab).isInitialized();
return tab;
} }
public static TabImpl prepareTab(int tabId) { public static TabImpl prepareTab(int tabId) {
......
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