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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
// 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 "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/sessions/session_backend.h"
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_service_test_helper.h"
#include "chrome/browser/sessions/session_types.h"
#include "chrome/browser/sessions/session_types_test_helper.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebData.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebHTTPBody.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
#include "webkit/glue/glue_serialize.h"
using content::NavigationEntry;
class SessionServiceTest : public BrowserWithTestWindowTest,
public content::NotificationObserver {
public:
SessionServiceTest() : window_bounds(0, 1, 2, 3), sync_save_count_(0) {}
protected:
virtual void SetUp() {
BrowserWithTestWindowTest::SetUp();
std::string b = base::Int64ToString(base::Time::Now().ToInternalValue());
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("SessionTestDirs"));
ASSERT_TRUE(file_util::CreateDirectory(path_));
path_ = path_.AppendASCII(b);
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
service()->SetWindowType(
window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window_id,
window_bounds,
ui::SHOW_STATE_NORMAL);
}
// Upon notification, increment the sync_save_count variable
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
ASSERT_EQ(type, chrome::NOTIFICATION_SESSION_SERVICE_SAVED);
sync_save_count_++;
}
virtual void TearDown() {
helper_.set_service(NULL);
BrowserWithTestWindowTest::TearDown();
}
void UpdateNavigation(const SessionID& window_id,
const SessionID& tab_id,
const TabNavigation& navigation,
bool select) {
service()->UpdateTabNavigation(window_id, tab_id, navigation);
if (select) {
service()->SetSelectedNavigationIndex(
window_id, tab_id, navigation.index());
}
}
void ReadWindows(std::vector<SessionWindow*>* windows,
SessionID::id_type* active_window_id) {
// Forces closing the file.
helper_.set_service(NULL);
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
SessionID::id_type* non_null_active_window_id = active_window_id;
SessionID::id_type dummy_active_window_id = 0;
if (!non_null_active_window_id)
non_null_active_window_id = &dummy_active_window_id;
helper_.ReadWindows(windows, non_null_active_window_id);
}
// Configures the session service with one window with one tab and a single
// navigation. If |pinned_state| is true or |write_always| is true, the
// pinned state of the tab is updated. The session service is then recreated
// and the pinned state of the read back tab is returned.
bool CreateAndWriteSessionWithOneTab(bool pinned_state, bool write_always) {
SessionID tab_id;
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
if (pinned_state || write_always)
helper_.service()->SetPinnedState(window_id, tab_id, pinned_state);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
EXPECT_EQ(1U, windows.size());
if (HasFatalFailure())
return false;
EXPECT_EQ(1U, windows[0]->tabs.size());
if (HasFatalFailure())
return false;
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
return tab->pinned;
}
void CreateAndWriteSessionWithTwoWindows(
const SessionID& window2_id,
const SessionID& tab1_id,
const SessionID& tab2_id,
TabNavigation* nav1,
TabNavigation* nav2) {
*nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
*nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab1_id, 0, true);
UpdateNavigation(window_id, tab1_id, *nav1, true);
const gfx::Rect window2_bounds(3, 4, 5, 6);
service()->SetWindowType(
window2_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window2_id,
window2_bounds,
ui::SHOW_STATE_MAXIMIZED);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, true);
UpdateNavigation(window2_id, tab2_id, *nav2, true);
}
SessionService* service() { return helper_.service(); }
SessionBackend* backend() { return helper_.backend(); }
const gfx::Rect window_bounds;
SessionID window_id;
int sync_save_count_;
// Path used in testing.
base::ScopedTempDir temp_dir_;
FilePath path_;
SessionServiceTestHelper helper_;
};
TEST_F(SessionServiceTest, Basic) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
SessionTypesTestHelper::SetOriginalRequestURL(
&nav1, GURL("http://original.request.com"));
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_TRUE(window_bounds == windows[0]->bounds);
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(1U, windows[0]->tabs.size());
ASSERT_EQ(Browser::TYPE_TABBED, windows[0]->type);
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
}
// Make sure we persist post entries.
TEST_F(SessionServiceTest, PersistPostData) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
SessionTypesTestHelper::SetHasPostData(&nav1, true);
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
helper_.AssertSingleWindowWithSingleTab(windows.get(), 1);
}
TEST_F(SessionServiceTest, ClosingTabStaysClosed) {
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(tab_id.id(), tab2_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window_id, tab2_id, 1, false);
UpdateNavigation(window_id, tab2_id, nav2, true);
service()->TabClosed(window_id, tab2_id, false);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(1U, windows[0]->tabs.size());
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
}
TEST_F(SessionServiceTest, Pruning) {
SessionID tab_id;
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
for (int i = 0; i < 6; ++i) {
TabNavigation* nav = (i % 2) == 0 ? &nav1 : &nav2;
nav->set_index(i);
UpdateNavigation(window_id, tab_id, *nav, true);
}
service()->TabNavigationPathPrunedFromBack(window_id, tab_id, 3);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(1U, windows[0]->tabs.size());
SessionTab* tab = windows[0]->tabs[0];
// We left the selected index at 5, then pruned. When rereading the
// index should get reset to last valid navigation, which is 2.
helper_.AssertTabEquals(window_id, tab_id, 0, 2, 3, *tab);
ASSERT_EQ(3u, tab->navigations.size());
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
helper_.AssertNavigationEquals(nav2, tab->navigations[1]);
helper_.AssertNavigationEquals(nav1, tab->navigations[2]);
}
TEST_F(SessionServiceTest, TwoWindows) {
SessionID window2_id;
SessionID tab1_id;
SessionID tab2_id;
TabNavigation nav1;
TabNavigation nav2;
CreateAndWriteSessionWithTwoWindows(
window2_id, tab1_id, tab2_id, &nav1, &nav2);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(2U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(0, windows[1]->selected_tab_index);
ASSERT_EQ(1U, windows[0]->tabs.size());
ASSERT_EQ(1U, windows[1]->tabs.size());
SessionTab* rt1;
SessionTab* rt2;
if (windows[0]->window_id.id() == window_id.id()) {
ASSERT_EQ(window2_id.id(), windows[1]->window_id.id());
ASSERT_EQ(ui::SHOW_STATE_NORMAL, windows[0]->show_state);
ASSERT_EQ(ui::SHOW_STATE_MAXIMIZED, windows[1]->show_state);
rt1 = windows[0]->tabs[0];
rt2 = windows[1]->tabs[0];
} else {
ASSERT_EQ(window2_id.id(), windows[0]->window_id.id());
ASSERT_EQ(window_id.id(), windows[1]->window_id.id());
ASSERT_EQ(ui::SHOW_STATE_MAXIMIZED, windows[0]->show_state);
ASSERT_EQ(ui::SHOW_STATE_NORMAL, windows[1]->show_state);
rt1 = windows[1]->tabs[0];
rt2 = windows[0]->tabs[0];
}
SessionTab* tab = rt1;
helper_.AssertTabEquals(window_id, tab1_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
tab = rt2;
helper_.AssertTabEquals(window2_id, tab2_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav2, tab->navigations[0]);
}
TEST_F(SessionServiceTest, WindowWithNoTabsGetsPruned) {
SessionID window2_id;
SessionID tab1_id;
SessionID tab2_id;
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
helper_.PrepareTabInWindow(window_id, tab1_id, 0, true);
UpdateNavigation(window_id, tab1_id, nav1, true);
const gfx::Rect window2_bounds(3, 4, 5, 6);
service()->SetWindowType(
window2_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window2_id,
window2_bounds,
ui::SHOW_STATE_NORMAL);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(1U, windows[0]->tabs.size());
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab1_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
}
TEST_F(SessionServiceTest, ClosingWindowDoesntCloseTabs) {
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(tab_id.id(), tab2_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window_id, tab2_id, 1, false);
UpdateNavigation(window_id, tab2_id, nav2, true);
service()->WindowClosing(window_id);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(2U, windows[0]->tabs.size());
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
tab = windows[0]->tabs[1];
helper_.AssertTabEquals(window_id, tab2_id, 1, 0, 1, *tab);
helper_.AssertNavigationEquals(nav2, tab->navigations[0]);
}
TEST_F(SessionServiceTest, WindowCloseCommittedAfterNavigate) {
SessionID window2_id;
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(window2_id.id(), window_id.id());
service()->SetWindowType(
window2_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window2_id,
window_bounds,
ui::SHOW_STATE_NORMAL);
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, false);
UpdateNavigation(window2_id, tab2_id, nav2, true);
service()->WindowClosing(window2_id);
service()->TabClosed(window2_id, tab2_id, false);
service()->WindowClosed(window2_id);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(1U, windows[0]->tabs.size());
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
}
// Makes sure we don't track popups.
TEST_F(SessionServiceTest, IgnorePopups) {
if (browser_defaults::kRestorePopups)
return; // This test is only applicable if popups aren't restored.
SessionID window2_id;
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(window2_id.id(), window_id.id());
service()->SetWindowType(
window2_id, Browser::TYPE_POPUP, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window2_id,
window_bounds,
ui::SHOW_STATE_NORMAL);
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, false);
UpdateNavigation(window2_id, tab2_id, nav2, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(1U, windows[0]->tabs.size());
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
}
// Makes sure we track popups.
TEST_F(SessionServiceTest, RestorePopup) {
if (!browser_defaults::kRestorePopups)
return; // This test is only applicable if popups are restored.
SessionID window2_id;
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(window2_id.id(), window_id.id());
service()->SetWindowType(
window2_id, Browser::TYPE_POPUP, SessionService::TYPE_NORMAL);
service()->SetWindowBounds(window2_id,
window_bounds,
ui::SHOW_STATE_NORMAL);
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, false);
UpdateNavigation(window2_id, tab2_id, nav2, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(2U, windows.size());
int tabbed_index = windows[0]->type == Browser::TYPE_TABBED ?
0 : 1;
int popup_index = tabbed_index == 0 ? 1 : 0;
ASSERT_EQ(0, windows[tabbed_index]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[tabbed_index]->window_id.id());
ASSERT_EQ(1U, windows[tabbed_index]->tabs.size());
SessionTab* tab = windows[tabbed_index]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
ASSERT_EQ(0, windows[popup_index]->selected_tab_index);
ASSERT_EQ(window2_id.id(), windows[popup_index]->window_id.id());
ASSERT_EQ(1U, windows[popup_index]->tabs.size());
tab = windows[popup_index]->tabs[0];
helper_.AssertTabEquals(window2_id, tab2_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav2, tab->navigations[0]);
}
#if defined (OS_CHROMEOS)
// Makes sure we track apps. Only applicable on chromeos.
TEST_F(SessionServiceTest, RestoreApp) {
SessionID window2_id;
SessionID tab_id;
SessionID tab2_id;
ASSERT_NE(window2_id.id(), window_id.id());
service()->SetWindowType(
window2_id, Browser::TYPE_POPUP, SessionService::TYPE_APP);
service()->SetWindowBounds(window2_id,
window_bounds,
ui::SHOW_STATE_NORMAL);
service()->SetWindowAppName(window2_id, "TestApp");
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation("http://google2.com", "abcd");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.PrepareTabInWindow(window2_id, tab2_id, 0, false);
UpdateNavigation(window2_id, tab2_id, nav2, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(2U, windows.size());
int tabbed_index = windows[0]->type == Browser::TYPE_TABBED ?
0 : 1;
int app_index = tabbed_index == 0 ? 1 : 0;
ASSERT_EQ(0, windows[tabbed_index]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[tabbed_index]->window_id.id());
ASSERT_EQ(1U, windows[tabbed_index]->tabs.size());
SessionTab* tab = windows[tabbed_index]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
ASSERT_EQ(0, windows[app_index]->selected_tab_index);
ASSERT_EQ(window2_id.id(), windows[app_index]->window_id.id());
ASSERT_EQ(1U, windows[app_index]->tabs.size());
ASSERT_TRUE(windows[app_index]->type == Browser::TYPE_POPUP);
ASSERT_EQ("TestApp", windows[app_index]->app_name);
tab = windows[app_index]->tabs[0];
helper_.AssertTabEquals(window2_id, tab2_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav2, tab->navigations[0]);
}
#endif // defined (OS_CHROMEOS)
// Tests pruning from the front.
TEST_F(SessionServiceTest, PruneFromFront) {
const std::string base_url("http://google.com/");
SessionID tab_id;
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
// Add 5 navigations, with the 4th selected.
for (int i = 0; i < 5; ++i) {
TabNavigation nav =
SessionTypesTestHelper::CreateNavigation(
base_url + base::IntToString(i), "a");
nav.set_index(i);
UpdateNavigation(window_id, tab_id, nav, (i == 3));
}
// Prune the first two navigations from the front.
helper_.service()->TabNavigationPathPrunedFromFront(window_id, tab_id, 2);
// Read back in.
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(0, windows[0]->selected_tab_index);
ASSERT_EQ(window_id.id(), windows[0]->window_id.id());
ASSERT_EQ(1U, windows[0]->tabs.size());
// There shouldn't be an app id.
EXPECT_TRUE(windows[0]->tabs[0]->extension_app_id.empty());
// We should be left with three navigations, the 2nd selected.
SessionTab* tab = windows[0]->tabs[0];
ASSERT_EQ(1, tab->current_navigation_index);
EXPECT_EQ(3U, tab->navigations.size());
EXPECT_TRUE(GURL(base_url + base::IntToString(2)) ==
tab->navigations[0].virtual_url());
EXPECT_TRUE(GURL(base_url + base::IntToString(3)) ==
tab->navigations[1].virtual_url());
EXPECT_TRUE(GURL(base_url + base::IntToString(4)) ==
tab->navigations[2].virtual_url());
}
// Prunes from front so that we have no entries.
TEST_F(SessionServiceTest, PruneToEmpty) {
const std::string base_url("http://google.com/");
SessionID tab_id;
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
// Add 5 navigations, with the 4th selected.
for (int i = 0; i < 5; ++i) {
TabNavigation nav =
SessionTypesTestHelper::CreateNavigation(
base_url + base::IntToString(i), "a");
nav.set_index(i);
UpdateNavigation(window_id, tab_id, nav, (i == 3));
}
// Prune the first two navigations from the front.
helper_.service()->TabNavigationPathPrunedFromFront(window_id, tab_id, 5);
// Read back in.
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(0U, windows.size());
}
// Don't set the pinned state and make sure the pinned value is false.
TEST_F(SessionServiceTest, PinnedDefaultsToFalse) {
EXPECT_FALSE(CreateAndWriteSessionWithOneTab(false, false));
}
// Explicitly set the pinned state to false and make sure we get back false.
TEST_F(SessionServiceTest, PinnedFalseWhenSetToFalse) {
EXPECT_FALSE(CreateAndWriteSessionWithOneTab(false, true));
}
// Explicitly set the pinned state to true and make sure we get back true.
TEST_F(SessionServiceTest, PinnedTrue) {
EXPECT_TRUE(CreateAndWriteSessionWithOneTab(true, true));
}
// Make sure application extension ids are persisted.
TEST_F(SessionServiceTest, PersistApplicationExtensionID) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
std::string app_id("foo");
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.SetTabExtensionAppID(window_id, tab_id, app_id);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
helper_.AssertSingleWindowWithSingleTab(windows.get(), 1);
EXPECT_TRUE(app_id == windows[0]->tabs[0]->extension_app_id);
}
// Check that user agent overrides are persisted.
TEST_F(SessionServiceTest, PersistUserAgentOverrides) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
std::string user_agent_override = "Mozilla/5.0 (X11; Linux x86_64) "
"AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 "
"Safari/535.19";
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
SessionTypesTestHelper::SetIsOverridingUserAgent(&nav1, true);
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
helper_.SetTabUserAgentOverride(window_id, tab_id, user_agent_override);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
helper_.AssertSingleWindowWithSingleTab(windows.get(), 1);
SessionTab* tab = windows[0]->tabs[0];
helper_.AssertTabEquals(window_id, tab_id, 0, 0, 1, *tab);
helper_.AssertNavigationEquals(nav1, tab->navigations[0]);
EXPECT_TRUE(user_agent_override == tab->user_agent_override);
}
// Test that the notification for SESSION_SERVICE_SAVED is working properly.
TEST_F(SessionServiceTest, SavedSessionNotification) {
content::NotificationRegistrar registrar_;
registrar_.Add(this, chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
content::NotificationService::AllSources());
service()->Save();
EXPECT_EQ(sync_save_count_, 1);
}
// Makes sure a tab closed by a user gesture is not restored.
TEST_F(SessionServiceTest, CloseTabUserGesture) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
service()->TabClosed(window_id, tab_id, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_TRUE(windows.empty());
}
// Verifies SetWindowBounds maps SHOW_STATE_DEFAULT to SHOW_STATE_NORMAL.
TEST_F(SessionServiceTest, DontPersistDefault) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "abc");
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
service()->SetWindowBounds(window_id,
window_bounds,
ui::SHOW_STATE_DEFAULT);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
ASSERT_EQ(1U, windows.size());
EXPECT_EQ(ui::SHOW_STATE_NORMAL, windows[0]->show_state);
}
TEST_F(SessionServiceTest, KeepPostDataWithoutPasswords) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
// Create a content state representing a HTTP body without posted passwords.
WebKit::WebHTTPBody http_body;
http_body.initialize();
const char char_data[] = "data";
http_body.appendData(WebKit::WebData(char_data, sizeof(char_data)-1));
WebKit::WebHistoryItem history_item;
history_item.initialize();
history_item.setHTTPBody(http_body);
std::string content_state = webkit_glue::HistoryItemToString(history_item);
// Create a TabNavigation containing content_state and representing a POST
// request.
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "title");
SessionTypesTestHelper::SetContentState(&nav1, content_state);
SessionTypesTestHelper::SetHasPostData(&nav1, true);
// Create a TabNavigation containing content_state and representing a normal
// request.
TabNavigation nav2 =
SessionTypesTestHelper::CreateNavigation(
"http://google.com/nopost", "title");
SessionTypesTestHelper::SetContentState(&nav2, content_state);
nav2.set_index(1);
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
UpdateNavigation(window_id, tab_id, nav2, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
helper_.AssertSingleWindowWithSingleTab(windows.get(), 2);
// Expected: the content state of both navigations was saved and restored.
ASSERT_EQ(2u, windows[0]->tabs[0]->navigations.size());
helper_.AssertNavigationEquals(nav1, windows[0]->tabs[0]->navigations[0]);
helper_.AssertNavigationEquals(nav2, windows[0]->tabs[0]->navigations[1]);
}
TEST_F(SessionServiceTest, RemovePostDataWithPasswords) {
SessionID tab_id;
ASSERT_NE(window_id.id(), tab_id.id());
// Create a content state representing a HTTP body with posted passwords.
WebKit::WebHTTPBody http_body;
http_body.initialize();
const char char_data[] = "data";
http_body.appendData(WebKit::WebData(char_data, sizeof(char_data)-1));
http_body.setContainsPasswordData(true);
WebKit::WebHistoryItem history_item;
history_item.initialize();
history_item.setHTTPBody(http_body);
std::string content_state = webkit_glue::HistoryItemToString(history_item);
// Create a TabNavigation containing content_state and representing a POST
// request with passwords.
TabNavigation nav1 =
SessionTypesTestHelper::CreateNavigation("http://google.com", "title");
SessionTypesTestHelper::SetContentState(&nav1, content_state);
SessionTypesTestHelper::SetHasPostData(&nav1, true);
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
UpdateNavigation(window_id, tab_id, nav1, true);
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
helper_.AssertSingleWindowWithSingleTab(windows.get(), 1);
// Expected: the HTTP body was removed from the content state of the POST
// navigation with passwords.
EXPECT_NE(content_state, windows[0]->tabs[0]->navigations[0].content_state());
}
// This test is only applicable to chromeos.
#if defined(OS_CHROMEOS)
// Verifies migration of tab/window closed works.
TEST_F(SessionServiceTest, CanOpenV1TabClosed) {
FilePath v1_file_path;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &v1_file_path));
// v1_session_file contains a tab closed command with the original id. The
// file was generated from ClosingTabStaysClosed. If we successfully processed
// the file we'll have one tab.
v1_file_path =
v1_file_path.AppendASCII("sessions").AppendASCII("v1_session_file");
FilePath dest_file_path(path_);
dest_file_path = dest_file_path.AppendASCII("Current Session");
// Forces closing the file.
helper_.set_service(NULL);
ASSERT_TRUE(file_util::CopyFile(v1_file_path, dest_file_path));
SessionService* session_service = new SessionService(path_);
helper_.set_service(session_service);
ScopedVector<SessionWindow> windows;
SessionID::id_type active_window_id = 0;
helper_.ReadWindows(&(windows.get()), &active_window_id);
ASSERT_EQ(1u, windows.size());
EXPECT_EQ(1u, windows[0]->tabs.size());
}
#endif // defined(OS_CHROMEOS)
TEST_F(SessionServiceTest, ReplacePendingNavigation) {
const std::string base_url("http://google.com/");
SessionID tab_id;
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
// Add 5 navigations, some with the same index
for (int i = 0; i < 5; ++i) {
TabNavigation nav =
SessionTypesTestHelper::CreateNavigation(
base_url + base::IntToString(i), "a");
nav.set_index(i / 2);
UpdateNavigation(window_id, tab_id, nav, true);
}
// Read back in.
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
// The ones with index 0, and 2 should have been replaced by 1 and 3.
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(1U, windows[0]->tabs.size());
EXPECT_EQ(3U, windows[0]->tabs[0]->navigations.size());
EXPECT_EQ(GURL(base_url + base::IntToString(1)),
windows[0]->tabs[0]->navigations[0].virtual_url());
EXPECT_EQ(GURL(base_url + base::IntToString(3)),
windows[0]->tabs[0]->navigations[1].virtual_url());
EXPECT_EQ(GURL(base_url + base::IntToString(4)),
windows[0]->tabs[0]->navigations[2].virtual_url());
}
TEST_F(SessionServiceTest, ReplacePendingNavigationAndPrune) {
const std::string base_url("http://google.com/");
SessionID tab_id;
helper_.PrepareTabInWindow(window_id, tab_id, 0, true);
for (int i = 0; i < 5; ++i) {
TabNavigation nav =
SessionTypesTestHelper::CreateNavigation(
base_url + base::IntToString(i), "a");
nav.set_index(i);
UpdateNavigation(window_id, tab_id, nav, true);
}
// Prune all those navigations.
helper_.service()->TabNavigationPathPrunedFromFront(window_id, tab_id, 5);
// Add another navigation to replace the last one.
TabNavigation nav =
SessionTypesTestHelper::CreateNavigation(
base_url + base::IntToString(5), "a");
nav.set_index(4);
UpdateNavigation(window_id, tab_id, nav, true);
// Read back in.
ScopedVector<SessionWindow> windows;
ReadWindows(&(windows.get()), NULL);
// We should still have that last navigation at the end,
// even though it replaced one that was set before the prune.
ASSERT_EQ(1U, windows.size());
ASSERT_EQ(1U, windows[0]->tabs.size());
ASSERT_EQ(1U, windows[0]->tabs[0]->navigations.size());
EXPECT_EQ(GURL(base_url + base::IntToString(5)),
windows[0]->tabs[0]->navigations[0].virtual_url());
}
TEST_F(SessionServiceTest, RestoreActivation1) {
SessionID window2_id;
SessionID tab1_id;
SessionID tab2_id;
TabNavigation nav1;
TabNavigation nav2;
CreateAndWriteSessionWithTwoWindows(
window2_id, tab1_id, tab2_id, &nav1, &nav2);
service()->ScheduleCommand(
service()->CreateSetActiveWindowCommand(window2_id));
service()->ScheduleCommand(
service()->CreateSetActiveWindowCommand(window_id));
ScopedVector<SessionWindow> windows;
SessionID::id_type active_window_id = 0;
ReadWindows(&(windows.get()), &active_window_id);
EXPECT_EQ(window_id.id(), active_window_id);
}
// It's easier to have two separate tests with setup/teardown than to manualy
// reset the state for the different flavors of the test.
TEST_F(SessionServiceTest, RestoreActivation2) {
SessionID window2_id;
SessionID tab1_id;
SessionID tab2_id;
TabNavigation nav1;
TabNavigation nav2;
CreateAndWriteSessionWithTwoWindows(
window2_id, tab1_id, tab2_id, &nav1, &nav2);
service()->ScheduleCommand(
service()->CreateSetActiveWindowCommand(window2_id));
service()->ScheduleCommand(
service()->CreateSetActiveWindowCommand(window_id));
service()->ScheduleCommand(
service()->CreateSetActiveWindowCommand(window2_id));
ScopedVector<SessionWindow> windows;
SessionID::id_type active_window_id = 0;
ReadWindows(&(windows.get()), &active_window_id);
EXPECT_EQ(window2_id.id(), active_window_id);
}
|