Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: chrome/browser/extensions/api/sessions/sessions_apitest.cc

Issue 21022018: Sessions API - previously Session Restore API. Supports restoring currently open foreign windows an… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updated Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/sessions/sessions_api.h"
6
7 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/sync/glue/session_model_associator.h"
14 #include "chrome/browser/sync/profile_sync_service.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "chrome/browser/sync/profile_sync_service_mock.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/testing_browser_process.h"
20
21 namespace utils = extension_function_test_utils;
22
23 namespace extensions {
24
25 namespace {
26
27 // If more sessions are added to session tags, num sessions should be updated.
28 const char* kSessionTags[] = {"tag0", "tag1", "tag2", "tag3", "tag4"};
29 const size_t kNumSessions = 5;
30
31 void BuildSessionSpecifics(const std::string& tag,
32 sync_pb::SessionSpecifics* meta) {
33 meta->set_session_tag(tag);
34 sync_pb::SessionHeader* header = meta->mutable_header();
35 header->set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
36 header->set_client_name(tag);
37 }
38
39 void BuildWindowSpecifics(int window_id,
40 const std::vector<int>& tab_list,
41 sync_pb::SessionSpecifics* meta) {
42 sync_pb::SessionHeader* header = meta->mutable_header();
43 sync_pb::SessionWindow* window = header->add_window();
44 window->set_window_id(window_id);
45 window->set_selected_tab_index(0);
46 window->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED);
47 for (std::vector<int>::const_iterator iter = tab_list.begin();
48 iter != tab_list.end(); ++iter) {
49 window->add_tab(*iter);
50 }
51 }
52
53 void BuildTabSpecifics(const std::string& tag, int window_id, int tab_id,
54 sync_pb::SessionSpecifics* tab_base) {
55 tab_base->set_session_tag(tag);
56 tab_base->set_tab_node_id(0);
57 sync_pb::SessionTab* tab = tab_base->mutable_tab();
58 tab->set_tab_id(tab_id);
59 tab->set_tab_visual_index(1);
60 tab->set_current_navigation_index(0);
61 tab->set_pinned(true);
62 tab->set_extension_app_id("app_id");
63 sync_pb::TabNavigation* navigation = tab->add_navigation();
64 navigation->set_virtual_url("http://foo/1");
65 navigation->set_referrer("referrer");
66 navigation->set_title("title");
67 navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
68 }
69
70 } // namespace
71
72 class ExtensionSessionsTest : public InProcessBrowserTest {
73 public:
74 virtual void SetUpOnMainThread() OVERRIDE;
75 protected:
76 void CreateTestProfileSyncService();
77 void CreateTestExtension();
78 void CreateSessionModels();
79
80 template <class T>
81 scoped_refptr<T> CreateFunction(bool has_callback) {
82 scoped_refptr<T> fn(new T());
83 fn->set_extension(extension_.get());
84 fn->set_has_callback(has_callback);
85 return fn;
86 };
87
88 Browser* browser_;
89 browser_sync::SessionModelAssociator* associator_;
90 scoped_refptr<extensions::Extension> extension_;
91 };
92
93 void ExtensionSessionsTest::SetUpOnMainThread() {
94 CreateTestProfileSyncService();
95 CreateTestExtension();
96 }
97
98 void ExtensionSessionsTest::CreateTestProfileSyncService() {
99 ProfileManager* profile_manager = g_browser_process->profile_manager();
100 base::FilePath path;
101 PathService::Get(chrome::DIR_USER_DATA, &path);
102 path = path.AppendASCII("test_profile");
103 if (!base::PathExists(path))
104 CHECK(file_util::CreateDirectory(path));
105 Profile* profile =
106 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
107 profile_manager->RegisterTestingProfile(profile, true, false);
108 browser_ = new Browser(Browser::CreateParams(
109 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
110 ProfileSyncServiceMock* service = static_cast<ProfileSyncServiceMock*>(
111 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
112 profile, &ProfileSyncServiceMock::BuildMockProfileSyncService));
113
114 associator_ = new browser_sync::SessionModelAssociator(
115 static_cast<ProfileSyncService*>(service), true);
116 syncer::ModelTypeSet preferred_types;
117 preferred_types.Put(syncer::SESSIONS);
118 GoogleServiceAuthError no_error(GoogleServiceAuthError::NONE);
119
120 ON_CALL(*service, GetSessionModelAssociator()).WillByDefault(
121 testing::Return(associator_));
122 ON_CALL(*service, GetPreferredDataTypes()).WillByDefault(
123 testing::Return(preferred_types));
124 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(
125 testing::ReturnRef(no_error));
126 ON_CALL(*service, GetActiveDataTypes()).WillByDefault(
127 testing::Return(preferred_types));
128 EXPECT_CALL(*service, AddObserver(testing::_)).Times(testing::AnyNumber());
129 EXPECT_CALL(*service, RemoveObserver(testing::_)).Times(testing::AnyNumber());
130
131 service->Initialize();
132 }
133
134 void ExtensionSessionsTest::CreateTestExtension() {
135 scoped_ptr<base::DictionaryValue> test_extension_value(
136 utils::ParseDictionary(
137 "{\"name\": \"Test\", \"version\": \"1.0\", "
138 "\"permissions\": [\"sessions\", \"tabs\"]}"));
139 extension_ = utils::CreateExtension(test_extension_value.get());
140 }
141
142 void ExtensionSessionsTest::CreateSessionModels() {
143 for (size_t index = 0; index < kNumSessions; ++index) {
144 // Fill an instance of session specifics with a foreign session's data.
145 sync_pb::SessionSpecifics meta;
146 BuildSessionSpecifics(kSessionTags[index], &meta);
147 SessionID::id_type tab_nums1[] = {5, 10, 13, 17};
148 std::vector<SessionID::id_type> tab_list1(
149 tab_nums1, tab_nums1 + arraysize(tab_nums1));
150 BuildWindowSpecifics(index, tab_list1, &meta);
151 std::vector<sync_pb::SessionSpecifics> tabs1;
152 tabs1.resize(tab_list1.size());
153 for (size_t i = 0; i < tab_list1.size(); ++i) {
154 BuildTabSpecifics(kSessionTags[index], 0, tab_list1[i], &tabs1[i]);
155 }
156
157 associator_->SetCurrentMachineTagForTesting(kSessionTags[index]);
158 // Update associator with the session's meta node containing one window.
159 associator_->AssociateForeignSpecifics(meta, base::Time());
160 // Add tabs for the window.
161 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs1.begin();
162 iter != tabs1.end(); ++iter) {
163 associator_->AssociateForeignSpecifics(*iter, base::Time());
164 }
165 }
166 }
167
168 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevices) {
169 CreateSessionModels();
170
171 scoped_ptr<base::ListValue> result(utils::ToList(
172 utils::RunFunctionAndReturnSingleResult(
173 CreateFunction<SessionsGetDevicesFunction>(true).get(),
174 "[{\"maxResults\": 3}]",
175 browser_)));
176 ASSERT_TRUE(result);
177 ListValue* devices = result.get();
178 EXPECT_EQ(3u, devices->GetSize());
179 DictionaryValue* device = NULL;
180 for (size_t i = 0; i < devices->GetSize(); ++i) {
181 EXPECT_TRUE(devices->GetDictionary(i, &device));
182 EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
183 }
184 }
185
186 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesMaxResults) {
187 CreateSessionModels();
188
189 scoped_ptr<base::ListValue> result(utils::ToList(
190 utils::RunFunctionAndReturnSingleResult(
191 CreateFunction<SessionsGetDevicesFunction>(true).get(),
192 "[]",
193 browser_)));
194 ASSERT_TRUE(result);
195 ListValue* devices = result.get();
196 EXPECT_EQ(5u, devices->GetSize());
197 DictionaryValue* device = NULL;
198 for (size_t i = 0; i < devices->GetSize(); ++i) {
199 EXPECT_TRUE(devices->GetDictionary(i, &device));
200 EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
201 }
202 }
203
204 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesListEmpty) {
205 scoped_ptr<base::ListValue> result(utils::ToList(
206 utils::RunFunctionAndReturnSingleResult(
207 CreateFunction<SessionsGetDevicesFunction>(true).get(),
208 "[{\"maxResults\": 3}]",
209 browser_)));
210
211 ASSERT_TRUE(result);
212 ListValue* devices = result.get();
213 EXPECT_EQ(0u, devices->GetSize());
214 }
215
216 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, RestoreForeignSessionWindow) {
217 CreateSessionModels();
218
219 scoped_ptr<base::DictionaryValue> restored_window_session(utils::ToDictionary(
220 utils::RunFunctionAndReturnSingleResult(
221 CreateFunction<SessionsRestoreFunction>(true).get(),
222 "[\"tag3.3\"]",
223 browser_,
224 utils::INCLUDE_INCOGNITO)));
225 ASSERT_TRUE(restored_window_session);
226
227 scoped_ptr<base::ListValue> result(utils::ToList(
228 utils::RunFunctionAndReturnSingleResult(
229 CreateFunction<WindowsGetAllFunction>(true).get(),
230 "[]",
231 browser_)));
232 ASSERT_TRUE(result);
233
234 ListValue* windows = result.get();
235 EXPECT_EQ(2u, windows->GetSize());
236 DictionaryValue* restored_window = NULL;
237 EXPECT_TRUE(restored_window_session->GetDictionary("window",
238 &restored_window));
239 DictionaryValue* window = NULL;
240 int restored_id = utils::GetInteger(restored_window, "id");
241 for (size_t i = 0; i < windows->GetSize(); ++i) {
242 EXPECT_TRUE(windows->GetDictionary(i, &window));
243 if (utils::GetInteger(window, "id") == restored_id)
244 break;
245 }
246 EXPECT_EQ(restored_id, utils::GetInteger(window, "id"));
247 }
248
249 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, RestoreForeignSessionTab) {
250 CreateSessionModels();
251
252 EXPECT_TRUE(utils::RunFunction(
253 CreateFunction<SessionsRestoreFunction>(true).get(),
254 "[\"tag1.1\"]",
255 browser_,
256 utils::INCLUDE_INCOGNITO));
257 scoped_ptr<base::ListValue> windows_before(utils::ToList(
258 utils::RunFunctionAndReturnSingleResult(
259 CreateFunction<WindowsGetAllFunction>(true).get(),
260 "[{\"populate\": true}]",
261 browser_)));
262 ASSERT_TRUE(windows_before);
263
264 EXPECT_EQ(2u, windows_before.get()->GetSize());
265 DictionaryValue* window_before = NULL;
266 EXPECT_TRUE(windows_before.get()->GetDictionary(1, &window_before));
267 ListValue* tabs_before = NULL;
268 EXPECT_TRUE(window_before->GetList("tabs", &tabs_before));
269 EXPECT_EQ(4u, tabs_before->GetSize());
270
271 scoped_ptr<base::DictionaryValue> restored_tab_session(utils::ToDictionary(
272 utils::RunFunctionAndReturnSingleResult(
273 CreateFunction<SessionsRestoreFunction>(true).get(),
274 "[\"tag0.5\"]",
275 browser_,
276 utils::INCLUDE_INCOGNITO)));
277 ASSERT_TRUE(restored_tab_session);
278
279 scoped_ptr<base::ListValue> result(utils::ToList(
280 utils::RunFunctionAndReturnSingleResult(
281 CreateFunction<WindowsGetAllFunction>(true).get(),
282 "[{\"populate\": true}]",
283 browser_)));
284 ASSERT_TRUE(result);
285 ListValue* windows = result.get();
286 EXPECT_EQ(2u, windows->GetSize());
287 DictionaryValue* window = NULL;
288 EXPECT_TRUE(windows->GetDictionary(1, &window));
289 ListValue* tabs = NULL;
290 EXPECT_TRUE(window->GetList("tabs", &tabs));
291 EXPECT_EQ(5u, tabs->GetSize());
292
293 DictionaryValue* restored_tab = NULL;
294 EXPECT_TRUE(restored_tab_session->GetDictionary("tab", &restored_tab));
295 int restored_id = utils::GetInteger(restored_tab, "id");
296 DictionaryValue* tab = NULL;
297 for (size_t i = 0; i < tabs->GetSize(); ++i) {
298 EXPECT_TRUE(tabs->GetDictionary(i, &tab));
299 if (utils::GetInteger(tab, "id") == restored_id)
300 break;
301 }
302 EXPECT_EQ(restored_id, utils::GetInteger(tab, "id"));
303 }
304
305 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, RestoreForeignSessionInvalidId) {
306 CreateSessionModels();
307
308 EXPECT_TRUE(MatchPattern(utils::RunFunctionAndReturnError(
309 CreateFunction<SessionsRestoreFunction>(true).get(),
310 "[\"tag3.0\"]",
311 browser_), "Session is not found."));
312 }
313
314 #if defined(OS_WIN) && defined(USE_ASH)
315 #include "base/win/windows_version.h"
316 #endif
317
318 // Flaky on ChromeOS, times out on OSX Debug http://crbug.com/251199
319 #if defined(OS_CHROMEOS) || (defined(OS_MACOSX) && !defined(NDEBUG))
320 #define MAYBE_SessionsApis DISABLED_SessionsApis
321 #else
322 #define MAYBE_SessionsApis SessionsApis
323 #endif
324 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_SessionsApis) {
325 #if defined(OS_WIN) && defined(USE_ASH)
326 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
327 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
328 return;
329 #endif
330
331 ASSERT_TRUE(RunExtensionSubtest("sessions",
332 "sessions.html")) << message_;
333 }
334
335 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698