summaryrefslogtreecommitdiffstats
path: root/chrome/test/live_sync/live_sessions_sync_test.h
blob: 87c3a65c1bbcd79f3eb9568a02e7c278f90fed41 (plain)
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
// Copyright (c) 2011 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.

#ifndef CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_
#define CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_
#pragma once

#include <algorithm>
#include <vector>

#include "base/compiler_specific.h"
#include "base/scoped_vector.h"
#include "base/ref_counted.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/sessions/base_session_service.h"
#include "chrome/browser/sessions/session_service_test_helper.h"
#include "chrome/browser/sessions/session_types.h"
#include "chrome/test/live_sync/live_sync_test.h"

class GURL;
class Profile;
class TabContents;

// Helper for accessing session service internals.
class TestSessionService
    : public SessionServiceTestHelper,
      public base::RefCountedThreadSafe<TestSessionService> {
 public:
  TestSessionService();
  TestSessionService(SessionService* service, Profile* profile);

  void SetUp();

  // Trigger saving the current session commands to file.
  void Save();

  // Synchronously reads the contents of the current session.
  std::vector<SessionWindow*>* ReadWindows();

  // Makes the actual call to session service.
  // Lives on the file thread.
  void DoReadWindows();

  // Internal method used in the callback to obtain the current session.
  // Lives on and called from backend thread (file_thread).
  // We don't own windows so need to make a deep copy.
  // In this case, we only copy those values compared against in WindowsMatch
  // (number of windows, number of tabs, and navigations within tabs).
  void OnGotSession(int handle, std::vector<SessionWindow*>* windows);

 private:
  virtual ~TestSessionService();

  friend class base::RefCountedThreadSafe<TestSessionService>;

  // Current session buffer.
  std::vector<SessionWindow*>* windows_;

  // List of all foreign sessions we created in ReadWindows. We delete them all
  // at destruction time so that complex tests can keep comparing against old
  // SessionWindow data. Note that since we're constantly creating new foreign
  // sessions, we don't have to worry about duplicates.
  ScopedVector<ForeignSession> foreign_sessions_;

  // Barrier for saving session.
  base::WaitableEvent done_saving_;

  // Barrier for getting current windows in ReadWindows.
  base::WaitableEvent got_windows_;

  // Consumer used to obtain the current session.
  CancelableRequestConsumer consumer_;

  // Sync profile associated with this session service.
  Profile* profile_;

  SessionID window_id_;
  const gfx::Rect window_bounds_;
};

class LiveSessionsSyncTest : public LiveSyncTest {
 public:
  explicit LiveSessionsSyncTest(TestType test_type);
  virtual ~LiveSessionsSyncTest();

  // Used to access the session service associated with a specific sync profile.
  SessionService* GetSessionService(int index);

  // Used to access the session service test helper associated with a specific
  // sync profile.
  TestSessionService* GetHelper(int index);

  // Used to access the browser associated with a specific sync profile.
  Browser* GetBrowser(int index);

  // Sets up the TestSessionService helper and the new browser windows.
  virtual bool SetupClients();

  // Open a single tab and return the TabContents. TabContents must be checked
  // to ensure the tab opened successsfully.
  TabContents* OpenTab(int index, GURL url) WARN_UNUSED_RESULT;

  // Creates and verifies the creation of a new window with one tab displaying
  // the specified GURL.
  // Returns: the SessionWindow associated with the new window.
  std::vector<SessionWindow*>* InitializeNewWindowWithTab(int index, GURL url)
      WARN_UNUSED_RESULT;

  // Checks that window count and foreign session count are 0.
  bool CheckInitialState(int index) WARN_UNUSED_RESULT;

  // Returns number of open windows for a profile.
  int GetNumWindows(int index);

  // Returns number of foreign sessions for a profile.
  int GetNumForeignSessions(int index);

  // Fills the sessions vector with the model associator's foreign session data.
  // Caller owns |sessions|, but not ForeignSession objects within.
  bool GetSessionData(int index, std::vector<const ForeignSession*>* sessions)
      WARN_UNUSED_RESULT;

  // Compare session windows based on their first tab's url.
  // Returns true if the virtual url of the lhs is < the rhs.
  static bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs);

  // Sort session windows using our custom comparator (first tab url
  // comparison).
  void SortSessionWindows(std::vector<SessionWindow*>* windows);

  // Compares a foreign session based on the first session window.
  // Returns true based on the comparison of the session windows.
  static bool CompareForeignSessions(
      const ForeignSession* lhs,
      const ForeignSession* rhs);

  // Sort a foreign session vector using our custom foreign session comparator.
  void SortForeignSessions(std::vector<const ForeignSession*>* sessions);

  // Compares two tab navigations base on the parameters we sync.
  // (Namely, we don't sync state or type mask)
  bool NavigationEquals(const TabNavigation& expected,
                        const TabNavigation& actual);

  // Verifies that two SessionWindows match.
  // Returns:
  //  - true if all the following match:
  //    1. number of SessionWindows,
  //    2. number of tabs per SessionWindow,
  //    3. number of tab navigations per tab,
  //    4. actual tab navigations contents
  // - false otherwise.
  bool WindowsMatch(const std::vector<SessionWindow*> &win1,
                    const std::vector<SessionWindow*> &win2) WARN_UNUSED_RESULT;

  // Retrieves the foreign sessions for a particular profile and compares them
  // with a reference SessionWindow list.
  // Returns true if the session windows of the foreign session matches the
  // reference.
  bool CheckForeignSessionsAgainst(int index,
      const std::vector<std::vector<SessionWindow*>* >& windows)
      WARN_UNUSED_RESULT;

 protected:
  // Clean up our mess.
  virtual void CleanUpOnMainThread();

  // Vector of our TestSessionService helpers.
  ScopedVector<scoped_refptr<TestSessionService> > test_session_services_;

  // Vector of our browsers for each profile.
  std::vector<Browser*> browsers_;

  // Barrier for closing the browsers we create in UI thread.
  base::WaitableEvent done_closing_;

  DISALLOW_COPY_AND_ASSIGN(LiveSessionsSyncTest);
};

class SingleClientLiveSessionsSyncTest : public LiveSessionsSyncTest {
 public:
  SingleClientLiveSessionsSyncTest()
      : LiveSessionsSyncTest(SINGLE_CLIENT) {}
  virtual ~SingleClientLiveSessionsSyncTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(SingleClientLiveSessionsSyncTest);
};

class TwoClientLiveSessionsSyncTest : public LiveSessionsSyncTest {
 public:
  TwoClientLiveSessionsSyncTest() : LiveSessionsSyncTest(TWO_CLIENT) {}
  virtual ~TwoClientLiveSessionsSyncTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TwoClientLiveSessionsSyncTest);
};

class MultipleClientLiveSessionsSyncTest : public LiveSessionsSyncTest {
 public:
  MultipleClientLiveSessionsSyncTest()
      : LiveSessionsSyncTest(MULTIPLE_CLIENT) {}
  virtual ~MultipleClientLiveSessionsSyncTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(MultipleClientLiveSessionsSyncTest);
};

#endif  // CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_