Commit 8c8ca818 authored by Mikel Astiz's avatar Mikel Astiz Committed by Commit Bot

Add integration tests for session gc

When a foreign session is detected to be old enough, garbage collection
should kick in and all sync entities deleted. This includes orphan tabs
as well (i.e. tabs not referenced from the header entity because the tab
was closed).

Bug: 882489
Change-Id: I9f6e0e4505cc798466294f1fa1a247818cf004b9
Reviewed-on: https://chromium-review.googlesource.com/c/1356717
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613068}
parent a0c670c2
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
#include "components/sync/base/time.h" #include "components/sync/base/time.h"
#include "components/sync/protocol/proto_value_conversions.h" #include "components/sync/protocol/proto_value_conversions.h"
#include "components/sync/test/fake_server/sessions_hierarchy.h" #include "components/sync/test/fake_server/sessions_hierarchy.h"
#include "components/sync_sessions/session_store.h"
#include "components/sync_sessions/session_sync_service.h" #include "components/sync_sessions/session_sync_service.h"
#include "components/sync_sessions/session_sync_test_helper.h"
#include "components/sync_sessions/synced_session_tracker.h" #include "components/sync_sessions/synced_session_tracker.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/mojo/window_open_disposition.mojom.h" #include "ui/base/mojo/window_open_disposition.mojom.h"
...@@ -51,6 +53,7 @@ using sessions_helper::SessionWindowMap; ...@@ -51,6 +53,7 @@ using sessions_helper::SessionWindowMap;
using sessions_helper::SyncedSessionVector; using sessions_helper::SyncedSessionVector;
using sessions_helper::WaitForTabsToLoad; using sessions_helper::WaitForTabsToLoad;
using sessions_helper::WindowsMatch; using sessions_helper::WindowsMatch;
using sync_sessions::SessionSyncTestHelper;
using typed_urls_helper::GetUrlFromClient; using typed_urls_helper::GetUrlFromClient;
static const char* kURL1 = "data:text/html,<html><title>Test</title></html>"; static const char* kURL1 = "data:text/html,<html><title>Test</title></html>";
...@@ -401,6 +404,59 @@ IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, OpenNewWindow) { ...@@ -401,6 +404,59 @@ IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, OpenNewWindow) {
SessionsHierarchy({{base_url.spec()}, {new_window_url.spec()}})); SessionsHierarchy({{base_url.spec()}, {new_window_url.spec()}}));
} }
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
GarbageCollectionOfForeignSessions) {
const std::string kForeignSessionTag = "ForeignSessionTag";
const SessionID kWindowId = SessionID::FromSerializedValue(5);
const SessionID kTabId1 = SessionID::FromSerializedValue(1);
const SessionID kTabId2 = SessionID::FromSerializedValue(2);
const base::Time kLastModifiedTime =
base::Time::Now() - base::TimeDelta::FromDays(100);
SessionSyncTestHelper helper;
sync_pb::EntitySpecifics tab1;
*tab1.mutable_session() =
helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId1);
sync_pb::EntitySpecifics tab2;
*tab2.mutable_session() =
helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId2);
// |tab2| is orphan, i.e. not referenced by the header. We do this to verify
// that such tabs are also subject to garbage collection.
sync_pb::EntitySpecifics header;
SessionSyncTestHelper::BuildSessionSpecifics(kForeignSessionTag,
header.mutable_session());
SessionSyncTestHelper::AddWindowSpecifics(kWindowId, {kTabId1},
header.mutable_session());
for (const sync_pb::EntitySpecifics& specifics : {tab1, tab2, header}) {
GetFakeServer()->InjectEntity(
syncer::PersistentUniqueClientEntity::CreateFromEntitySpecifics(
sync_sessions::SessionStore::GetClientTag(specifics.session()),
specifics,
/*creation_time=*/syncer::TimeToProtoTime(kLastModifiedTime),
/*last_modified_time=*/syncer::TimeToProtoTime(kLastModifiedTime)));
}
base::HistogramTester histogram_tester;
ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
// Verify that all entities have been deleted.
WaitForHierarchyOnServer(SessionsHierarchy());
std::vector<sync_pb::SyncEntity> entities =
fake_server_->GetSyncEntitiesByModelType(syncer::SESSIONS);
for (const sync_pb::SyncEntity& entity : entities) {
EXPECT_NE(kForeignSessionTag, entity.specifics().session().session_tag());
}
EXPECT_EQ(
3, histogram_tester.GetBucketCount("Sync.ModelTypeEntityChange3.SESSION",
/*LOCAL_DELETION=*/0));
}
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, TabMovedToOtherWindow) { IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, TabMovedToOtherWindow) {
ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
ASSERT_TRUE(CheckInitialState(0)); ASSERT_TRUE(CheckInitialState(0));
......
...@@ -88,23 +88,22 @@ void SessionSyncTestHelper::VerifySyncedSession( ...@@ -88,23 +88,22 @@ void SessionSyncTestHelper::VerifySyncedSession(
} }
} }
void SessionSyncTestHelper::BuildTabSpecifics( sync_pb::SessionSpecifics SessionSyncTestHelper::BuildTabSpecifics(
const std::string& tag, const std::string& tag,
SessionID window_id, SessionID window_id,
SessionID tab_id, SessionID tab_id) {
sync_pb::SessionSpecifics* tab_base) { return BuildTabSpecifics(tag, window_id, tab_id, ++max_tab_node_id_);
BuildTabSpecifics(tag, window_id, tab_id, ++max_tab_node_id_, tab_base);
} }
void SessionSyncTestHelper::BuildTabSpecifics( sync_pb::SessionSpecifics SessionSyncTestHelper::BuildTabSpecifics(
const std::string& tag, const std::string& tag,
SessionID window_id, SessionID window_id,
SessionID tab_id, SessionID tab_id,
int tab_node_id, int tab_node_id) {
sync_pb::SessionSpecifics* tab_base) { sync_pb::SessionSpecifics specifics;
tab_base->set_session_tag(tag); specifics.set_session_tag(tag);
tab_base->set_tab_node_id(tab_node_id); specifics.set_tab_node_id(tab_node_id);
sync_pb::SessionTab* tab = tab_base->mutable_tab(); sync_pb::SessionTab* tab = specifics.mutable_tab();
tab->set_tab_id(tab_id.id()); tab->set_tab_id(tab_id.id());
tab->set_tab_visual_index(1); tab->set_tab_visual_index(1);
tab->set_current_navigation_index(0); tab->set_current_navigation_index(0);
...@@ -115,6 +114,7 @@ void SessionSyncTestHelper::BuildTabSpecifics( ...@@ -115,6 +114,7 @@ void SessionSyncTestHelper::BuildTabSpecifics(
navigation->set_referrer(kReferrer); navigation->set_referrer(kReferrer);
navigation->set_title(kTitle); navigation->set_title(kTitle);
navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED); navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
return specifics;
} }
void SessionSyncTestHelper::Reset() { void SessionSyncTestHelper::Reset() {
...@@ -125,19 +125,19 @@ sync_pb::SessionSpecifics SessionSyncTestHelper::BuildForeignSession( ...@@ -125,19 +125,19 @@ sync_pb::SessionSpecifics SessionSyncTestHelper::BuildForeignSession(
const std::string& tag, const std::string& tag,
const std::vector<SessionID>& tab_list, const std::vector<SessionID>& tab_list,
std::vector<sync_pb::SessionSpecifics>* tabs) { std::vector<sync_pb::SessionSpecifics>* tabs) {
sync_pb::SessionSpecifics meta; const SessionID window_id = SessionID::FromSerializedValue(1);
BuildSessionSpecifics(tag, &meta); sync_pb::SessionSpecifics header;
AddWindowSpecifics(SessionID::FromSerializedValue(1), tab_list, &meta); BuildSessionSpecifics(tag, &header);
std::vector<sync_pb::SessionSpecifics> tabs1; AddWindowSpecifics(window_id, tab_list, &header);
tabs1.resize(tab_list.size());
for (size_t i = 0; i < tab_list.size(); ++i) { if (tabs) {
BuildTabSpecifics(tag, SessionID::FromSerializedValue(1), tab_list[i], tabs->clear();
&tabs1[i]); for (SessionID tab_id : tab_list) {
tabs->push_back(BuildTabSpecifics(tag, window_id, tab_id));
}
} }
if (tabs) return header;
tabs->swap(tabs1);
return meta;
} }
} // namespace sync_sessions } // namespace sync_sessions
...@@ -38,18 +38,16 @@ class SessionSyncTestHelper { ...@@ -38,18 +38,16 @@ class SessionSyncTestHelper {
// Build a SessionSpecifics object with a tab and sample data. Uses a // Build a SessionSpecifics object with a tab and sample data. Uses a
// monotonically increasing variable to generate tab_node_ids and avoid // monotonically increasing variable to generate tab_node_ids and avoid
// conflicts. // conflicts.
void BuildTabSpecifics(const std::string& tag, sync_pb::SessionSpecifics BuildTabSpecifics(const std::string& tag,
SessionID window_id, SessionID window_id,
SessionID tab_id, SessionID tab_id);
sync_pb::SessionSpecifics* tab_base);
// Overload of BuildTabSpecifics to allow forcing a specific tab_node_id. // Overload of BuildTabSpecifics to allow forcing a specific tab_node_id.
// Typically only useful to test reusing tab_node_ids. // Typically only useful to test reusing tab_node_ids.
void BuildTabSpecifics(const std::string& tag, sync_pb::SessionSpecifics BuildTabSpecifics(const std::string& tag,
SessionID window_id, SessionID window_id,
SessionID tab_id, SessionID tab_id,
int tab_node_id, int tab_node_id);
sync_pb::SessionSpecifics* tab_base);
sync_pb::SessionSpecifics BuildForeignSession( sync_pb::SessionSpecifics BuildForeignSession(
const std::string& tag, const std::string& tag,
......
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