summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/synced_session_tracker_unittest.cc
blob: 9fc3ae856ec8582b88cca173648f813e4a3eb3a0 (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
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
// Copyright 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 <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "base/rand_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/sync/glue/synced_session_tracker.h"
#include "components/sessions/serialized_navigation_entry_test_helper.h"
#include "components/sessions/session_types.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browser_sync {

typedef testing::Test SyncedSessionTrackerTest;

TEST_F(SyncedSessionTrackerTest, GetSession) {
  SyncedSessionTracker tracker;
  sync_driver::SyncedSession* session1 = tracker.GetSession("tag");
  sync_driver::SyncedSession* session2 = tracker.GetSession("tag2");
  ASSERT_EQ(session1, tracker.GetSession("tag"));
  ASSERT_NE(session1, session2);
  // Should clean up memory on its own.
}

TEST_F(SyncedSessionTrackerTest, GetTabUnmapped) {
  SyncedSessionTracker tracker;
  sessions::SessionTab* tab = tracker.GetTab("tag", 0, 0);
  ASSERT_EQ(tab, tracker.GetTab("tag", 0, 0));
  // Should clean up memory on its own.
}

TEST_F(SyncedSessionTrackerTest, PutWindowInSession) {
  SyncedSessionTracker tracker;
  tracker.PutWindowInSession("tag", 0);
  sync_driver::SyncedSession* session = tracker.GetSession("tag");
  ASSERT_EQ(1U, session->windows.size());
  // Should clean up memory on its own.
}

TEST_F(SyncedSessionTrackerTest, PutTabInWindow) {
  SyncedSessionTracker tracker;
  tracker.PutWindowInSession("tag", 10);
  tracker.PutTabInWindow("tag", 10, 15, 0);  // win id 10, tab id 15, tab ind 0.
  sync_driver::SyncedSession* session = tracker.GetSession("tag");
  ASSERT_EQ(1U, session->windows.size());
  ASSERT_EQ(1U, session->windows[10]->tabs.size());
  ASSERT_EQ(tracker.GetTab("tag", 15, 1), session->windows[10]->tabs[0]);
  // Should clean up memory on its own.
}

TEST_F(SyncedSessionTrackerTest, LookupAllForeignSessions) {
  SyncedSessionTracker tracker;
  std::vector<const sync_driver::SyncedSession*> sessions;
  ASSERT_FALSE(tracker.LookupAllForeignSessions(&sessions));
  tracker.GetSession("tag1");
  tracker.GetSession("tag2");
  tracker.PutWindowInSession("tag1", 0);
  tracker.PutTabInWindow("tag1", 0, 15, 0);
  sessions::SessionTab* tab = tracker.GetTab("tag1", 15, 1);
  ASSERT_TRUE(tab);
  tab->navigations.push_back(
      sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
      "bla://valid_url", "title"));
  ASSERT_TRUE(tracker.LookupAllForeignSessions(&sessions));
  // Only the session with a valid window and tab gets returned.
  ASSERT_EQ(1U, sessions.size());
  ASSERT_EQ("tag1", sessions[0]->session_tag);
}

TEST_F(SyncedSessionTrackerTest, LookupSessionWindows) {
  SyncedSessionTracker tracker;
  std::vector<const sessions::SessionWindow*> windows;
  ASSERT_FALSE(tracker.LookupSessionWindows("tag1", &windows));
  tracker.GetSession("tag1");
  tracker.PutWindowInSession("tag1", 0);
  tracker.PutWindowInSession("tag1", 2);
  tracker.GetSession("tag2");
  tracker.PutWindowInSession("tag2", 0);
  tracker.PutWindowInSession("tag2", 2);
  ASSERT_TRUE(tracker.LookupSessionWindows("tag1", &windows));
  ASSERT_EQ(2U, windows.size());  // Only windows from tag1 session.
  ASSERT_NE((sessions::SessionWindow*)nullptr, windows[0]);
  ASSERT_NE((sessions::SessionWindow*)nullptr, windows[1]);
  ASSERT_NE(windows[1], windows[0]);
}

TEST_F(SyncedSessionTrackerTest, LookupSessionTab) {
  SyncedSessionTracker tracker;
  const sessions::SessionTab* tab;
  ASSERT_FALSE(tracker.LookupSessionTab("tag1", 5, &tab));
  tracker.GetSession("tag1");
  tracker.PutWindowInSession("tag1", 0);
  tracker.PutTabInWindow("tag1", 0, 5, 0);
  ASSERT_TRUE(tracker.LookupSessionTab("tag1", 5, &tab));
  ASSERT_NE((sessions::SessionTab*)nullptr, tab);
}

