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

Move getTimestampMillis to CriticalPersistedTabData

This supports the effort of moving Tab attributes to UserData objects

Bug: 1109036
Change-Id: Iafb008decac9e8f214d8c86f2d88e76a5b415cbc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2316803
Commit-Queue: David Maunder <davidjm@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#793764}
parent 1fa9e57f
......@@ -208,7 +208,7 @@ public class PseudoTab {
public long getTimestampMillis() {
assert mTab != null
&& mTab.get() != null : "getTimestampMillis can only be used with real tabs";
return mTab.get().getTimestampMillis();
return CriticalPersistedTabData.from(mTab.get()).getTimestampMillis();
}
/**
......
......@@ -110,7 +110,8 @@ public class TabContext {
String referrerUrl = getReferrerUrlFromTab(tab);
return new TabInfo(tab.getId(), tab.getTitle(), tab.getUrlString(),
tab.getOriginalUrl(), referrerUrl != null ? referrerUrl : "",
tab.getTimestampMillis(), tab.getUrlString(), tab.isIncognito());
CriticalPersistedTabData.from(tab).getTimestampMillis(), tab.getUrlString(),
tab.isIncognito());
}
public double getSiteEngagementScore() {
......
......@@ -306,8 +306,9 @@ public class PseudoTabUnitTest {
@Test
public void getTimestampMillis_realTab() {
CriticalPersistedTabData criticalPersistedTabaData = CriticalPersistedTabData.from(mTab1);
long timestamp = 12345;
doReturn(timestamp).when(mTab1).getTimestampMillis();
doReturn(timestamp).when(criticalPersistedTabaData).getTimestampMillis();
PseudoTab tab = PseudoTab.fromTab(mTab1);
Assert.assertEquals(timestamp, tab.getTimestampMillis());
......
......@@ -1296,8 +1296,10 @@ public class TabListMediatorUnitTest {
long timestamp1 = 1;
long timestamp2 = 2;
doReturn(timestamp1).when(mTab1).getTimestampMillis();
doReturn(timestamp2).when(mTab2).getTimestampMillis();
CriticalPersistedTabData criticalPersistedTabData1 = CriticalPersistedTabData.from(mTab1);
CriticalPersistedTabData criticalPersistedTabData2 = CriticalPersistedTabData.from(mTab2);
doReturn(timestamp1).when(criticalPersistedTabData1).getTimestampMillis();
doReturn(timestamp2).when(criticalPersistedTabData2).getTimestampMillis();
mMediator.resetWithListOfTabs(
PseudoTab.getListOfPseudoTab(tabs), /*quickMode =*/false, /*mruMode =*/true);
......@@ -1307,8 +1309,8 @@ public class TabListMediatorUnitTest {
assertThat(mMediator.indexOfTab(TAB1_ID), equalTo(1));
assertThat(mMediator.indexOfTab(TAB2_ID), equalTo(0));
doReturn(timestamp2).when(mTab1).getTimestampMillis();
doReturn(timestamp1).when(mTab2).getTimestampMillis();
doReturn(timestamp2).when(criticalPersistedTabData1).getTimestampMillis();
doReturn(timestamp1).when(criticalPersistedTabData2).getTimestampMillis();
mMediator.resetWithListOfTabs(
PseudoTab.getListOfPseudoTab(tabs), /*quickMode =*/false, /*mruMode =*/true);
......
......@@ -31,6 +31,8 @@ public class TabUiUnitTestUtils {
public static TabImpl prepareTab(int tabId) {
TabImpl tab = prepareTab();
doReturn(tabId).when(tab).getId();
UserDataHost userDataHost = new UserDataHost();
doReturn(userDataHost).when(tab).getUserDataHost();
return tab;
}
......
......@@ -53,7 +53,7 @@ public class TabContextTest {
private static final int NEW_TAB_2_ID = 4;
private static final int LAST_COMMITTED_INDEX = 1;
private static final String TAB_CONTEXT_TAB_0_JSON = "[{\"id\":0,\"url\":"
+ "\"mock_url_tab_0\",\"title\":\"mock_title_tab_0\",\"timestamp\":0,"
+ "\"mock_url_tab_0\",\"title\":\"mock_title_tab_0\",\"timestamp\":100,"
+ "\"referrer\":\"mock_referrer_url_tab_0\"}]";
@Rule
......@@ -92,7 +92,7 @@ public class TabContextTest {
}
private static TabImpl mockTab(int id, int rootId, String title, String url, String originalUrl,
String referrerUrl, long getTimestampMillis) {
String referrerUrl, long timestampMillis) {
TabImpl tab = mock(TabImpl.class);
doReturn(id).when(tab).getId();
UserDataHost userDataHost = new UserDataHost();
......@@ -116,6 +116,7 @@ public class TabContextTest {
.when(navigationController)
.getEntryAtIndex(eq(LAST_COMMITTED_INDEX));
doReturn(referrerUrl).when(navigationEntry).getReferrerUrl();
doReturn(timestampMillis).when(criticalPersistedTabData).getTimestampMillis();
return tab;
}
......@@ -207,7 +208,8 @@ public class TabContextTest {
Assert.assertNotNull(tabInfo);
Assert.assertEquals(mTab0.getId(), tabInfo.id);
Assert.assertEquals(mTab0.getUrlString(), tabInfo.url);
Assert.assertEquals(mTab0.getTimestampMillis(), tabInfo.timestampMillis);
Assert.assertEquals(
CriticalPersistedTabData.from(mTab0).getTimestampMillis(), tabInfo.timestampMillis);
Assert.assertEquals(mTab0.getTitle(), tabInfo.title);
NavigationEntry lastCommittedEntry =
mTab0.getWebContents().getNavigationController().getEntryAtIndex(
......
......@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowProcess;
import org.chromium.base.Callback;
import org.chromium.base.test.util.InMemorySharedPreferences;
......@@ -35,6 +36,7 @@ import org.chromium.chrome.browser.tabmodel.TabModelFilter;
import org.chromium.chrome.browser.tabmodel.TabModelFilterProvider;
import org.chromium.chrome.browser.tabmodel.TabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tasks.tab_management.TabUiUnitTestUtils;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.WebContents;
import org.chromium.testing.local.LocalRobolectricTestRunner;
......@@ -49,7 +51,7 @@ import java.util.List;
*/
@SuppressWarnings({"ResultOfMethodCallIgnored", "ArraysAsListWithZeroOrOneArgument"})
@RunWith(LocalRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@Config(manifest = Config.NONE, shadows = ShadowProcess.class)
public class TabSuggestionsOrchestratorTest {
private static final int[] TAB_IDS = {0, 1, 2, 3, 4};
private static final String GROUPING_PROVIDER = "groupingProvider";
......@@ -76,12 +78,10 @@ public class TabSuggestionsOrchestratorTest {
@Mock
private ActivityLifecycleDispatcher mDispatcher;
private static Tab[] sTabs = {mockTab(TAB_IDS[0]), mockTab(TAB_IDS[1]), mockTab(TAB_IDS[2]),
mockTab(TAB_IDS[3]), mockTab(TAB_IDS[4])};
private static Tab[] sTabs = new Tab[TAB_IDS.length];
private static Tab mockTab(int id) {
TabImpl tab = mock(TabImpl.class);
doReturn(id).when(tab).getId();
TabImpl tab = TabUiUnitTestUtils.prepareTab(id);
WebContents webContents = mock(WebContents.class);
GURL gurl = mock(GURL.class);
doReturn("").when(gurl).getSpec();
......@@ -93,6 +93,10 @@ public class TabSuggestionsOrchestratorTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
ShadowProcess.reset();
for (int i = 0; i < sTabs.length; i++) {
sTabs[i] = mockTab(TAB_IDS[i]);
}
mocker.mock(ProfileJni.TEST_HOOKS, mMockProfileNatives);
doReturn(mTabModelFilterProvider).when(mTabModelSelector).getTabModelFilterProvider();
doNothing()
......
......@@ -173,11 +173,6 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
/** Whether the renderer is currently unresponsive. */
private boolean mIsRendererUnresponsive;
/**
* The last time this tab was shown or the time of its initialization if it wasn't yet shown.
*/
private long mTimestampMillis = INVALID_TIMESTAMP;
/**
* Title of the ContentViews webpage.
*/
......@@ -420,11 +415,6 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
return mLaunchTypeAtCreation;
}
@Override
public long getTimestampMillis() {
return mTimestampMillis;
}
@Override
public boolean isIncognito() {
return mIncognito;
......@@ -630,7 +620,7 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
// Updating the timestamp has to happen after the showInternal() call since subclasses
// may use it for logging.
mTimestampMillis = System.currentTimeMillis();
CriticalPersistedTabData.from(this).setTimestampMillis(System.currentTimeMillis());
} finally {
TraceEvent.end("Tab.show");
}
......@@ -821,8 +811,8 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
}
} finally {
if (mTimestampMillis == INVALID_TIMESTAMP) {
mTimestampMillis = System.currentTimeMillis();
if (CriticalPersistedTabData.from(this).getTimestampMillis() == INVALID_TIMESTAMP) {
CriticalPersistedTabData.from(this).setTimestampMillis(System.currentTimeMillis());
}
String appId = tabState != null ? tabState.openerAppId : null;
Boolean hasThemeColor = tabState != null ? tabState.hasThemeColor() : null;
......@@ -847,7 +837,7 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
void restoreFieldsFromState(TabState state) {
assert state != null;
CriticalPersistedTabData.from(this).setWebContentsState(state.contentsState);
mTimestampMillis = state.timestampMillis;
CriticalPersistedTabData.from(this).setTimestampMillis(state.timestampMillis);
mUrl = new GURL(state.contentsState.getVirtualUrlFromState());
mTitle = state.contentsState.getDisplayTitleFromState();
mLaunchTypeAtCreation = state.tabLaunchTypeAtCreation;
......
......@@ -25,7 +25,7 @@ public class TabStateExtractor {
tabState.contentsState = getWebContentsState(tabImpl);
tabState.openerAppId = TabAssociatedApp.getAppId(tab);
tabState.parentId = CriticalPersistedTabData.from(tab).getParentId();
tabState.timestampMillis = tab.getTimestampMillis();
tabState.timestampMillis = CriticalPersistedTabData.from(tab).getTimestampMillis();
tabState.tabLaunchTypeAtCreation = tab.getLaunchTypeAtInitialTabCreation();
// Don't save the actual default theme color because it could change on night mode state
// changed.
......
......@@ -9,6 +9,7 @@ import android.text.format.DateUtils;
import org.chromium.base.UserData;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.chrome.browser.tab.state.CriticalPersistedTabData;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.net.NetError;
......@@ -162,7 +163,7 @@ public class TabUma extends EmptyTabObserver implements UserData {
@Override
public void onShown(Tab tab, @TabSelectionType int selectionType) {
long previousTimestampMillis = tab.getTimestampMillis();
long previousTimestampMillis = CriticalPersistedTabData.from(tab).getTimestampMillis();
long now = SystemClock.elapsedRealtime();
// Do not collect the tab switching data for the first switch to a tab after the cold start
......
......@@ -164,12 +164,6 @@ public interface Tab extends TabLifecycle {
@TabLaunchType
Integer getLaunchTypeAtInitialTabCreation();
/**
* @return the last time this tab was shown or the time of its initialization if it wasn't yet
* shown.
*/
long getTimestampMillis();
/**
* @return {@code true} if the Tab is in incognito mode.
*/
......
......@@ -31,6 +31,7 @@ public class CriticalPersistedTabData extends PersistedTabData {
CriticalPersistedTabData.class;
private static final int UNSPECIFIED_THEME_COLOR = Color.TRANSPARENT;
private static final long INVALID_TIMESTAMP = -1;
private int mParentId;
private int mRootId;
......@@ -140,7 +141,7 @@ public class CriticalPersistedTabData extends PersistedTabData {
// CriticalPersistedTabData is initialized with default values
CriticalPersistedTabData criticalPersistedTabData =
new CriticalPersistedTabData(tab, Tab.INVALID_TAB_ID, tab.getId(),
tab.getTimestampMillis(), null, -1, "", UNSPECIFIED_THEME_COLOR,
INVALID_TIMESTAMP, null, -1, "", UNSPECIFIED_THEME_COLOR,
tab.getLaunchTypeAtInitialTabCreation() == null
? TabLaunchType.FROM_LINK
: tab.getLaunchTypeAtInitialTabCreation(),
......@@ -335,6 +336,14 @@ public class CriticalPersistedTabData extends PersistedTabData {
return mTimestampMillis;
}
/**
* set the timetsamp
* @param timestamp the timestamp
*/
public void setTimestampMillis(long timestamp) {
mTimestampMillis = timestamp;
}
/**
* @return content state bytes for the {@link Tab}
*/
......
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