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.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/rand_util.h"
10 #include "base/stringprintf.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/sessions/session_types.h"
13 #include "chrome/browser/sessions/session_types_test_helper.h"
14 #include "chrome/browser/sync/glue/synced_session_tracker.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace browser_sync
{
19 typedef testing::Test SyncedSessionTrackerTest
;
21 TEST_F(SyncedSessionTrackerTest
, GetSession
) {
22 SyncedSessionTracker tracker
;
23 SyncedSession
* session1
= tracker
.GetSession("tag");
24 SyncedSession
* session2
= tracker
.GetSession("tag2");
25 ASSERT_EQ(session1
, tracker
.GetSession("tag"));
26 ASSERT_NE(session1
, session2
);
27 // Should clean up memory on its own.
30 TEST_F(SyncedSessionTrackerTest
, GetTabUnmapped
) {
31 SyncedSessionTracker tracker
;
32 SessionTab
* tab
= tracker
.GetTab("tag", 0);
33 ASSERT_EQ(tab
, tracker
.GetTab("tag", 0));
34 // Should clean up memory on its own.
37 TEST_F(SyncedSessionTrackerTest
, PutWindowInSession
) {
38 SyncedSessionTracker tracker
;
39 tracker
.PutWindowInSession("tag", 0);
40 SyncedSession
* session
= tracker
.GetSession("tag");
41 ASSERT_EQ(1U, session
->windows
.size());
42 // Should clean up memory on its own.
45 TEST_F(SyncedSessionTrackerTest
, PutTabInWindow
) {
46 SyncedSessionTracker tracker
;
47 tracker
.PutWindowInSession("tag", 10);
48 tracker
.PutTabInWindow("tag", 10, 15, 0); // win id 10, tab id 15, tab ind 0.
49 SyncedSession
* session
= tracker
.GetSession("tag");
50 ASSERT_EQ(1U, session
->windows
.size());
51 ASSERT_EQ(1U, session
->windows
[10]->tabs
.size());
52 ASSERT_EQ(tracker
.GetTab("tag", 15), session
->windows
[10]->tabs
[0]);
53 // Should clean up memory on its own.
56 TEST_F(SyncedSessionTrackerTest
, LookupAllForeignSessions
) {
57 SyncedSessionTracker tracker
;
58 std::vector
<const SyncedSession
*> sessions
;
59 ASSERT_FALSE(tracker
.LookupAllForeignSessions(&sessions
));
60 tracker
.GetSession("tag1");
61 tracker
.GetSession("tag2");
62 tracker
.PutWindowInSession("tag1", 0);
63 tracker
.PutTabInWindow("tag1", 0, 15, 0);
64 SessionTab
* tab
= tracker
.GetTab("tag1", 15);
66 tab
->navigations
.push_back(SessionTypesTestHelper::CreateNavigation(
67 "bla://valid_url", "title"));
68 ASSERT_TRUE(tracker
.LookupAllForeignSessions(&sessions
));
69 // Only the session with a valid window and tab gets returned.
70 ASSERT_EQ(1U, sessions
.size());
71 ASSERT_EQ("tag1", sessions
[0]->session_tag
);
74 TEST_F(SyncedSessionTrackerTest
, LookupSessionWindows
) {
75 SyncedSessionTracker tracker
;
76 std::vector
<const SessionWindow
*> windows
;
77 ASSERT_FALSE(tracker
.LookupSessionWindows("tag1", &windows
));
78 tracker
.GetSession("tag1");
79 tracker
.PutWindowInSession("tag1", 0);
80 tracker
.PutWindowInSession("tag1", 2);
81 tracker
.GetSession("tag2");
82 tracker
.PutWindowInSession("tag2", 0);
83 tracker
.PutWindowInSession("tag2", 2);
84 ASSERT_TRUE(tracker
.LookupSessionWindows("tag1", &windows
));
85 ASSERT_EQ(2U, windows
.size()); // Only windows from tag1 session.
86 ASSERT_NE((SessionWindow
*)NULL
, windows
[0]);
87 ASSERT_NE((SessionWindow
*)NULL
, windows
[1]);
88 ASSERT_NE(windows
[1], windows
[0]);
91 TEST_F(SyncedSessionTrackerTest
, LookupSessionTab
) {
92 SyncedSessionTracker tracker
;
93 const SessionTab
* tab
;
94 ASSERT_FALSE(tracker
.LookupSessionTab("tag1", 5, &tab
));
95 tracker
.GetSession("tag1");
96 tracker
.PutWindowInSession("tag1", 0);
97 tracker
.PutTabInWindow("tag1", 0, 5, 0);
98 ASSERT_TRUE(tracker
.LookupSessionTab("tag1", 5, &tab
));
99 ASSERT_NE((SessionTab
*)NULL
, tab
);
102 TEST_F(SyncedSessionTrackerTest
, Complex
) {
103 const std::string tag1
= "tag";
104 const std::string tag2
= "tag2";
105 const std::string tag3
= "tag3";
106 SyncedSessionTracker tracker
;
107 std::vector
<SessionTab
*> tabs1
, tabs2
;
108 SessionTab
* temp_tab
;
109 ASSERT_TRUE(tracker
.Empty());
110 ASSERT_EQ(0U, tracker
.num_synced_sessions());
111 ASSERT_EQ(0U, tracker
.num_synced_tabs(tag1
));
112 tabs1
.push_back(tracker
.GetTab(tag1
, 0));
113 tabs1
.push_back(tracker
.GetTab(tag1
, 1));
114 tabs1
.push_back(tracker
.GetTab(tag1
, 2));
115 ASSERT_EQ(3U, tracker
.num_synced_tabs(tag1
));
116 ASSERT_EQ(0U, tracker
.num_synced_sessions());
117 temp_tab
= tracker
.GetTab(tag1
, 0); // Already created.
118 ASSERT_EQ(3U, tracker
.num_synced_tabs(tag1
));
119 ASSERT_EQ(0U, tracker
.num_synced_sessions());
120 ASSERT_EQ(tabs1
[0], temp_tab
);
121 tabs2
.push_back(tracker
.GetTab(tag2
, 0));
122 ASSERT_EQ(1U, tracker
.num_synced_tabs(tag2
));
123 ASSERT_EQ(0U, tracker
.num_synced_sessions());
124 ASSERT_FALSE(tracker
.DeleteSession(tag3
));
126 SyncedSession
* session
= tracker
.GetSession(tag1
);
127 SyncedSession
* session2
= tracker
.GetSession(tag2
);
128 SyncedSession
* session3
= tracker
.GetSession(tag3
);
129 ASSERT_EQ(3U, tracker
.num_synced_sessions());
131 ASSERT_TRUE(session
);
132 ASSERT_TRUE(session2
);
133 ASSERT_TRUE(session3
);
134 ASSERT_NE(session
, session2
);
135 ASSERT_NE(session2
, session3
);
136 ASSERT_TRUE(tracker
.DeleteSession(tag3
));
137 ASSERT_EQ(2U, tracker
.num_synced_sessions());
139 tracker
.PutWindowInSession(tag1
, 0); // Create a window.
140 tracker
.PutTabInWindow(tag1
, 0, 2, 0); // No longer unmapped.
141 ASSERT_EQ(3U, tracker
.num_synced_tabs(tag1
)); // Has not changed.
143 const SessionTab
*tab_ptr
;
144 ASSERT_TRUE(tracker
.LookupSessionTab(tag1
, 0, &tab_ptr
));
145 ASSERT_EQ(tab_ptr
, tabs1
[0]);
146 ASSERT_TRUE(tracker
.LookupSessionTab(tag1
, 2, &tab_ptr
));
147 ASSERT_EQ(tab_ptr
, tabs1
[2]);
148 ASSERT_FALSE(tracker
.LookupSessionTab(tag1
, 3, &tab_ptr
));
149 ASSERT_EQ(static_cast<const SessionTab
*>(NULL
), tab_ptr
);
151 std::vector
<const SessionWindow
*> windows
;
152 ASSERT_TRUE(tracker
.LookupSessionWindows(tag1
, &windows
));
153 ASSERT_EQ(1U, windows
.size());
154 ASSERT_TRUE(tracker
.LookupSessionWindows(tag2
, &windows
));
155 ASSERT_EQ(0U, windows
.size());
157 // The sessions don't have valid tabs, lookup should not succeed.
158 std::vector
<const SyncedSession
*> sessions
;
159 ASSERT_FALSE(tracker
.LookupAllForeignSessions(&sessions
));
162 ASSERT_EQ(0U, tracker
.num_synced_tabs(tag1
));
163 ASSERT_EQ(0U, tracker
.num_synced_tabs(tag2
));
164 ASSERT_EQ(0U, tracker
.num_synced_sessions());
167 TEST_F(SyncedSessionTrackerTest
, ManyGetTabs
) {
168 SyncedSessionTracker tracker
;
169 ASSERT_TRUE(tracker
.Empty());
170 const int kMaxSessions
= 10;
171 const int kMaxTabs
= 1000;
172 const int kMaxAttempts
= 10000;
173 for (int j
=0; j
<kMaxSessions
; ++j
) {
174 std::string tag
= base::StringPrintf("tag%d", j
);
175 for (int i
=0; i
<kMaxAttempts
; ++i
) {
176 // More attempts than tabs means we'll sometimes get the same tabs,
177 // sometimes have to allocate new tabs.
178 int rand_tab_num
= base::RandInt(0, kMaxTabs
);
179 SessionTab
* tab
= tracker
.GetTab(tag
, rand_tab_num
);
185 TEST_F(SyncedSessionTrackerTest
, SessionTracking
) {
186 SyncedSessionTracker tracker
;
187 ASSERT_TRUE(tracker
.Empty());
188 std::string tag1
= "tag1";
189 std::string tag2
= "tag2";
191 // Create some session information that is stale.
192 SyncedSession
* session1
= tracker
.GetSession(tag1
);
193 tracker
.PutWindowInSession(tag1
, 0);
194 tracker
.PutTabInWindow(tag1
, 0, 0, 0);
195 tracker
.PutTabInWindow(tag1
, 0, 1, 1);
196 tracker
.GetTab(tag1
, 2)->window_id
.set_id(0); // Will be an unmapped tab.
197 tracker
.GetTab(tag1
, 3)->window_id
.set_id(0); // Will be an unmapped tab.
198 tracker
.PutWindowInSession(tag1
, 1);
199 tracker
.PutTabInWindow(tag1
, 1, 4, 0);
200 tracker
.PutTabInWindow(tag1
, 1, 5, 1);
201 ASSERT_EQ(2U, session1
->windows
.size());
202 ASSERT_EQ(2U, session1
->windows
[0]->tabs
.size());
203 ASSERT_EQ(2U, session1
->windows
[1]->tabs
.size());
204 ASSERT_EQ(6U, tracker
.num_synced_tabs(tag1
));
206 // Create a session that should not be affected.
207 SyncedSession
* session2
= tracker
.GetSession(tag2
);
208 tracker
.PutWindowInSession(tag2
, 2);
209 tracker
.PutTabInWindow(tag2
, 2, 1, 0);
210 ASSERT_EQ(1U, session2
->windows
.size());
211 ASSERT_EQ(1U, session2
->windows
[2]->tabs
.size());
212 ASSERT_EQ(1U, tracker
.num_synced_tabs(tag2
));
214 // Reset tracking and get the current windows/tabs.
215 // We simulate moving a tab from one window to another, then closing the first
216 // window (including its one remaining tab), and opening a new tab on the
218 tracker
.GetTab(tag1
, 6); // New tab, arrived before meta node so unmapped.
219 tracker
.ResetSessionTracking(tag1
);
220 tracker
.PutWindowInSession(tag1
, 0);
221 tracker
.PutTabInWindow(tag1
, 0, 0, 0);
223 tracker
.PutTabInWindow(tag1
, 0, 2, 1); // No longer unmapped.
224 // Tab 3 was unmapped and does not get used.
225 tracker
.PutTabInWindow(tag1
, 0, 4, 2); // Moved from window 1.
226 // Window 1 was closed, along with tab 5.
227 tracker
.PutTabInWindow(tag1
, 0, 6, 3); // No longer unmapped.
228 // Session 2 should not be affected.
229 tracker
.CleanupSession(tag1
);
231 // Verify that only those parts of the session not owned have been removed.
232 ASSERT_EQ(1U, session1
->windows
.size());
233 ASSERT_EQ(4U, session1
->windows
[0]->tabs
.size());
234 ASSERT_EQ(1U, session2
->windows
.size());
235 ASSERT_EQ(1U, session2
->windows
[2]->tabs
.size());
236 ASSERT_EQ(2U, tracker
.num_synced_sessions());
237 ASSERT_EQ(4U, tracker
.num_synced_tabs(tag1
));
238 ASSERT_EQ(1U, tracker
.num_synced_tabs(tag2
));
240 // All memory should be properly deallocated by destructor for the
241 // SyncedSessionTracker.
244 } // namespace browser_sync