Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: chrome/browser/ui/toolbar/recent_tabs_sub_menu_model_unittest.cc

Issue 11298004: alternate ntp: add "Recent Tabs" submenu to wrench menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed disposition, minor style cleanup Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
6
7 #include "base/rand_util.h"
8 #include "base/string_number_conversions.h"
9 #include "base/stringprintf.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/sessions/session_types.h"
12 #include "chrome/browser/sessions/session_types_test_helper.h"
13 #include "chrome/browser/sync/glue/session_model_associator.h"
14 #include "chrome/browser/sync/glue/synced_session.h"
15 #include "chrome/browser/sync/profile_sync_service_mock.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/test/base/browser_with_test_window_test.h"
19 #include "chrome/test/base/menu_model_test.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "grit/generated_resources.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace {
26
27 const char kBaseSessionTag[] = "session_tag";
28 const char kBaseSessionName[] = "session_name";
29 const char kBaseTabUrl[] = "http://foo/?";
30 const char kTabTitleFormat[] = "session=%d;window=%d;tab=%d";
31
32 struct TabTime {
33 TabTime(SessionID::id_type session_id,
34 SessionID::id_type window_id,
35 int tab_idx,
36 base::Time timestamp)
37 : session_id(session_id),
38 window_id(window_id),
39 tab_idx(tab_idx),
40 timestamp(timestamp) {}
41
42 SessionID::id_type session_id;
43 SessionID::id_type window_id;
44 int tab_idx;
45 base::Time timestamp;
46 };
47
48 bool SortTabTimesByRecency(const TabTime& t1, const TabTime& t2) {
49 return t1.timestamp > t2.timestamp;
50 }
51
52 std::string ToSessionTag(SessionID::id_type session_id) {
53 return std::string(kBaseSessionTag + base::IntToString(session_id));
54 }
55
56 std::string ToSessionName(SessionID::id_type session_id) {
57 return std::string(kBaseSessionName + base::IntToString(session_id));
58 }
59
60 std::string ToTabTitle(SessionID::id_type session_id,
61 SessionID::id_type window_id,
62 int tab_idx) {
63 return base::StringPrintf(kTabTitleFormat, session_id, window_id, tab_idx);
64 }
65
66 std::string ToTabUrl(SessionID::id_type session_id,
67 SessionID::id_type window_id,
68 int tab_idx) {
69 return std::string(kBaseTabUrl + ToTabTitle(session_id, window_id, tab_idx));
70 }
71
72 // Copies parts of MenuModelTest::Delegate and combines them with the
73 // RecentTabsSubMenuModel since RecentTabsSubMenuModel is a
74 // SimpleMenuModel::Delegate and not just derived from SimpleMenuModel.
75 class TestRecentTabsSubMenuModel : public RecentTabsSubMenuModel {
76 public:
77 TestRecentTabsSubMenuModel(ui::AcceleratorProvider* provider,
78 Browser* browser,
79 bool can_restore_tab)
80 : RecentTabsSubMenuModel(provider, browser),
81 can_restore_tab_(can_restore_tab),
82 execute_count_(0),
83 enable_count_(0) {
84 }
85
86 TestRecentTabsSubMenuModel(ui::AcceleratorProvider* provider,
87 Browser* browser,
88 browser_sync::SessionModelAssociator* associator,
89 bool can_restore_tab)
90 : RecentTabsSubMenuModel(provider, browser, associator, true),
91 can_restore_tab_(can_restore_tab),
92 execute_count_(0),
93 enable_count_(0) {
94 }
95
96 // Testing overrides to ui::SimpleMenuModel::Delegate:
97 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
98 bool val = command_id == IDC_RESTORE_TAB ? can_restore_tab_ :
99 RecentTabsSubMenuModel::IsCommandIdEnabled(command_id);
100 if (val)
101 ++enable_count_;
102 return val;
103 }
104
105 virtual void ExecuteCommand(int command_id) OVERRIDE {
106 ++execute_count_;
107 }
108
109 bool can_restore_tab_;
110 int execute_count_;
111 int mutable enable_count_; // Mutable because IsCommandIdEnabledAt is const.
112 };
113
114 } // namespace
115
116 namespace browser_sync {
117
118 class SessionModelAssociatorForRecentTabsSubMenuModelTest {
119 public:
120 SessionModelAssociatorForRecentTabsSubMenuModelTest()
121 : sync_service_(&profile_),
122 model_associator_(&sync_service_, true) {}
123
124 void InitWindowPb(int num_tabs, SessionID::id_type* tab_id,
125 sync_pb::SessionWindow* window_pb) {
126 for (int i = 0; i < num_tabs; ++i)
127 window_pb->add_tab((*tab_id)++);
128 window_pb->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED);
129 window_pb->set_selected_tab_index(0);
130 }
131
132 void CreateSession(SessionID::id_type session_id) {
133 SyncedSession* session = GetSession(session_id);
134 session->device_type = SyncedSession::TYPE_CHROMEOS;
135 session->session_name = ToSessionName(session_id);
136 }
137
138 void PutWindowInSessionAndTracker(SessionID::id_type session_id,
139 SessionID::id_type window_id,
140 const sync_pb::SessionWindow& window_pb) {
141 tracker().PutWindowInSession(ToSessionTag(session_id), window_id);
142 SyncedSession* session = GetSession(session_id);
143 SessionWindow* window = session->windows[window_id];
144 SessionModelAssociator::PopulateSessionWindowFromSpecifics(
145 ToSessionTag(session_id), window_pb, base::Time(), window, &tracker());
146 ASSERT_EQ(static_cast<size_t>(window_pb.tab_size()), window->tabs.size());
147 }
148
149 void MakeTabSyncable(SessionID::id_type session_id,
150 SessionID::id_type window_id,
151 int tab_idx,
152 base::Time timestamp) {
153 SyncedSession* session = GetSession(session_id);
154 SessionTab* tab = session->windows[window_id]->tabs[tab_idx];
155 tab->navigations.push_back(SessionTypesTestHelper::CreateNavigation(
156 ToTabUrl(session_id, window_id, tab_idx),
157 ToTabTitle(session_id, window_id, tab_idx)));
158 tab->current_navigation_index = 0;
159 tab->timestamp = timestamp;
160 session->modified_time = timestamp;
161 }
162
163 SyncedSession* GetSession(SessionID::id_type session_id) {
164 return tracker().GetSession(ToSessionTag(session_id));
165 }
166
167 SyncedSessionTracker& tracker() {
168 return model_associator_.synced_session_tracker_;
169 }
170
171 SessionModelAssociator& model_associator() {
172 return model_associator_;
173 }
174
175 private:
176 TestingProfile profile_;
177 testing::NiceMock<ProfileSyncServiceMock> sync_service_;
178 SessionModelAssociator model_associator_;
179 };
180
181 } // namespace browser_sync
182
183 class RecentTabsSubMenuModelTest : public BrowserWithTestWindowTest,
184 public ui::AcceleratorProvider {
185 public:
186 // Don't handle accelerators.
187 virtual bool GetAcceleratorForCommandId(
188 int command_id,
189 ui::Accelerator* accelerator) OVERRIDE {
190 return false;
191 }
192 };
193
194 TEST_F(RecentTabsSubMenuModelTest, NoTabs) {
195 TestRecentTabsSubMenuModel model(this, browser(), false);
196
197 // Expected menu:
198 // Menu index Menu items
199 // --------------------------------------
200 // 0 Reopen closed tab
201 // 1 <separator>
202 // 2 No tabs from other Devices
203
204 int num_items = model.GetItemCount();
205 EXPECT_EQ(3, num_items);
206 EXPECT_FALSE(model.IsEnabledAt(0));
207 EXPECT_FALSE(model.IsEnabledAt(2));
208 EXPECT_EQ(0, model.enable_count_);
209 }
210
211 TEST_F(RecentTabsSubMenuModelTest, ReopenClosedTab) {
212 TestRecentTabsSubMenuModel model(this, browser(), true);
213 // Menu contents are identical to NoTabs test.
214 int num_items = model.GetItemCount();
215 EXPECT_EQ(3, num_items);
216 EXPECT_TRUE(model.IsEnabledAt(0));
217 model.ActivatedAt(0);
218 EXPECT_FALSE(model.IsEnabledAt(2));
219 EXPECT_EQ(1, model.enable_count_);
220 EXPECT_EQ(1, model.execute_count_);
221 }
222
223 TEST_F(RecentTabsSubMenuModelTest, OtherDevices) {
224 browser_sync::SessionModelAssociatorForRecentTabsSubMenuModelTest associator;
225 SessionID::id_type id = 0;
226 // Tabs are populated in decreasing timestamp.
227 base::Time now = base::Time::Now();
228
229 // Create 1st session : 1 window, 3 tabs
230 SessionID::id_type session_id = id++;
231 associator.CreateSession(session_id);
232 SessionID::id_type window_id = id++;
233 sync_pb::SessionWindow window_pb;
234 associator.InitWindowPb(3, &id, &window_pb);
235 associator.PutWindowInSessionAndTracker(session_id, window_id, window_pb);
236 base::Time timestamp = now - base::TimeDelta::FromMinutes(window_id * 10);
237 associator.MakeTabSyncable(session_id, window_id, 0, timestamp);
238 associator.MakeTabSyncable(session_id, window_id, 1,
239 timestamp - base::TimeDelta::FromMinutes(1));
240 associator.MakeTabSyncable(session_id, window_id, 2,
241 timestamp - base::TimeDelta::FromMinutes(2));
242 ASSERT_EQ(3U, associator.tracker().num_synced_tabs(ToSessionTag(session_id)));
243
244 // Create 2nd session : 2 windows, 1 tab in 1st window, 2 tabs in 2nd window
245 session_id = id++;
246 associator.CreateSession(session_id);
247 window_id = id++;
248 sync_pb::SessionWindow window_pb0;
249 associator.InitWindowPb(1, &id, &window_pb0);
250 associator.PutWindowInSessionAndTracker(session_id, window_id, window_pb0);
251 timestamp = now - base::TimeDelta::FromMinutes(window_id * 10);
252 associator.MakeTabSyncable(session_id, window_id, 0, timestamp);
253 ASSERT_EQ(1U, associator.tracker().num_synced_tabs(ToSessionTag(session_id)));
254 window_id = id++;
255 sync_pb::SessionWindow window_pb1;
256 associator.InitWindowPb(2, &id, &window_pb1);
257 associator.PutWindowInSessionAndTracker(session_id, window_id, window_pb1);
258 timestamp = now - base::TimeDelta::FromMinutes(window_id * 10);
259 associator.MakeTabSyncable(session_id, window_id, 0, timestamp);
260 associator.MakeTabSyncable(session_id, window_id, 1,
261 timestamp - base::TimeDelta::FromMinutes(1));
262 ASSERT_EQ(3U, associator.tracker().num_synced_tabs(ToSessionTag(session_id)));
263
264 ASSERT_EQ(2U, associator.tracker().num_synced_sessions());
265
266 TestRecentTabsSubMenuModel model(this, browser(),
267 &associator.model_associator(), true);
268
269 // Expected menu:
270 // - first inserted tab is most recent and hence is top
271 // Menu index Menu items
272 // --------------------------------------
273 // 0 Reopen closed tab
274 // 1 <separator>
275 // 2 <section header for 1st session>
276 // 3-5 <3 tabs of the only window of session 0>
277 // 6 <separator>
278 // 7 <section header for 2nd session>
279 // 8 <the only tab of window 0 of session 1>
280 // 9-10 <2 tabs of window 1 of session 2>
281
282 int num_items = model.GetItemCount();
283 EXPECT_EQ(11, num_items);
284 model.ActivatedAt(0);
285 EXPECT_TRUE(model.IsEnabledAt(0));
286 model.ActivatedAt(3);
287 EXPECT_TRUE(model.IsEnabledAt(3));
288 model.ActivatedAt(4);
289 EXPECT_TRUE(model.IsEnabledAt(4));
290 model.ActivatedAt(5);
291 EXPECT_TRUE(model.IsEnabledAt(5));
292 model.ActivatedAt(8);
293 EXPECT_TRUE(model.IsEnabledAt(8));
294 model.ActivatedAt(9);
295 EXPECT_TRUE(model.IsEnabledAt(9));
296 model.ActivatedAt(10);
297 EXPECT_TRUE(model.IsEnabledAt(10));
298 EXPECT_EQ(7, model.enable_count_);
299 EXPECT_EQ(7, model.execute_count_);
300 }
301
302 TEST_F(RecentTabsSubMenuModelTest, MaxSessionsAndRecency) {
303 browser_sync::SessionModelAssociatorForRecentTabsSubMenuModelTest associator;
304 SessionID::id_type id = 0;
305 base::Time now = base::Time::Now();
306
307 // Create 4 sessions : each session has 1 window with 1 tab each .
308 std::vector<TabTime> tab_times;
309 for (int i = 0; i < 4; ++i) {
310 SessionID::id_type session_id = id++;
311 associator.CreateSession(session_id);
312 SessionID::id_type window_id = id++;
313 sync_pb::SessionWindow window_pb;
314 associator.InitWindowPb(1, &id, &window_pb);
315 associator.PutWindowInSessionAndTracker(session_id, window_id, window_pb);
316 base::Time timestamp = now +
317 base::TimeDelta::FromMinutes(base::RandUint64());
318 associator.MakeTabSyncable(session_id, window_id, 0, timestamp);
319 tab_times.push_back(TabTime(session_id, window_id, 0, timestamp));
320 ASSERT_EQ(1U,
321 associator.tracker().num_synced_tabs(ToSessionTag(session_id)));
322 }
323 ASSERT_EQ(4U, associator.tracker().num_synced_sessions());
324
325 TestRecentTabsSubMenuModel model(this, browser(),
326 &associator.model_associator(), true);
327
328 // Expected menu:
329 // - max sessions is 3, so only 3 most-recent sessions will show
330 // Menu index Menu items
331 // --------------------------------------
332 // 0 Reopen closed tab
333 // 1 <separator>
334 // 2 <section header for 1st session>
335 // 3 <the only tab of the only window of session 3>
336 // 4 <separator>
337 // 5 <section header for 2nd session>
338 // 6 <the only tab of the only window of session 2>
339 // 7 <separator>
340 // 8 <section header for 3rd session>
341 // 9 <the only tab of the only window of session 1>
342
343 int num_items = model.GetItemCount();
344 EXPECT_EQ(10, num_items);
345
346 sort(tab_times.begin(), tab_times.end(), SortTabTimesByRecency);
347 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[0].session_id,
348 tab_times[0].window_id,
349 tab_times[0].tab_idx)),
350 model.GetLabelAt(3));
351 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[1].session_id,
352 tab_times[1].window_id,
353 tab_times[1].tab_idx)),
354 model.GetLabelAt(6));
355 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[2].session_id,
356 tab_times[2].window_id,
357 tab_times[2].tab_idx)),
358 model.GetLabelAt(9));
359 }
360
361 TEST_F(RecentTabsSubMenuModelTest, MaxTabsPerSessionAndRecency) {
362 browser_sync::SessionModelAssociatorForRecentTabsSubMenuModelTest associator;
363 SessionID::id_type id = 0;
364 base::Time now = base::Time::Now();
365
366 // Create a session: 2 windows with 5 tabs each.
367 std::vector<TabTime> tab_times;
368 SessionID::id_type session_id = id++;
369 associator.CreateSession(session_id);
370 for (int j = 0; j < 2; ++j) {
371 SessionID::id_type window_id = id++;
372 sync_pb::SessionWindow window_pb;
373 associator.InitWindowPb(5, &id, &window_pb);
374 associator.PutWindowInSessionAndTracker(session_id, window_id, window_pb);
375 for (int k = 0; k < 5; ++k) {
376 base::Time timestamp(now +
377 base::TimeDelta::FromMinutes(base::RandUint64()));
378 associator.MakeTabSyncable(session_id, window_id, k, timestamp);
379 tab_times.push_back(TabTime(session_id, window_id, k, timestamp));
380 }
381 }
382 ASSERT_EQ(10U,
383 associator.tracker().num_synced_tabs(ToSessionTag(session_id)));
384 ASSERT_EQ(1U, associator.tracker().num_synced_sessions());
385
386 TestRecentTabsSubMenuModel model(this, browser(),
387 &associator.model_associator(), true);
388
389 // Expected menu:
390 // - max tabs per session is 4, so only 4 most-recent tabs will show,
391 // independent of which window they came from
392 // Menu index Menu items
393 // --------------------------------------
394 // 0 Reopen closed tab
395 // 1 <separator>
396 // 2 <section header for session>
397 // 3-6 <4 most-recent tabs of session>
398
399 int num_items = model.GetItemCount();
400 EXPECT_EQ(7, num_items);
401
402 sort(tab_times.begin(), tab_times.end(), SortTabTimesByRecency);
403 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[0].session_id,
404 tab_times[0].window_id,
405 tab_times[0].tab_idx)),
406 model.GetLabelAt(3));
407 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[1].session_id,
408 tab_times[1].window_id,
409 tab_times[1].tab_idx)),
410 model.GetLabelAt(4));
411 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[2].session_id,
412 tab_times[2].window_id,
413 tab_times[2].tab_idx)),
414 model.GetLabelAt(5));
415 EXPECT_EQ(UTF8ToUTF16(ToTabTitle(tab_times[3].session_id,
416 tab_times[3].window_id,
417 tab_times[3].tab_idx)),
418 model.GetLabelAt(6));
419 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698