Commit 4ebe47d2 authored by Yue Zhang's avatar Yue Zhang Committed by Commit Bot

Make setRootId() persistent

Currently, changes by setRootId() in Tab are not reflected in TabState
and will be lost when tabs are restored. This CL fixes this issue.

Bug: 971327
Change-Id: Ia7daf36d4feb23f79b9c364d1232090671a7b9d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1647134
Commit-Queue: Yue Zhang <yuezhanggg@google.com>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#667239}
parent 171850d2
...@@ -178,6 +178,7 @@ chrome_junit_test_java_sources = [ ...@@ -178,6 +178,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/suggestions/tile/TileGroupUnitTest.java", "junit/src/org/chromium/chrome/browser/suggestions/tile/TileGroupUnitTest.java",
"junit/src/org/chromium/chrome/browser/survey/ChromeSurveyControllerTest.java", "junit/src/org/chromium/chrome/browser/survey/ChromeSurveyControllerTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabAttributesTest.java", "junit/src/org/chromium/chrome/browser/tab/TabAttributesTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabUnitTest.java",
"junit/src/org/chromium/chrome/browser/tabmodel/TabPersistentStoreUnitTest.java", "junit/src/org/chromium/chrome/browser/tabmodel/TabPersistentStoreUnitTest.java",
"junit/src/org/chromium/chrome/browser/tabstate/TabStateUnitTest.java", "junit/src/org/chromium/chrome/browser/tabstate/TabStateUnitTest.java",
"junit/src/org/chromium/chrome/browser/tasks/EngagementTimeUtilTest.java", "junit/src/org/chromium/chrome/browser/tasks/EngagementTimeUtilTest.java",
......
...@@ -166,4 +166,7 @@ public class EmptyTabObserver implements TabObserver { ...@@ -166,4 +166,7 @@ public class EmptyTabObserver implements TabObserver {
@Override @Override
public void onContentViewSystemUiVisibilityChanged(Tab tab, int visibility) {} public void onContentViewSystemUiVisibilityChanged(Tab tab, int visibility) {}
@Override
public void onRootIdChanged(Tab tab, int newRootId) {}
} }
...@@ -643,7 +643,12 @@ public class Tab ...@@ -643,7 +643,12 @@ public class Tab
* @param rootId New relationship id to be set. * @param rootId New relationship id to be set.
*/ */
public void setRootId(int rootId) { public void setRootId(int rootId) {
if (rootId == mRootId) return;
mRootId = rootId; mRootId = rootId;
mIsTabStateDirty = true;
for (TabObserver observer : mObservers) {
observer.onRootIdChanged(this, rootId);
}
} }
/** /**
......
...@@ -366,4 +366,10 @@ public interface TabObserver { ...@@ -366,4 +366,10 @@ public interface TabObserver {
* @see View#setSystemUiVisibility(int) * @see View#setSystemUiVisibility(int)
*/ */
void onContentViewSystemUiVisibilityChanged(Tab tab, int visibility); void onContentViewSystemUiVisibilityChanged(Tab tab, int visibility);
/**
* Called when the root Id of tab is changed.
* @param newRootId New root ID to be set.
*/
void onRootIdChanged(Tab tab, int newRootId);
} }
...@@ -203,6 +203,11 @@ public class TabModelSelectorImpl extends TabModelSelectorBase implements TabMod ...@@ -203,6 +203,11 @@ public class TabModelSelectorImpl extends TabModelSelectorBase implements TabMod
public void onCloseContents(Tab tab) { public void onCloseContents(Tab tab) {
closeTab(tab); closeTab(tab);
} }
@Override
public void onRootIdChanged(Tab tab, int newRootId) {
mTabSaver.addTabToSaveQueue(tab);
}
}; };
} }
......
// Copyright 2019 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.tab;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.content.Context;
import android.support.test.filters.SmallTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.ui.base.WindowAndroid;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
/**
* Tests for {@link Tab}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class TabUnitTest {
private static final int TAB1_ID = 456;
private static final int TAB2_ID = 789;
@Mock
private WindowAndroid mWindowAndroid;
@Mock
private LoadUrlParams mLoadUrlParams;
@Mock
private EmptyTabObserver mObserver;
@Mock
private Context mContext;
@Mock
private WeakReference<Context> mWeakReferenceContext;
@Mock
private WeakReference<Activity> mWeakReferenceActivity;
@Mock
private Activity mActivity;
private Tab mTab;
@Before
public void setUp() throws InterruptedException {
MockitoAnnotations.initMocks(this);
doReturn(mWeakReferenceActivity).when(mWindowAndroid).getActivity();
doReturn(mWeakReferenceContext).when(mWindowAndroid).getContext();
doReturn(mActivity).when(mWeakReferenceActivity).get();
doReturn(mContext).when(mWeakReferenceContext).get();
doReturn(mContext).when(mContext).getApplicationContext();
// Bypass feature checks.
Map<String, Boolean> features = new HashMap<>();
features.put("ShoppingAssist", true);
ChromeFeatureList.setTestFeatures(features);
mTab = new Tab(TAB1_ID, null, false, mWindowAndroid, null, null, mLoadUrlParams);
mTab.addObserver(mObserver);
}
@Test
@SmallTest
public void testSetRootIdWithChange() throws Exception {
assertThat(mTab.getRootId(), equalTo(TAB1_ID));
mTab.setRootId(TAB2_ID);
verify(mObserver).onRootIdChanged(mTab, TAB2_ID);
assertThat(mTab.getRootId(), equalTo(TAB2_ID));
assertThat(mTab.isTabStateDirty(), equalTo(true));
}
@Test
@SmallTest
public void testSetRootIdWithoutChange() throws Exception {
assertThat(mTab.getRootId(), equalTo(TAB1_ID));
mTab.setIsTabStateDirty(false);
mTab.setRootId(TAB1_ID);
verify(mObserver, never()).onRootIdChanged(any(Tab.class), anyInt());
assertThat(mTab.getRootId(), equalTo(TAB1_ID));
assertThat(mTab.isTabStateDirty(), equalTo(false));
}
}
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