TEST_F(SyncedSessionTrackerTest, Complex) {
  const std::string tag1 = "tag";
  const std::string tag2 = "tag2";
  const std::string tag3 = "tag3";
  SyncedSessionTracker tracker;
  std::vector<sessions::SessionTab*> tabs1, tabs2;
  sessions::SessionTab* temp_tab;
  ASSERT_TRUE(tracker.Empty());
  ASSERT_EQ(0U, tracker.num_synced_sessions());
  ASSERT_EQ(0U, tracker.num_synced_tabs(tag1));
  tabs1.push_back(tracker.GetTab(tag1, 0, 0));
  tabs1.push_back(tracker.GetTab(tag1, 1, 1));
  tabs1.push_back(tracker.GetTab(tag1, 2, 2));
  ASSERT_EQ(3U, tracker.num_synced_tabs(tag1));
  ASSERT_EQ(0U, tracker.num_synced_sessions());
  temp_tab = tracker.GetTab(tag1, 0, 0);  // Already created.
  ASSERT_EQ(3U, tracker.num_synced_tabs(tag1));
  ASSERT_EQ(0U, tracker.num_synced_sessions());
  ASSERT_EQ(tabs1[0], temp_tab);
  tabs2.push_back(tracker.GetTab(tag2, 0, 0));
  ASSERT_EQ(1U, tracker.num_synced_tabs(tag2));
  ASSERT_EQ(0U, tracker.num_synced_sessions());
  ASSERT_FALSE(tracker.DeleteSession(tag3));

  sync_driver::SyncedSession* session = tracker.GetSession(tag1);
  sync_driver::SyncedSession* session2 = tracker.GetSession(tag2);
  sync_driver::SyncedSession* session3 = tracker.GetSession(tag3);
  ASSERT_EQ(3U, tracker.num_synced_sessions());

  ASSERT_TRUE(session);
  ASSERT_TRUE(session2);
  ASSERT_TRUE(session3);
  ASSERT_NE(session, session2);
  ASSERT_NE(session2, session3);
  ASSERT_TRUE(tracker.DeleteSession(tag3));
  ASSERT_EQ(2U, tracker.num_synced_sessions());

  tracker.PutWindowInSession(tag1, 0);      // Create a window.
  tracker.PutTabInWindow(tag1, 0, 2, 0);    // No longer unmapped.
  ASSERT_EQ(3U, tracker.num_synced_tabs(tag1));      // Has not changed.

  const sessions::SessionTab *tab_ptr;
  ASSERT_TRUE(tracker.LookupSessionTab(tag1, 0, &tab_ptr));
  ASSERT_EQ(tab_ptr, tabs1[0]);
  ASSERT_TRUE(tracker.LookupSessionTab(tag1, 2, &tab_ptr));
  ASSERT_EQ(tab_ptr, tabs1[2]);
  ASSERT_FALSE(tracker.LookupSessionTab(tag1, 3, &tab_ptr));
  ASSERT_EQ(static_cast<const sessions::SessionTab*>(NULL), tab_ptr);

  std::vector<const sessions::SessionWindow*> windows;
  ASSERT_TRUE(tracker.LookupSessionWindows(tag1, &windows));
  ASSERT_EQ(1U, windows.size());
  ASSERT_TRUE(tracker.LookupSessionWindows(tag2, &windows));
  ASSERT_EQ(0U, windows.size());

  // The sessions don't have valid tabs, lookup should not succeed.
  std::vector<const sync_driver::SyncedSession*> sessions;
  ASSERT_FALSE(tracker.LookupAllForeignSessions(&sessions));

  tracker.Clear();
  ASSERT_EQ(0U, tracker.num_synced_tabs(tag1));
  ASSERT_EQ(0U, tracker.num_synced_tabs(tag2));
  ASSERT_EQ(0U, tracker.num_synced_sessions());
}

TEST_F(SyncedSessionTrackerTest, ManyGetTabs) {
  SyncedSessionTracker tracker;
  ASSERT_TRUE(tracker.Empty());
  const int kMaxSessions = 10;
  const int kMaxTabs = 1000;
  const int kMaxAttempts = 10000;
  for (int j=0; j<kMaxSessions; ++j) {
    std::string tag = base::StringPrintf("tag%d", j);
    for (int i=0; i<kMaxAttempts; ++i) {
      // More attempts than tabs means we'll sometimes get the same tabs,
      // sometimes have to allocate new tabs.
      int rand_tab_num = base::RandInt(0, kMaxTabs);
      sessions::SessionTab* tab =
          tracker.GetTab(tag, rand_tab_num, rand_tab_num + 1);
      ASSERT_TRUE(tab);
    }
  }
}

