1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// Copyright (c) 2012 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.
#include "chrome/browser/ui/toolbar/recent_tabs_builder_test_helper.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/sync/open_tabs_ui_delegate.h"
#include "chrome/browser/sync/sessions/sessions_sync_manager.h"
#include "sync/api/attachments/attachment_id.h"
#include "sync/api/attachments/attachment_service_proxy_for_test.h"
#include "sync/protocol/session_specifics.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kBaseSessionTag[] = "session_tag";
const char kBaseSessionName[] = "session_name";
const char kBaseTabUrl[] = "http://foo/?";
const char kTabTitleFormat[] = "session=%d;window=%d;tab=%d";
struct TitleTimestampPair {
base::string16 title;
base::Time timestamp;
};
bool SortTabTimesByRecency(const TitleTimestampPair& t1,
const TitleTimestampPair& t2) {
return t1.timestamp > t2.timestamp;
}
int CreateUniqueID() {
static int s_id = 0;
++s_id;
return s_id;
}
std::string ToSessionTag(SessionID::id_type session_id) {
return std::string(kBaseSessionTag + base::IntToString(session_id));
}
std::string ToSessionName(SessionID::id_type session_id) {
return std::string(kBaseSessionName + base::IntToString(session_id));
}
std::string ToTabTitle(SessionID::id_type session_id,
SessionID::id_type window_id,
SessionID::id_type tab_id) {
return base::StringPrintf(kTabTitleFormat, session_id, window_id, tab_id);
}
std::string ToTabUrl(SessionID::id_type session_id,
SessionID::id_type window_id,
SessionID::id_type tab_id) {
return std::string(kBaseTabUrl + ToTabTitle(session_id, window_id, tab_id));
}
} // namespace
struct RecentTabsBuilderTestHelper::TabInfo {
TabInfo() : id(0) {}
SessionID::id_type id;
base::Time timestamp;
base::string16 title;
};
struct RecentTabsBuilderTestHelper::WindowInfo {
WindowInfo() : id(0) {}
~WindowInfo() {}
SessionID::id_type id;
std::vector<TabInfo> tabs;
};
struct RecentTabsBuilderTestHelper::SessionInfo {
SessionInfo() : id(0) {}
~SessionInfo() {}
SessionID::id_type id;
std::vector<WindowInfo> windows;
};
RecentTabsBuilderTestHelper::RecentTabsBuilderTestHelper()
: max_tab_node_id_(0) {
start_time_ = base::Time::Now();
}
RecentTabsBuilderTestHelper::~RecentTabsBuilderTestHelper() {
}
void RecentTabsBuilderTestHelper::AddSession() {
SessionInfo info;
info.id = CreateUniqueID();
sessions_.push_back(info);
}
int RecentTabsBuilderTestHelper::GetSessionCount() {
return sessions_.size();
}
SessionID::id_type RecentTabsBuilderTestHelper::GetSessionID(
int session_index) {
return sessions_[session_index].id;
}
base::Time RecentTabsBuilderTestHelper::GetSessionTimestamp(int session_index) {
std::vector<base::Time> timestamps;
for (int w = 0; w < GetWindowCount(session_index); ++w) {
for (int t = 0; t < GetTabCount(session_index, w); ++t)
timestamps.push_back(GetTabTimestamp(session_index, w, t));
}
if (timestamps.empty())
return base::Time::Now();
sort(timestamps.begin(), timestamps.end());
return timestamps[0];
}
void RecentTabsBuilderTestHelper::AddWindow(int session_index) {
WindowInfo window_info;
window_info.id = CreateUniqueID();
sessions_[session_index].windows.push_back(window_info);
}
int RecentTabsBuilderTestHelper::GetWindowCount(int session_index) {
return sessions_[session_index].windows.size();
}
SessionID::id_type RecentTabsBuilderTestHelper::GetWindowID(int session_index,
int window_index) {
return sessions_[session_index].windows[window_index].id;
}
void RecentTabsBuilderTestHelper::AddTab(int session_index, int window_index) {
base::Time timestamp =
start_time_ + base::TimeDelta::FromMinutes(base::RandUint64());
AddTabWithInfo(session_index, window_index, timestamp, base::string16());
}
void RecentTabsBuilderTestHelper::AddTabWithInfo(int session_index,
int window_index,
base::Time timestamp,
const base::string16& title) {
TabInfo tab_info;
tab_info.id = CreateUniqueID();
tab_info.timestamp = timestamp;
tab_info.title = title;
sessions_[session_index].windows[window_index].tabs.push_back(tab_info);
}
int RecentTabsBuilderTestHelper::GetTabCount(int session_index,
int window_index) {
return sessions_[session_index].windows[window_index].tabs.size();
}
SessionID::id_type RecentTabsBuilderTestHelper::GetTabID(int session_index,
int window_index,
int tab_index) {
return sessions_[session_index].windows[window_index].tabs[tab_index].id;
}
base::Time RecentTabsBuilderTestHelper::GetTabTimestamp(int session_index,
int window_index,
int tab_index) {
return sessions_[session_index].windows[window_index]
.tabs[tab_index].timestamp;
}
base::string16 RecentTabsBuilderTestHelper::GetTabTitle(int session_index,
int window_index,
int tab_index) {
base::string16 title =
sessions_[session_index].windows[window_index].tabs[tab_index].title;
if (title.empty()) {
title = base::UTF8ToUTF16(ToTabTitle(
GetSessionID(session_index),
GetWindowID(session_index, window_index),
GetTabID(session_index, window_index, tab_index)));
}
return title;
}
void RecentTabsBuilderTestHelper::ExportToSessionsSyncManager(
browser_sync::SessionsSyncManager* manager) {
syncer::SyncChangeList changes;
for (int s = 0; s < GetSessionCount(); ++s) {
sync_pb::EntitySpecifics session_entity;
sync_pb::SessionSpecifics* meta = session_entity.mutable_session();
BuildSessionSpecifics(s, meta);
for (int w = 0; w < GetWindowCount(s); ++w) {
BuildWindowSpecifics(s, w, meta);
for (int t = 0; t < GetTabCount(s, w); ++t) {
sync_pb::EntitySpecifics entity;
sync_pb::SessionSpecifics* tab_base = entity.mutable_session();
BuildTabSpecifics(s, w, t, tab_base);
changes.push_back(syncer::SyncChange(
FROM_HERE,
syncer::SyncChange::ACTION_ADD,
syncer::SyncData::CreateRemoteData(
tab_base->tab_node_id(),
entity,
GetTabTimestamp(s, w, t),
syncer::AttachmentIdList(),
syncer::AttachmentServiceProxyForTest::Create())));
}
}
changes.push_back(syncer::SyncChange(
FROM_HERE,
syncer::SyncChange::ACTION_ADD,
syncer::SyncData::CreateRemoteData(
1,
session_entity,
GetSessionTimestamp(s),
syncer::AttachmentIdList(),
syncer::AttachmentServiceProxyForTest::Create())));
}
manager->ProcessSyncChanges(FROM_HERE, changes);
VerifyExport(manager);
}
void RecentTabsBuilderTestHelper::VerifyExport(
browser_sync::OpenTabsUIDelegate* delegate) {
// Make sure data is populated correctly in SessionModelAssociator.
std::vector<const browser_sync::SyncedSession*> sessions;
ASSERT_TRUE(delegate->GetAllForeignSessions(&sessions));
ASSERT_EQ(GetSessionCount(), static_cast<int>(sessions.size()));
for (int s = 0; s < GetSessionCount(); ++s) {
std::vector<const SessionWindow*> windows;
ASSERT_TRUE(delegate->GetForeignSession(ToSessionTag(GetSessionID(s)),
&windows));
ASSERT_EQ(GetWindowCount(s), static_cast<int>(windows.size()));
for (int w = 0; w < GetWindowCount(s); ++w)
ASSERT_EQ(GetTabCount(s, w), static_cast<int>(windows[w]->tabs.size()));
}
}
std::vector<base::string16>
RecentTabsBuilderTestHelper::GetTabTitlesSortedByRecency() {
std::vector<TitleTimestampPair> tabs;
for (int s = 0; s < GetSessionCount(); ++s) {
for (int w = 0; w < GetWindowCount(s); ++w) {
for (int t = 0; t < GetTabCount(s, w); ++t) {
TitleTimestampPair pair;
pair.title = GetTabTitle(s, w, t);
pair.timestamp = GetTabTimestamp(s, w, t);
tabs.push_back(pair);
}
}
}
sort(tabs.begin(), tabs.end(), SortTabTimesByRecency);
std::vector<base::string16> titles;
for (size_t i = 0; i < tabs.size(); ++i)
titles.push_back(tabs[i].title);
return titles;
}
void RecentTabsBuilderTestHelper::BuildSessionSpecifics(
int session_index,
sync_pb::SessionSpecifics* meta) {
SessionID::id_type session_id = GetSessionID(session_index);
meta->set_session_tag(ToSessionTag(session_id));
sync_pb::SessionHeader* header = meta->mutable_header();
header->set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_CROS);
header->set_client_name(ToSessionName(session_id));
}
void RecentTabsBuilderTestHelper::BuildWindowSpecifics(
int session_index,
int window_index,
sync_pb::SessionSpecifics* meta) {
sync_pb::SessionHeader* header = meta->mutable_header();
sync_pb::SessionWindow* window = header->add_window();
SessionID::id_type window_id = GetWindowID(session_index, window_index);
window->set_window_id(window_id);
window->set_selected_tab_index(0);
window->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED);
for (int i = 0; i < GetTabCount(session_index, window_index); ++i)
window->add_tab(GetTabID(session_index, window_index, i));
}
void RecentTabsBuilderTestHelper::BuildTabSpecifics(
int session_index,
int window_index,
int tab_index,
sync_pb::SessionSpecifics* tab_base) {
SessionID::id_type session_id = GetSessionID(session_index);
SessionID::id_type window_id = GetWindowID(session_index, window_index);
SessionID::id_type tab_id = GetTabID(session_index, window_index, tab_index);
tab_base->set_session_tag(ToSessionTag(session_id));
tab_base->set_tab_node_id(++max_tab_node_id_);
sync_pb::SessionTab* tab = tab_base->mutable_tab();
tab->set_window_id(window_id);
tab->set_tab_id(tab_id);
tab->set_tab_visual_index(1);
tab->set_current_navigation_index(0);
tab->set_pinned(true);
tab->set_extension_app_id("app_id");
sync_pb::TabNavigation* navigation = tab->add_navigation();
navigation->set_virtual_url(ToTabUrl(session_id, window_id, tab_id));
navigation->set_referrer("referrer");
navigation->set_title(base::UTF16ToUTF8(GetTabTitle(
session_index, window_index, tab_index)));
navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
}
|