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 "base/memory/scoped_vector.h"
6 #include "chrome/browser/history/history_types.h"
7 #include "chrome/browser/sessions/session_service.h"
8 #include "chrome/browser/sessions/session_types.h"
9 #include "chrome/browser/sync/profile_sync_service.h"
10 #include "chrome/browser/sync/test/integration/sessions_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
12 #include "chrome/browser/sync/test/integration/sync_test.h"
13 #include "chrome/browser/sync/test/integration/typed_urls_helper.h"
14 #include "sync/util/time.h"
15
16 using sessions_helper::CheckInitialState;
17 using sessions_helper::GetLocalWindows;
18 using sessions_helper::GetSessionData;
19 using sessions_helper::OpenTabAndGetLocalWindows;
20 using sessions_helper::ScopedWindowMap;
21 using sessions_helper::SessionWindowMap;
22 using sessions_helper::SyncedSessionVector;
23 using sessions_helper::WindowsMatch;
24 using sync_integration_test_util::AwaitCommitActivityCompletion;
25 using typed_urls_helper::GetUrlFromClient;
26
27 class SingleClientSessionsSyncTest : public SyncTest {
28 public:
SingleClientSessionsSyncTest()29 SingleClientSessionsSyncTest() : SyncTest(SINGLE_CLIENT) {}
~SingleClientSessionsSyncTest()30 virtual ~SingleClientSessionsSyncTest() {}
31
32 private:
33 DISALLOW_COPY_AND_ASSIGN(SingleClientSessionsSyncTest);
34 };
35
36 // Timeout on Windows, see http://crbug.com/99819
37 #if defined(OS_WIN)
38 #define MAYBE_Sanity DISABLED_Sanity
39 #else
40 #define MAYBE_Sanity Sanity
41 #endif
42
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,MAYBE_Sanity)43 IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, MAYBE_Sanity) {
44 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
45
46 ASSERT_TRUE(CheckInitialState(0));
47
48 // Add a new session to client 0 and wait for it to sync.
49 ScopedWindowMap old_windows;
50 ASSERT_TRUE(OpenTabAndGetLocalWindows(0,
51 GURL("http://127.0.0.1/bubba"),
52 old_windows.GetMutable()));
53 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
54
55 // Get foreign session data from client 0.
56 SyncedSessionVector sessions;
57 ASSERT_FALSE(GetSessionData(0, &sessions));
58 ASSERT_EQ(0U, sessions.size());
59
60 // Verify client didn't change.
61 ScopedWindowMap new_windows;
62 ASSERT_TRUE(GetLocalWindows(0, new_windows.GetMutable()));
63 ASSERT_TRUE(WindowsMatch(*old_windows.Get(), *new_windows.Get()));
64 }
65
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,TimestampMatchesHistory)66 IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, TimestampMatchesHistory) {
67 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
68
69 ASSERT_TRUE(CheckInitialState(0));
70
71 // We want a URL that doesn't 404 and has a non-empty title.
72 // about:version is simple to render, too.
73 const GURL url("about:version");
74
75 ScopedWindowMap windows;
76 ASSERT_TRUE(OpenTabAndGetLocalWindows(0, url, windows.GetMutable()));
77
78 int found_navigations = 0;
79 for (SessionWindowMap::const_iterator it = windows.Get()->begin();
80 it != windows.Get()->end(); ++it) {
81 for (std::vector<SessionTab*>::const_iterator it2 =
82 it->second->tabs.begin(); it2 != it->second->tabs.end(); ++it2) {
83 for (std::vector<sessions::SerializedNavigationEntry>::const_iterator
84 it3 = (*it2)->navigations.begin();
85 it3 != (*it2)->navigations.end(); ++it3) {
86 const base::Time timestamp = it3->timestamp();
87
88 history::URLRow virtual_row;
89 ASSERT_TRUE(GetUrlFromClient(0, it3->virtual_url(), &virtual_row));
90 const base::Time history_timestamp = virtual_row.last_visit();
91
92 ASSERT_EQ(timestamp, history_timestamp);
93 ++found_navigations;
94 }
95 }
96 }
97 ASSERT_EQ(1, found_navigations);
98 }
99
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,ResponseCodeIsPreserved)100 IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, ResponseCodeIsPreserved) {
101 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
102
103 ASSERT_TRUE(CheckInitialState(0));
104
105 // We want a URL that doesn't 404 and has a non-empty title.
106 // about:version is simple to render, too.
107 const GURL url("about:version");
108
109 ScopedWindowMap windows;
110 ASSERT_TRUE(OpenTabAndGetLocalWindows(0, url, windows.GetMutable()));
111
112 int found_navigations = 0;
113 for (SessionWindowMap::const_iterator it = windows.Get()->begin();
114 it != windows.Get()->end(); ++it) {
115 for (std::vector<SessionTab*>::const_iterator it2 =
116 it->second->tabs.begin(); it2 != it->second->tabs.end(); ++it2) {
117 for (std::vector<sessions::SerializedNavigationEntry>::const_iterator
118 it3 = (*it2)->navigations.begin();
119 it3 != (*it2)->navigations.end(); ++it3) {
120 EXPECT_EQ(200, it3->http_status_code());
121 ++found_navigations;
122 }
123 }
124 }
125 ASSERT_EQ(1, found_navigations);
126 }
127