OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 |
6 #include <utility> | 7 #include <utility> |
7 | 8 |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
11 #include "base/strings/pattern.h" | 13 #include "base/strings/pattern.h" |
12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
14 #include "chrome/browser/extensions/api/sessions/sessions_api.h" | 16 #include "chrome/browser/extensions/api/sessions/sessions_api.h" |
15 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 17 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
16 #include "chrome/browser/extensions/extension_apitest.h" | 18 #include "chrome/browser/extensions/extension_apitest.h" |
17 #include "chrome/browser/extensions/extension_function_test_utils.h" | 19 #include "chrome/browser/extensions/extension_function_test_utils.h" |
18 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
19 #include "chrome/browser/sync/chrome_sync_client.h" | 21 #include "chrome/browser/sync/chrome_sync_client.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 navigation->set_title("MyTitle"); | 91 navigation->set_title("MyTitle"); |
90 navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED); | 92 navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED); |
91 } | 93 } |
92 | 94 |
93 class ExtensionSessionsTest : public InProcessBrowserTest { | 95 class ExtensionSessionsTest : public InProcessBrowserTest { |
94 public: | 96 public: |
95 void SetUpCommandLine(base::CommandLine* command_line) override; | 97 void SetUpCommandLine(base::CommandLine* command_line) override; |
96 void SetUpOnMainThread() override; | 98 void SetUpOnMainThread() override; |
97 | 99 |
98 protected: | 100 protected: |
99 static scoped_ptr<KeyedService> BuildProfileSyncService( | 101 static std::unique_ptr<KeyedService> BuildProfileSyncService( |
100 content::BrowserContext* profile); | 102 content::BrowserContext* profile); |
101 | 103 |
102 void CreateTestProfileSyncService(); | 104 void CreateTestProfileSyncService(); |
103 void CreateTestExtension(); | 105 void CreateTestExtension(); |
104 void CreateSessionModels(); | 106 void CreateSessionModels(); |
105 | 107 |
106 template <class T> | 108 template <class T> |
107 scoped_refptr<T> CreateFunction(bool has_callback) { | 109 scoped_refptr<T> CreateFunction(bool has_callback) { |
108 scoped_refptr<T> fn(new T()); | 110 scoped_refptr<T> fn(new T()); |
109 fn->set_extension(extension_.get()); | 111 fn->set_extension(extension_.get()); |
(...skipping 10 matching lines...) Expand all Loading... |
120 command_line->AppendSwitch( | 122 command_line->AppendSwitch( |
121 chromeos::switches::kIgnoreUserProfileMappingForTests); | 123 chromeos::switches::kIgnoreUserProfileMappingForTests); |
122 #endif | 124 #endif |
123 } | 125 } |
124 | 126 |
125 void ExtensionSessionsTest::SetUpOnMainThread() { | 127 void ExtensionSessionsTest::SetUpOnMainThread() { |
126 CreateTestProfileSyncService(); | 128 CreateTestProfileSyncService(); |
127 CreateTestExtension(); | 129 CreateTestExtension(); |
128 } | 130 } |
129 | 131 |
130 scoped_ptr<KeyedService> ExtensionSessionsTest::BuildProfileSyncService( | 132 std::unique_ptr<KeyedService> ExtensionSessionsTest::BuildProfileSyncService( |
131 content::BrowserContext* context) { | 133 content::BrowserContext* context) { |
132 scoped_ptr<SyncApiComponentFactoryMock> factory( | 134 std::unique_ptr<SyncApiComponentFactoryMock> factory( |
133 new SyncApiComponentFactoryMock()); | 135 new SyncApiComponentFactoryMock()); |
134 | 136 |
135 factory->SetLocalDeviceInfoProvider( | 137 factory->SetLocalDeviceInfoProvider( |
136 scoped_ptr<sync_driver::LocalDeviceInfoProvider>( | 138 std::unique_ptr<sync_driver::LocalDeviceInfoProvider>( |
137 new sync_driver::LocalDeviceInfoProviderMock( | 139 new sync_driver::LocalDeviceInfoProviderMock( |
138 kSessionTags[0], | 140 kSessionTags[0], "machine name", "Chromium 10k", "Chrome 10k", |
139 "machine name", | 141 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id"))); |
140 "Chromium 10k", | |
141 "Chrome 10k", | |
142 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
143 "device_id"))); | |
144 | 142 |
145 Profile* profile = static_cast<Profile*>(context); | 143 Profile* profile = static_cast<Profile*>(context); |
146 ProfileSyncServiceMock* sync_service = | 144 ProfileSyncServiceMock* sync_service = |
147 new ProfileSyncServiceMock(CreateProfileSyncServiceParamsForTest( | 145 new ProfileSyncServiceMock(CreateProfileSyncServiceParamsForTest( |
148 make_scoped_ptr(new browser_sync::ChromeSyncClient(profile)), | 146 base::WrapUnique(new browser_sync::ChromeSyncClient(profile)), |
149 profile)); | 147 profile)); |
150 static_cast<browser_sync::ChromeSyncClient*>(sync_service->GetSyncClient()) | 148 static_cast<browser_sync::ChromeSyncClient*>(sync_service->GetSyncClient()) |
151 ->SetSyncApiComponentFactoryForTesting(std::move(factory)); | 149 ->SetSyncApiComponentFactoryForTesting(std::move(factory)); |
152 return make_scoped_ptr(sync_service); | 150 return base::WrapUnique(sync_service); |
153 } | 151 } |
154 | 152 |
155 void ExtensionSessionsTest::CreateTestProfileSyncService() { | 153 void ExtensionSessionsTest::CreateTestProfileSyncService() { |
156 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 154 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
157 base::FilePath path; | 155 base::FilePath path; |
158 PathService::Get(chrome::DIR_USER_DATA, &path); | 156 PathService::Get(chrome::DIR_USER_DATA, &path); |
159 path = path.AppendASCII("test_profile"); | 157 path = path.AppendASCII("test_profile"); |
160 if (!base::PathExists(path)) | 158 if (!base::PathExists(path)) |
161 CHECK(base::CreateDirectory(path)); | 159 CHECK(base::CreateDirectory(path)); |
162 Profile* profile = | 160 Profile* profile = |
(...skipping 19 matching lines...) Expand all Loading... |
182 | 180 |
183 EXPECT_CALL(*service, AddObserver(testing::_)).Times(testing::AnyNumber()); | 181 EXPECT_CALL(*service, AddObserver(testing::_)).Times(testing::AnyNumber()); |
184 EXPECT_CALL(*service, RemoveObserver(testing::_)).Times(testing::AnyNumber()); | 182 EXPECT_CALL(*service, RemoveObserver(testing::_)).Times(testing::AnyNumber()); |
185 | 183 |
186 browser_ = new Browser(Browser::CreateParams(profile)); | 184 browser_ = new Browser(Browser::CreateParams(profile)); |
187 | 185 |
188 service->Initialize(); | 186 service->Initialize(); |
189 } | 187 } |
190 | 188 |
191 void ExtensionSessionsTest::CreateTestExtension() { | 189 void ExtensionSessionsTest::CreateTestExtension() { |
192 scoped_ptr<base::DictionaryValue> test_extension_value( | 190 std::unique_ptr<base::DictionaryValue> test_extension_value( |
193 api_test_utils::ParseDictionary( | 191 api_test_utils::ParseDictionary( |
194 "{\"name\": \"Test\", \"version\": \"1.0\", " | 192 "{\"name\": \"Test\", \"version\": \"1.0\", " |
195 "\"permissions\": [\"sessions\", \"tabs\"]}")); | 193 "\"permissions\": [\"sessions\", \"tabs\"]}")); |
196 extension_ = api_test_utils::CreateExtension(test_extension_value.get()); | 194 extension_ = api_test_utils::CreateExtension(test_extension_value.get()); |
197 } | 195 } |
198 | 196 |
199 void ExtensionSessionsTest::CreateSessionModels() { | 197 void ExtensionSessionsTest::CreateSessionModels() { |
200 syncer::SyncDataList initial_data; | 198 syncer::SyncDataList initial_data; |
201 for (size_t index = 0; index < arraysize(kSessionTags); ++index) { | 199 for (size_t index = 0; index < arraysize(kSessionTags); ++index) { |
202 // Fill an instance of session specifics with a foreign session's data. | 200 // Fill an instance of session specifics with a foreign session's data. |
(...skipping 20 matching lines...) Expand all Loading... |
223 entity.mutable_session()->CopyFrom(tabs[i]); | 221 entity.mutable_session()->CopyFrom(tabs[i]); |
224 initial_data.push_back(syncer::SyncData::CreateRemoteData( | 222 initial_data.push_back(syncer::SyncData::CreateRemoteData( |
225 i + 2, | 223 i + 2, |
226 entity, | 224 entity, |
227 base::Time(), | 225 base::Time(), |
228 syncer::AttachmentIdList(), | 226 syncer::AttachmentIdList(), |
229 syncer::AttachmentServiceProxyForTest::Create())); | 227 syncer::AttachmentServiceProxyForTest::Create())); |
230 } | 228 } |
231 } | 229 } |
232 | 230 |
233 ProfileSyncServiceFactory::GetForProfile(browser_->profile())-> | 231 ProfileSyncServiceFactory::GetForProfile(browser_->profile()) |
234 GetSessionsSyncableService()-> | 232 ->GetSessionsSyncableService() |
235 MergeDataAndStartSyncing(syncer::SESSIONS, initial_data, | 233 ->MergeDataAndStartSyncing(syncer::SESSIONS, initial_data, |
236 scoped_ptr<syncer::SyncChangeProcessor>( | 234 std::unique_ptr<syncer::SyncChangeProcessor>( |
237 new syncer::FakeSyncChangeProcessor()), | 235 new syncer::FakeSyncChangeProcessor()), |
238 scoped_ptr<syncer::SyncErrorFactory>( | 236 std::unique_ptr<syncer::SyncErrorFactory>( |
239 new syncer::SyncErrorFactoryMock())); | 237 new syncer::SyncErrorFactoryMock())); |
240 } | 238 } |
241 | 239 |
242 testing::AssertionResult CheckSessionModels(const base::ListValue& devices, | 240 testing::AssertionResult CheckSessionModels(const base::ListValue& devices, |
243 size_t num_sessions) { | 241 size_t num_sessions) { |
244 EXPECT_EQ(5u, devices.GetSize()); | 242 EXPECT_EQ(5u, devices.GetSize()); |
245 const base::DictionaryValue* device = NULL; | 243 const base::DictionaryValue* device = NULL; |
246 const base::ListValue* sessions = NULL; | 244 const base::ListValue* sessions = NULL; |
247 for (size_t i = 0; i < devices.GetSize(); ++i) { | 245 for (size_t i = 0; i < devices.GetSize(); ++i) { |
248 EXPECT_TRUE(devices.GetDictionary(i, &device)); | 246 EXPECT_TRUE(devices.GetDictionary(i, &device)); |
249 EXPECT_EQ(kSessionTags[i], api_test_utils::GetString(device, "info")); | 247 EXPECT_EQ(kSessionTags[i], api_test_utils::GetString(device, "info")); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 api_test_utils::GetString(tab, "favIconUrl")); | 281 api_test_utils::GetString(tab, "favIconUrl")); |
284 EXPECT_EQ(base::StringPrintf("%s.%d", kSessionTags[i], kTabIDs[j]), | 282 EXPECT_EQ(base::StringPrintf("%s.%d", kSessionTags[i], kTabIDs[j]), |
285 api_test_utils::GetString(tab, "sessionId")); | 283 api_test_utils::GetString(tab, "sessionId")); |
286 } | 284 } |
287 } | 285 } |
288 return testing::AssertionSuccess(); | 286 return testing::AssertionSuccess(); |
289 } | 287 } |
290 | 288 |
291 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevices) { | 289 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevices) { |
292 CreateSessionModels(); | 290 CreateSessionModels(); |
293 scoped_ptr<base::ListValue> result(utils::ToList( | 291 std::unique_ptr<base::ListValue> result( |
294 utils::RunFunctionAndReturnSingleResult( | 292 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
295 CreateFunction<SessionsGetDevicesFunction>(true).get(), | 293 CreateFunction<SessionsGetDevicesFunction>(true).get(), |
296 "[{\"maxResults\": 0}]", | 294 "[{\"maxResults\": 0}]", browser_))); |
297 browser_))); | |
298 ASSERT_TRUE(result); | 295 ASSERT_TRUE(result); |
299 EXPECT_TRUE(CheckSessionModels(*result, 0u)); | 296 EXPECT_TRUE(CheckSessionModels(*result, 0u)); |
300 } | 297 } |
301 | 298 |
302 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesMaxResults) { | 299 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesMaxResults) { |
303 CreateSessionModels(); | 300 CreateSessionModels(); |
304 scoped_ptr<base::ListValue> result(utils::ToList( | 301 std::unique_ptr<base::ListValue> result( |
305 utils::RunFunctionAndReturnSingleResult( | 302 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
306 CreateFunction<SessionsGetDevicesFunction>(true).get(), | 303 CreateFunction<SessionsGetDevicesFunction>(true).get(), "[]", |
307 "[]", | |
308 browser_))); | 304 browser_))); |
309 ASSERT_TRUE(result); | 305 ASSERT_TRUE(result); |
310 EXPECT_TRUE(CheckSessionModels(*result, 1u)); | 306 EXPECT_TRUE(CheckSessionModels(*result, 1u)); |
311 } | 307 } |
312 | 308 |
313 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesListEmpty) { | 309 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesListEmpty) { |
314 scoped_ptr<base::ListValue> result(utils::ToList( | 310 std::unique_ptr<base::ListValue> result( |
315 utils::RunFunctionAndReturnSingleResult( | 311 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
316 CreateFunction<SessionsGetDevicesFunction>(true).get(), | 312 CreateFunction<SessionsGetDevicesFunction>(true).get(), "[]", |
317 "[]", | |
318 browser_))); | 313 browser_))); |
319 | 314 |
320 ASSERT_TRUE(result); | 315 ASSERT_TRUE(result); |
321 base::ListValue* devices = result.get(); | 316 base::ListValue* devices = result.get(); |
322 EXPECT_EQ(0u, devices->GetSize()); | 317 EXPECT_EQ(0u, devices->GetSize()); |
323 } | 318 } |
324 | 319 |
325 // Flaky timeout: http://crbug.com/278372 | 320 // Flaky timeout: http://crbug.com/278372 |
326 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, | 321 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, |
327 DISABLED_RestoreForeignSessionWindow) { | 322 DISABLED_RestoreForeignSessionWindow) { |
328 CreateSessionModels(); | 323 CreateSessionModels(); |
329 | 324 |
330 scoped_ptr<base::DictionaryValue> restored_window_session(utils::ToDictionary( | 325 std::unique_ptr<base::DictionaryValue> restored_window_session( |
331 utils::RunFunctionAndReturnSingleResult( | 326 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( |
332 CreateFunction<SessionsRestoreFunction>(true).get(), | 327 CreateFunction<SessionsRestoreFunction>(true).get(), "[\"tag3.3\"]", |
333 "[\"tag3.3\"]", | 328 browser_, utils::INCLUDE_INCOGNITO))); |
334 browser_, | |
335 utils::INCLUDE_INCOGNITO))); | |
336 ASSERT_TRUE(restored_window_session); | 329 ASSERT_TRUE(restored_window_session); |
337 | 330 |
338 scoped_ptr<base::ListValue> result(utils::ToList( | 331 std::unique_ptr<base::ListValue> result( |
339 utils::RunFunctionAndReturnSingleResult( | 332 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
340 CreateFunction<WindowsGetAllFunction>(true).get(), | 333 CreateFunction<WindowsGetAllFunction>(true).get(), "[]", browser_))); |
341 "[]", | |
342 browser_))); | |
343 ASSERT_TRUE(result); | 334 ASSERT_TRUE(result); |
344 | 335 |
345 base::ListValue* windows = result.get(); | 336 base::ListValue* windows = result.get(); |
346 EXPECT_EQ(2u, windows->GetSize()); | 337 EXPECT_EQ(2u, windows->GetSize()); |
347 base::DictionaryValue* restored_window = NULL; | 338 base::DictionaryValue* restored_window = NULL; |
348 EXPECT_TRUE(restored_window_session->GetDictionary("window", | 339 EXPECT_TRUE(restored_window_session->GetDictionary("window", |
349 &restored_window)); | 340 &restored_window)); |
350 base::DictionaryValue* window = NULL; | 341 base::DictionaryValue* window = NULL; |
351 int restored_id = api_test_utils::GetInteger(restored_window, "id"); | 342 int restored_id = api_test_utils::GetInteger(restored_window, "id"); |
352 for (size_t i = 0; i < windows->GetSize(); ++i) { | 343 for (size_t i = 0; i < windows->GetSize(); ++i) { |
(...skipping 17 matching lines...) Expand all Loading... |
370 CreateSessionModels(); | 361 CreateSessionModels(); |
371 | 362 |
372 EXPECT_TRUE(base::MatchPattern(utils::RunFunctionAndReturnError( | 363 EXPECT_TRUE(base::MatchPattern(utils::RunFunctionAndReturnError( |
373 CreateFunction<SessionsRestoreFunction>(true).get(), | 364 CreateFunction<SessionsRestoreFunction>(true).get(), |
374 "[\"1\"]", | 365 "[\"1\"]", |
375 CreateIncognitoBrowser()), | 366 CreateIncognitoBrowser()), |
376 "Can not restore sessions in incognito mode.")); | 367 "Can not restore sessions in incognito mode.")); |
377 } | 368 } |
378 | 369 |
379 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetRecentlyClosedIncognito) { | 370 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetRecentlyClosedIncognito) { |
380 scoped_ptr<base::ListValue> result(utils::ToList( | 371 std::unique_ptr<base::ListValue> result( |
381 utils::RunFunctionAndReturnSingleResult( | 372 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
382 CreateFunction<SessionsGetRecentlyClosedFunction>(true).get(), | 373 CreateFunction<SessionsGetRecentlyClosedFunction>(true).get(), "[]", |
383 "[]", | |
384 CreateIncognitoBrowser()))); | 374 CreateIncognitoBrowser()))); |
385 ASSERT_TRUE(result); | 375 ASSERT_TRUE(result); |
386 base::ListValue* sessions = result.get(); | 376 base::ListValue* sessions = result.get(); |
387 EXPECT_EQ(0u, sessions->GetSize()); | 377 EXPECT_EQ(0u, sessions->GetSize()); |
388 } | 378 } |
389 | 379 |
390 // http://crbug.com/251199 | 380 // http://crbug.com/251199 |
391 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_SessionsApis) { | 381 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_SessionsApis) { |
392 #if defined(OS_WIN) && defined(USE_ASH) | 382 #if defined(OS_WIN) && defined(USE_ASH) |
393 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 383 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
394 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 384 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
395 switches::kAshBrowserTests)) | 385 switches::kAshBrowserTests)) |
396 return; | 386 return; |
397 #endif | 387 #endif |
398 | 388 |
399 ASSERT_TRUE(RunExtensionSubtest("sessions", | 389 ASSERT_TRUE(RunExtensionSubtest("sessions", |
400 "sessions.html")) << message_; | 390 "sessions.html")) << message_; |
401 } | 391 } |
402 | 392 |
403 } // namespace | 393 } // namespace |
404 | 394 |
405 } // namespace extensions | 395 } // namespace extensions |
OLD | NEW |