1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/toolbar/recent_tabs_builder_test_helper.h"
7 #include "base/rand_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/sync/sessions/sessions_sync_manager.h"
12 #include "components/sync_driver/open_tabs_ui_delegate.h"
13 #include "sync/api/attachments/attachment_id.h"
14 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
15 #include "sync/protocol/session_specifics.pb.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 const char kBaseSessionTag
[] = "session_tag";
21 const char kBaseSessionName
[] = "session_name";
22 const char kBaseTabUrl
[] = "http://foo/?";
23 const char kTabTitleFormat
[] = "session=%d;window=%d;tab=%d";
25 struct TitleTimestampPair
{
30 bool SortTabTimesByRecency(const TitleTimestampPair
& t1
,
31 const TitleTimestampPair
& t2
) {
32 return t1
.timestamp
> t2
.timestamp
;
35 int CreateUniqueID() {
41 std::string
ToSessionTag(SessionID::id_type session_id
) {
42 return std::string(kBaseSessionTag
+ base::IntToString(session_id
));
45 std::string
ToSessionName(SessionID::id_type session_id
) {
46 return std::string(kBaseSessionName
+ base::IntToString(session_id
));
49 std::string
ToTabTitle(SessionID::id_type session_id
,
50 SessionID::id_type window_id
,
51 SessionID::id_type tab_id
) {
52 return base::StringPrintf(kTabTitleFormat
, session_id
, window_id
, tab_id
);
55 std::string
ToTabUrl(SessionID::id_type session_id
,
56 SessionID::id_type window_id
,
57 SessionID::id_type tab_id
) {
58 return std::string(kBaseTabUrl
+ ToTabTitle(session_id
, window_id
, tab_id
));
63 struct RecentTabsBuilderTestHelper::TabInfo
{
65 SessionID::id_type id
;
69 struct RecentTabsBuilderTestHelper::WindowInfo
{
70 WindowInfo() : id(0) {}
72 SessionID::id_type id
;
73 std::vector
<TabInfo
> tabs
;
75 struct RecentTabsBuilderTestHelper::SessionInfo
{
76 SessionInfo() : id(0) {}
78 SessionID::id_type id
;
79 std::vector
<WindowInfo
> windows
;
82 RecentTabsBuilderTestHelper::RecentTabsBuilderTestHelper()
83 : max_tab_node_id_(0) {
84 start_time_
= base::Time::Now();
87 RecentTabsBuilderTestHelper::~RecentTabsBuilderTestHelper() {
90 void RecentTabsBuilderTestHelper::AddSession() {
92 info
.id
= CreateUniqueID();
93 sessions_
.push_back(info
);
96 int RecentTabsBuilderTestHelper::GetSessionCount() {
97 return sessions_
.size();
100 SessionID::id_type
RecentTabsBuilderTestHelper::GetSessionID(
102 return sessions_
[session_index
].id
;
105 base::Time
RecentTabsBuilderTestHelper::GetSessionTimestamp(int session_index
) {
106 std::vector
<base::Time
> timestamps
;
107 for (int w
= 0; w
< GetWindowCount(session_index
); ++w
) {
108 for (int t
= 0; t
< GetTabCount(session_index
, w
); ++t
)
109 timestamps
.push_back(GetTabTimestamp(session_index
, w
, t
));
112 if (timestamps
.empty())
113 return base::Time::Now();
115 sort(timestamps
.begin(), timestamps
.end());
116 return timestamps
[0];
119 void RecentTabsBuilderTestHelper::AddWindow(int session_index
) {
120 WindowInfo window_info
;
121 window_info
.id
= CreateUniqueID();
122 sessions_
[session_index
].windows
.push_back(window_info
);
125 int RecentTabsBuilderTestHelper::GetWindowCount(int session_index
) {
126 return sessions_
[session_index
].windows
.size();
129 SessionID::id_type
RecentTabsBuilderTestHelper::GetWindowID(int session_index
,
131 return sessions_
[session_index
].windows
[window_index
].id
;
134 void RecentTabsBuilderTestHelper::AddTab(int session_index
, int window_index
) {
135 base::Time timestamp
=
136 start_time_
+ base::TimeDelta::FromMinutes(base::RandUint64());
137 AddTabWithInfo(session_index
, window_index
, timestamp
, base::string16());
140 void RecentTabsBuilderTestHelper::AddTabWithInfo(int session_index
,
142 base::Time timestamp
,
143 const base::string16
& title
) {
145 tab_info
.id
= CreateUniqueID();
146 tab_info
.timestamp
= timestamp
;
147 tab_info
.title
= title
;
148 sessions_
[session_index
].windows
[window_index
].tabs
.push_back(tab_info
);
151 int RecentTabsBuilderTestHelper::GetTabCount(int session_index
,
153 return sessions_
[session_index
].windows
[window_index
].tabs
.size();
156 SessionID::id_type
RecentTabsBuilderTestHelper::GetTabID(int session_index
,
159 return sessions_
[session_index
].windows
[window_index
].tabs
[tab_index
].id
;
162 base::Time
RecentTabsBuilderTestHelper::GetTabTimestamp(int session_index
,
165 return sessions_
[session_index
].windows
[window_index
]
166 .tabs
[tab_index
].timestamp
;
169 base::string16
RecentTabsBuilderTestHelper::GetTabTitle(int session_index
,
172 base::string16 title
=
173 sessions_
[session_index
].windows
[window_index
].tabs
[tab_index
].title
;
175 title
= base::UTF8ToUTF16(ToTabTitle(
176 GetSessionID(session_index
),
177 GetWindowID(session_index
, window_index
),
178 GetTabID(session_index
, window_index
, tab_index
)));
183 void RecentTabsBuilderTestHelper::ExportToSessionsSyncManager(
184 browser_sync::SessionsSyncManager
* manager
) {
185 syncer::SyncChangeList changes
;
186 for (int s
= 0; s
< GetSessionCount(); ++s
) {
187 sync_pb::EntitySpecifics session_entity
;
188 sync_pb::SessionSpecifics
* meta
= session_entity
.mutable_session();
189 BuildSessionSpecifics(s
, meta
);
190 for (int w
= 0; w
< GetWindowCount(s
); ++w
) {
191 BuildWindowSpecifics(s
, w
, meta
);
192 for (int t
= 0; t
< GetTabCount(s
, w
); ++t
) {
193 sync_pb::EntitySpecifics entity
;
194 sync_pb::SessionSpecifics
* tab_base
= entity
.mutable_session();
195 BuildTabSpecifics(s
, w
, t
, tab_base
);
196 changes
.push_back(syncer::SyncChange(
198 syncer::SyncChange::ACTION_ADD
,
199 syncer::SyncData::CreateRemoteData(
200 tab_base
->tab_node_id(),
202 GetTabTimestamp(s
, w
, t
),
203 syncer::AttachmentIdList(),
204 syncer::AttachmentServiceProxyForTest::Create())));
207 changes
.push_back(syncer::SyncChange(
209 syncer::SyncChange::ACTION_ADD
,
210 syncer::SyncData::CreateRemoteData(
213 GetSessionTimestamp(s
),
214 syncer::AttachmentIdList(),
215 syncer::AttachmentServiceProxyForTest::Create())));
217 manager
->ProcessSyncChanges(FROM_HERE
, changes
);
218 VerifyExport(manager
);
221 void RecentTabsBuilderTestHelper::VerifyExport(
222 sync_driver::OpenTabsUIDelegate
* delegate
) {
223 // Make sure data is populated correctly in SessionModelAssociator.
224 std::vector
<const sync_driver::SyncedSession
*> sessions
;
225 ASSERT_TRUE(delegate
->GetAllForeignSessions(&sessions
));
226 ASSERT_EQ(GetSessionCount(), static_cast<int>(sessions
.size()));
227 for (int s
= 0; s
< GetSessionCount(); ++s
) {
228 std::vector
<const sessions::SessionWindow
*> windows
;
229 ASSERT_TRUE(delegate
->GetForeignSession(ToSessionTag(GetSessionID(s
)),
231 ASSERT_EQ(GetWindowCount(s
), static_cast<int>(windows
.size()));
232 for (int w
= 0; w
< GetWindowCount(s
); ++w
)
233 ASSERT_EQ(GetTabCount(s
, w
), static_cast<int>(windows
[w
]->tabs
.size()));
237 std::vector
<base::string16
>
238 RecentTabsBuilderTestHelper::GetTabTitlesSortedByRecency() {
239 std::vector
<TitleTimestampPair
> tabs
;
240 for (int s
= 0; s
< GetSessionCount(); ++s
) {
241 for (int w
= 0; w
< GetWindowCount(s
); ++w
) {
242 for (int t
= 0; t
< GetTabCount(s
, w
); ++t
) {
243 TitleTimestampPair pair
;
244 pair
.title
= GetTabTitle(s
, w
, t
);
245 pair
.timestamp
= GetTabTimestamp(s
, w
, t
);
246 tabs
.push_back(pair
);
250 sort(tabs
.begin(), tabs
.end(), SortTabTimesByRecency
);
252 std::vector
<base::string16
> titles
;
253 for (size_t i
= 0; i
< tabs
.size(); ++i
)
254 titles
.push_back(tabs
[i
].title
);
258 void RecentTabsBuilderTestHelper::BuildSessionSpecifics(
260 sync_pb::SessionSpecifics
* meta
) {
261 SessionID::id_type session_id
= GetSessionID(session_index
);
262 meta
->set_session_tag(ToSessionTag(session_id
));
263 sync_pb::SessionHeader
* header
= meta
->mutable_header();
264 header
->set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_CROS
);
265 header
->set_client_name(ToSessionName(session_id
));
268 void RecentTabsBuilderTestHelper::BuildWindowSpecifics(
271 sync_pb::SessionSpecifics
* meta
) {
272 sync_pb::SessionHeader
* header
= meta
->mutable_header();
273 sync_pb::SessionWindow
* window
= header
->add_window();
274 SessionID::id_type window_id
= GetWindowID(session_index
, window_index
);
275 window
->set_window_id(window_id
);
276 window
->set_selected_tab_index(0);
277 window
->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED
);
278 for (int i
= 0; i
< GetTabCount(session_index
, window_index
); ++i
)
279 window
->add_tab(GetTabID(session_index
, window_index
, i
));
282 void RecentTabsBuilderTestHelper::BuildTabSpecifics(
286 sync_pb::SessionSpecifics
* tab_base
) {
287 SessionID::id_type session_id
= GetSessionID(session_index
);
288 SessionID::id_type window_id
= GetWindowID(session_index
, window_index
);
289 SessionID::id_type tab_id
= GetTabID(session_index
, window_index
, tab_index
);
291 tab_base
->set_session_tag(ToSessionTag(session_id
));
292 tab_base
->set_tab_node_id(++max_tab_node_id_
);
293 sync_pb::SessionTab
* tab
= tab_base
->mutable_tab();
294 tab
->set_window_id(window_id
);
295 tab
->set_tab_id(tab_id
);
296 tab
->set_tab_visual_index(1);
297 tab
->set_current_navigation_index(0);
298 tab
->set_pinned(true);
299 tab
->set_extension_app_id("app_id");
300 sync_pb::TabNavigation
* navigation
= tab
->add_navigation();
301 navigation
->set_virtual_url(ToTabUrl(session_id
, window_id
, tab_id
));
302 navigation
->set_referrer("referrer");
303 navigation
->set_title(base::UTF16ToUTF8(GetTabTitle(
304 session_index
, window_index
, tab_index
)));
305 navigation
->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED
);