TEST_F(SyncedSessionTrackerTest, LookupTabNodeIds) {
  SyncedSessionTracker tracker;
  std::set<int> result;
  std::string tag1 = "session1";
  std::string tag2 = "session2";
  std::string tag3 = "session3";

  tracker.GetTab(tag1, 1, 1);
  tracker.GetTab(tag1, 2, 2);
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag1, &result));
  EXPECT_EQ(2U, result.size());
  EXPECT_FALSE(result.end() == result.find(1));
  EXPECT_FALSE(result.end() == result.find(2));
  EXPECT_FALSE(tracker.LookupTabNodeIds(tag2, &result));

  tracker.PutWindowInSession(tag1, 0);
  tracker.PutTabInWindow(tag1, 0, 3, 0);
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag1, &result));
  EXPECT_EQ(2U, result.size());

  tracker.GetTab(tag1, 3, 3);
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag1, &result));
  EXPECT_EQ(3U, result.size());
  EXPECT_FALSE(result.end() == result.find(3));

  tracker.GetTab(tag2, 1, 21);
  tracker.GetTab(tag2, 2, 22);
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag2, &result));
  EXPECT_EQ(2U, result.size());
  EXPECT_FALSE(result.end() == result.find(21));
  EXPECT_FALSE(result.end() == result.find(22));
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag1, &result));
  EXPECT_EQ(3U, result.size());
  EXPECT_FALSE(result.end() == result.find(1));
  EXPECT_FALSE(result.end() == result.find(2));

  EXPECT_FALSE(tracker.LookupTabNodeIds(tag3, &result));
  tracker.PutWindowInSession(tag3, 1);
  tracker.PutTabInWindow(tag3, 1, 5, 0);
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag3, &result));
  EXPECT_TRUE(result.empty());
  EXPECT_TRUE(tracker.DeleteSession(tag3));
  EXPECT_FALSE(tracker.LookupTabNodeIds(tag3, &result));

  EXPECT_TRUE(tracker.DeleteSession(tag1));
  EXPECT_FALSE(tracker.LookupTabNodeIds(tag1, &result));
  EXPECT_TRUE(tracker.LookupTabNodeIds(tag2, &result));
  EXPECT_EQ(2U, result.size());
  EXPECT_FALSE(result.end() == result.find(21));
  EXPECT_FALSE(result.end() == result.find(22));
  EXPECT_TRUE(tracker.DeleteSession(tag2));
  EXPECT_FALSE(tracker.LookupTabNodeIds(tag2, &result));
}

TEST_F(SyncedSessionTrackerTest, SessionTracking) {
  SyncedSessionTracker tracker;
  ASSERT_TRUE(tracker.Empty());
  std::string tag1 = "tag1";
  std::string tag2 = "tag2";

  // Create some session information that is stale.
  sync_driver::SyncedSession* session1 = tracker.GetSession(tag1);
  tracker.PutWindowInSession(tag1, 0);
  tracker.PutTabInWindow(tag1, 0, 0, 0);
  tracker.PutTabInWindow(tag1, 0, 1, 1);
  tracker.GetTab(tag1, 2, 3U)->window_id.set_id(0);  // Will be unmapped.
  tracker.GetTab(tag1, 3, 4U)->window_id.set_id(0);  // Will be unmapped.
  tracker.PutWindowInSession(tag1, 1);
  tracker.PutTabInWindow(tag1, 1, 4, 0);
  tracker.PutTabInWindow(tag1, 1, 5, 1);
  ASSERT_EQ(2U, session1->windows.size());
  ASSERT_EQ(2U, session1->windows[0]->tabs.size());
  ASSERT_EQ(2U, session1->windows[1]->tabs.size());
  ASSERT_EQ(6U, tracker.num_synced_tabs(tag1));

  // Create a session that should not be affected.
  sync_driver::SyncedSession* session2 = tracker.GetSession(tag2);
  tracker.PutWindowInSession(tag2, 2);
  tracker.PutTabInWindow(tag2, 2, 1, 0);
  ASSERT_EQ(1U, session2->windows.size());
  ASSERT_EQ(1U, session2->windows[2]->tabs.size());
  ASSERT_EQ(1U, tracker.num_synced_tabs(tag2));

  // Reset tracking and get the current windows/tabs.
  // We simulate moving a tab from one window to another, then closing the
  // first window (including its one remaining tab), and opening a new tab
  // on the remaining window.

  // New tab, arrived before meta node so unmapped.
  tracker.GetTab(tag1, 6, 7U);
  tracker.ResetSessionTracking(tag1);
  tracker.PutWindowInSession(tag1, 0);
  tracker.PutTabInWindow(tag1, 0, 0, 0);
  // Tab 1 is closed.
  tracker.PutTabInWindow(tag1, 0, 2, 1);  // No longer unmapped.
  // Tab 3 was unmapped and does not get used.
  tracker.PutTabInWindow(tag1, 0, 4, 2);  // Moved from window 1.
  // Window 1 was closed, along with tab 5.
  tracker.PutTabInWindow(tag1, 0, 6, 3);  // No longer unmapped.
  // Session 2 should not be affected.
  tracker.CleanupSession(tag1);

  // Verify that only those parts of the session not owned have been removed.
  ASSERT_EQ(1U, session1->windows.size());
  ASSERT_EQ(4U, session1->windows[0]->tabs.size());
  ASSERT_EQ(1U, session2->windows.size());
  ASSERT_EQ(1U, session2->windows[2]->tabs.size());
  ASSERT_EQ(2U, tracker.num_synced_sessions());
  ASSERT_EQ(4U, tracker.num_synced_tabs(tag1));
  ASSERT_EQ(1U, tracker.num_synced_tabs(tag2));

  // All memory should be properly deallocated by destructor for the
  // SyncedSessionTracker.
}

}  // namespace browser_sync