two_client_sessions_sync_test.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/sessions/session_service.h"
7#include "chrome/browser/sync/test/integration/passwords_helper.h"
8#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
9#include "chrome/browser/sync/test/integration/sessions_helper.h"
10#include "chrome/browser/sync/test/integration/sync_test.h"
11#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
12
13using passwords_helper::SetDecryptionPassphrase;
14using passwords_helper::SetEncryptionPassphrase;
15using sessions_helper::CheckInitialState;
16using sessions_helper::DeleteForeignSession;
17using sessions_helper::GetLocalWindows;
18using sessions_helper::GetSessionData;
19using sessions_helper::OpenTabAndGetLocalWindows;
20using sessions_helper::ScopedWindowMap;
21using sessions_helper::SyncedSessionVector;
22using sessions_helper::WindowsMatch;
23
24class TwoClientSessionsSyncTest : public SyncTest {
25 public:
26  TwoClientSessionsSyncTest() : SyncTest(TWO_CLIENT) {}
27  virtual ~TwoClientSessionsSyncTest() {}
28
29 private:
30  DISALLOW_COPY_AND_ASSIGN(TwoClientSessionsSyncTest);
31};
32
33static const char* kURL1 = "http://127.0.0.1/bubba1";
34static const char* kURL2 = "http://127.0.0.1/bubba2";
35
36// TODO(zea): Test each individual session command we care about separately.
37// (as well as multi-window). We're currently only checking basic single-window/
38// single-tab functionality.
39
40// Fails on Win, see http://crbug.com/232313
41#if defined(OS_WIN)
42#define MAYBE_SingleClientChanged DISABLED_SingleClientChanged
43#define MAYBE_SingleClientEnabledEncryptionAndChanged DISABLED_SingleClientEnabledEncryptionAndChanged
44#define MAYBE_BothChanged DISABLED_BothChanged
45#define MAYBE_DeleteIdleSession DISABLED_DeleteIdleSession
46#else
47#define MAYBE_SingleClientChanged SingleClientChanged
48#define MAYBE_SingleClientEnabledEncryptionAndChanged SingleClientEnabledEncryptionAndChanged
49#define MAYBE_BothChanged BothChanged
50#define MAYBE_DeleteIdleSession DeleteIdleSession
51#endif
52
53
54IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest,
55                       MAYBE_SingleClientChanged) {
56  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
57
58  ASSERT_TRUE(CheckInitialState(0));
59  ASSERT_TRUE(CheckInitialState(1));
60
61  ScopedWindowMap client0_windows;
62  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL1),
63      client0_windows.GetMutable()));
64
65  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
66
67  // Get foreign session data from client 1.
68  SyncedSessionVector sessions1;
69  ASSERT_TRUE(GetSessionData(1, &sessions1));
70
71  // Verify client 1's foreign session matches client 0 current window.
72  ASSERT_EQ(1U, sessions1.size());
73  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
74}
75
76IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest,
77                       SingleClientEnabledEncryption) {
78  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
79
80  ASSERT_TRUE(CheckInitialState(0));
81  ASSERT_TRUE(CheckInitialState(1));
82
83  ASSERT_TRUE(EnableEncryption(0));
84  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
85  ASSERT_TRUE(IsEncryptionComplete(0));
86  ASSERT_TRUE(IsEncryptionComplete(1));
87}
88
89IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest,
90                       MAYBE_SingleClientEnabledEncryptionAndChanged) {
91  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
92
93  ASSERT_TRUE(CheckInitialState(0));
94  ASSERT_TRUE(CheckInitialState(1));
95
96  ScopedWindowMap client0_windows;
97  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL1),
98      client0_windows.GetMutable()));
99  ASSERT_TRUE(EnableEncryption(0));
100  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
101
102  // Get foreign session data from client 1.
103  ASSERT_TRUE(IsEncryptionComplete(1));
104  SyncedSessionVector sessions1;
105  ASSERT_TRUE(GetSessionData(1, &sessions1));
106
107  // Verify client 1's foreign session matches client 0 current window.
108  ASSERT_EQ(1U, sessions1.size());
109  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
110}
111
112IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest,
113                       BothClientsEnabledEncryption) {
114  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
115
116  ASSERT_TRUE(CheckInitialState(0));
117  ASSERT_TRUE(CheckInitialState(1));
118
119  ASSERT_TRUE(EnableEncryption(0));
120  ASSERT_TRUE(EnableEncryption(1));
121  ASSERT_TRUE(AwaitQuiescence());
122  ASSERT_TRUE(IsEncryptionComplete(0));
123  ASSERT_TRUE(IsEncryptionComplete(1));
124}
125
126IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest, MAYBE_BothChanged) {
127  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
128
129  ASSERT_TRUE(CheckInitialState(0));
130  ASSERT_TRUE(CheckInitialState(1));
131
132  // Open tabs on both clients and retain window information.
133  ScopedWindowMap client0_windows;
134  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL2),
135      client0_windows.GetMutable()));
136  ScopedWindowMap client1_windows;
137  ASSERT_TRUE(OpenTabAndGetLocalWindows(1, GURL(kURL1),
138      client1_windows.GetMutable()));
139
140  // Wait for sync.
141  ASSERT_TRUE(AwaitQuiescence());
142
143  // Get foreign session data from client 0 and 1.
144  SyncedSessionVector sessions0;
145  SyncedSessionVector sessions1;
146  ASSERT_TRUE(GetSessionData(0, &sessions0));
147  ASSERT_TRUE(GetSessionData(1, &sessions1));
148
149  // Verify client 1's foreign session matches client 0's current window and
150  // vice versa.
151  ASSERT_EQ(1U, sessions0.size());
152  ASSERT_EQ(1U, sessions1.size());
153  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
154  ASSERT_TRUE(WindowsMatch(sessions0[0]->windows, *client1_windows.Get()));
155}
156
157IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest, MAYBE_DeleteIdleSession) {
158  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
159
160  ASSERT_TRUE(CheckInitialState(0));
161  ASSERT_TRUE(CheckInitialState(1));
162
163  // Client 0 opened some tabs then went idle.
164  ScopedWindowMap client0_windows;
165  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL1),
166      client0_windows.GetMutable()));
167
168  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
169
170  // Get foreign session data from client 1.
171  SyncedSessionVector sessions1;
172  ASSERT_TRUE(GetSessionData(1, &sessions1));
173
174  // Verify client 1's foreign session matches client 0 current window.
175  ASSERT_EQ(1U, sessions1.size());
176  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
177
178  // Client 1 now deletes client 0's tabs. This frees the memory of sessions1.
179  DeleteForeignSession(1, sessions1[0]->session_tag);
180  ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
181  ASSERT_FALSE(GetSessionData(1, &sessions1));
182}
183
184// Fails all release trybots. crbug.com/263369.
185IN_PROC_BROWSER_TEST_F(TwoClientSessionsSyncTest,
186                       DISABLED_DeleteActiveSession) {
187  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
188
189  ASSERT_TRUE(CheckInitialState(0));
190  ASSERT_TRUE(CheckInitialState(1));
191
192  // Client 0 opened some tabs then went idle.
193  ScopedWindowMap client0_windows;
194  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL1),
195      client0_windows.GetMutable()));
196
197  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
198  SyncedSessionVector sessions1;
199  ASSERT_TRUE(GetSessionData(1, &sessions1));
200  ASSERT_EQ(1U, sessions1.size());
201  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
202
203  // Client 1 now deletes client 0's tabs. This frees the memory of sessions1.
204  DeleteForeignSession(1, sessions1[0]->session_tag);
205  ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
206  ASSERT_FALSE(GetSessionData(1, &sessions1));
207
208  // Client 0 becomes active again with a new tab.
209  ASSERT_TRUE(OpenTabAndGetLocalWindows(0, GURL(kURL2),
210      client0_windows.GetMutable()));
211  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
212  ASSERT_TRUE(GetSessionData(1, &sessions1));
213  ASSERT_EQ(1U, sessions1.size());
214  ASSERT_TRUE(WindowsMatch(sessions1[0]->windows, *client0_windows.Get()));
215}
216