| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_browsertest.h" | 5 #include "chrome/browser/extensions/extension_browsertest.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "chrome/browser/extensions/crx_installer.h" | 15 #include "chrome/browser/extensions/crx_installer.h" |
| 16 #include "chrome/browser/extensions/extension_creator.h" | 16 #include "chrome/browser/extensions/extension_creator.h" |
| 17 #include "chrome/browser/extensions/extension_error_reporter.h" | 17 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 18 #include "chrome/browser/extensions/extension_host.h" | 18 #include "chrome/browser/extensions/extension_host.h" |
| 19 #include "chrome/browser/extensions/extension_install_ui.h" | 19 #include "chrome/browser/extensions/extension_install_ui.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
| 24 #include "chrome/browser/ui/omnibox/location_bar.h" | 24 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 25 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
| 26 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/test/base/ui_test_utils.h" | 28 #include "chrome/test/base/ui_test_utils.h" |
| 29 #include "content/public/browser/notification_registrar.h" | 29 #include "content/public/browser/notification_registrar.h" |
| 30 #include "content/common/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 31 | 31 |
| 32 ExtensionBrowserTest::ExtensionBrowserTest() | 32 ExtensionBrowserTest::ExtensionBrowserTest() |
| 33 : loaded_(false), | 33 : loaded_(false), |
| 34 installed_(false), | 34 installed_(false), |
| 35 extension_installs_observed_(0), | 35 extension_installs_observed_(0), |
| 36 target_page_action_count_(-1), | 36 target_page_action_count_(-1), |
| 37 target_visible_page_action_count_(-1) { | 37 target_visible_page_action_count_(-1) { |
| 38 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 38 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 58 command_line->AppendSwitch(switches::kNoFirstRun); | 58 command_line->AppendSwitch(switches::kNoFirstRun); |
| 59 #endif | 59 #endif |
| 60 } | 60 } |
| 61 | 61 |
| 62 const Extension* ExtensionBrowserTest::LoadExtensionWithOptions( | 62 const Extension* ExtensionBrowserTest::LoadExtensionWithOptions( |
| 63 const FilePath& path, bool incognito_enabled, bool fileaccess_enabled) { | 63 const FilePath& path, bool incognito_enabled, bool fileaccess_enabled) { |
| 64 ExtensionService* service = browser()->profile()->GetExtensionService(); | 64 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 65 { | 65 { |
| 66 content::NotificationRegistrar registrar; | 66 content::NotificationRegistrar registrar; |
| 67 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 67 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 68 NotificationService::AllSources()); | 68 content::NotificationService::AllSources()); |
| 69 service->LoadExtension(path, false); | 69 service->LoadExtension(path, false); |
| 70 ui_test_utils::RunMessageLoop(); | 70 ui_test_utils::RunMessageLoop(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Find the extension by iterating backwards since it is likely last. | 73 // Find the extension by iterating backwards since it is likely last. |
| 74 FilePath extension_path = path; | 74 FilePath extension_path = path; |
| 75 file_util::AbsolutePath(&extension_path); | 75 file_util::AbsolutePath(&extension_path); |
| 76 const Extension* extension = NULL; | 76 const Extension* extension = NULL; |
| 77 for (ExtensionList::const_reverse_iterator iter = | 77 for (ExtensionList::const_reverse_iterator iter = |
| 78 service->extensions()->rbegin(); | 78 service->extensions()->rbegin(); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 int expected_change, | 235 int expected_change, |
| 236 Profile* profile, | 236 Profile* profile, |
| 237 bool from_webstore) { | 237 bool from_webstore) { |
| 238 ExtensionService* service = profile->GetExtensionService(); | 238 ExtensionService* service = profile->GetExtensionService(); |
| 239 service->set_show_extensions_prompts(false); | 239 service->set_show_extensions_prompts(false); |
| 240 size_t num_before = service->extensions()->size(); | 240 size_t num_before = service->extensions()->size(); |
| 241 | 241 |
| 242 { | 242 { |
| 243 content::NotificationRegistrar registrar; | 243 content::NotificationRegistrar registrar; |
| 244 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 244 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 245 NotificationService::AllSources()); | 245 content::NotificationService::AllSources()); |
| 246 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED, | 246 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED, |
| 247 NotificationService::AllSources()); | 247 content::NotificationService::AllSources()); |
| 248 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 248 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 249 NotificationService::AllSources()); | 249 content::NotificationService::AllSources()); |
| 250 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOAD_ERROR, | 250 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOAD_ERROR, |
| 251 NotificationService::AllSources()); | 251 content::NotificationService::AllSources()); |
| 252 | 252 |
| 253 ExtensionInstallUI* install_ui = NULL; | 253 ExtensionInstallUI* install_ui = NULL; |
| 254 if (ui_type == INSTALL_UI_TYPE_CANCEL) | 254 if (ui_type == INSTALL_UI_TYPE_CANCEL) |
| 255 install_ui = new MockAbortExtensionInstallUI(); | 255 install_ui = new MockAbortExtensionInstallUI(); |
| 256 else if (ui_type == INSTALL_UI_TYPE_NORMAL) | 256 else if (ui_type == INSTALL_UI_TYPE_NORMAL) |
| 257 install_ui = new ExtensionInstallUI(profile); | 257 install_ui = new ExtensionInstallUI(profile); |
| 258 else if (ui_type == INSTALL_UI_TYPE_AUTO_CONFIRM) | 258 else if (ui_type == INSTALL_UI_TYPE_AUTO_CONFIRM) |
| 259 install_ui = new MockAutoConfirmExtensionInstallUI(profile); | 259 install_ui = new MockAutoConfirmExtensionInstallUI(profile); |
| 260 | 260 |
| 261 // TODO(tessamac): Update callers to always pass an unpacked extension | 261 // TODO(tessamac): Update callers to always pass an unpacked extension |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 } | 296 } |
| 297 | 297 |
| 298 return WaitForExtensionHostsToLoad(); | 298 return WaitForExtensionHostsToLoad(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void ExtensionBrowserTest::ReloadExtension(const std::string& extension_id) { | 301 void ExtensionBrowserTest::ReloadExtension(const std::string& extension_id) { |
| 302 ExtensionService* service = browser()->profile()->GetExtensionService(); | 302 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 303 service->ReloadExtension(extension_id); | 303 service->ReloadExtension(extension_id); |
| 304 ui_test_utils::RegisterAndWait(this, | 304 ui_test_utils::RegisterAndWait(this, |
| 305 chrome::NOTIFICATION_EXTENSION_LOADED, | 305 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 306 NotificationService::AllSources()); | 306 content::NotificationService::AllSources()); |
| 307 } | 307 } |
| 308 | 308 |
| 309 void ExtensionBrowserTest::UnloadExtension(const std::string& extension_id) { | 309 void ExtensionBrowserTest::UnloadExtension(const std::string& extension_id) { |
| 310 ExtensionService* service = browser()->profile()->GetExtensionService(); | 310 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 311 service->UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_DISABLE); | 311 service->UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_DISABLE); |
| 312 } | 312 } |
| 313 | 313 |
| 314 void ExtensionBrowserTest::UninstallExtension(const std::string& extension_id) { | 314 void ExtensionBrowserTest::UninstallExtension(const std::string& extension_id) { |
| 315 ExtensionService* service = browser()->profile()->GetExtensionService(); | 315 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 316 service->UninstallExtension(extension_id, false, NULL); | 316 service->UninstallExtension(extension_id, false, NULL); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void ExtensionBrowserTest::DisableExtension(const std::string& extension_id) { | 319 void ExtensionBrowserTest::DisableExtension(const std::string& extension_id) { |
| 320 ExtensionService* service = browser()->profile()->GetExtensionService(); | 320 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 321 service->DisableExtension(extension_id); | 321 service->DisableExtension(extension_id); |
| 322 } | 322 } |
| 323 | 323 |
| 324 void ExtensionBrowserTest::EnableExtension(const std::string& extension_id) { | 324 void ExtensionBrowserTest::EnableExtension(const std::string& extension_id) { |
| 325 ExtensionService* service = browser()->profile()->GetExtensionService(); | 325 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 326 service->EnableExtension(extension_id); | 326 service->EnableExtension(extension_id); |
| 327 } | 327 } |
| 328 | 328 |
| 329 bool ExtensionBrowserTest::WaitForPageActionCountChangeTo(int count) { | 329 bool ExtensionBrowserTest::WaitForPageActionCountChangeTo(int count) { |
| 330 LocationBarTesting* location_bar = | 330 LocationBarTesting* location_bar = |
| 331 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); | 331 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 332 if (location_bar->PageActionCount() != count) { | 332 if (location_bar->PageActionCount() != count) { |
| 333 target_page_action_count_ = count; | 333 target_page_action_count_ = count; |
| 334 ui_test_utils::RegisterAndWait(this, | 334 ui_test_utils::RegisterAndWait(this, |
| 335 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, | 335 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, |
| 336 NotificationService::AllSources()); | 336 content::NotificationService::AllSources()); |
| 337 } | 337 } |
| 338 return location_bar->PageActionCount() == count; | 338 return location_bar->PageActionCount() == count; |
| 339 } | 339 } |
| 340 | 340 |
| 341 bool ExtensionBrowserTest::WaitForPageActionVisibilityChangeTo(int count) { | 341 bool ExtensionBrowserTest::WaitForPageActionVisibilityChangeTo(int count) { |
| 342 LocationBarTesting* location_bar = | 342 LocationBarTesting* location_bar = |
| 343 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); | 343 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 344 if (location_bar->PageActionVisibleCount() != count) { | 344 if (location_bar->PageActionVisibleCount() != count) { |
| 345 target_visible_page_action_count_ = count; | 345 target_visible_page_action_count_ = count; |
| 346 ui_test_utils::RegisterAndWait(this, | 346 ui_test_utils::RegisterAndWait(this, |
| 347 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, | 347 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 348 NotificationService::AllSources()); | 348 content::NotificationService::AllSources()); |
| 349 } | 349 } |
| 350 return location_bar->PageActionVisibleCount() == count; | 350 return location_bar->PageActionVisibleCount() == count; |
| 351 } | 351 } |
| 352 | 352 |
| 353 bool ExtensionBrowserTest::WaitForExtensionHostsToLoad() { | 353 bool ExtensionBrowserTest::WaitForExtensionHostsToLoad() { |
| 354 // Wait for all the extension hosts that exist to finish loading. | 354 // Wait for all the extension hosts that exist to finish loading. |
| 355 content::NotificationRegistrar registrar; | 355 content::NotificationRegistrar registrar; |
| 356 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 356 registrar.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 357 NotificationService::AllSources()); | 357 content::NotificationService::AllSources()); |
| 358 | 358 |
| 359 ExtensionProcessManager* manager = | 359 ExtensionProcessManager* manager = |
| 360 browser()->profile()->GetExtensionProcessManager(); | 360 browser()->profile()->GetExtensionProcessManager(); |
| 361 for (ExtensionProcessManager::const_iterator iter = manager->begin(); | 361 for (ExtensionProcessManager::const_iterator iter = manager->begin(); |
| 362 iter != manager->end();) { | 362 iter != manager->end();) { |
| 363 if ((*iter)->did_stop_loading()) { | 363 if ((*iter)->did_stop_loading()) { |
| 364 ++iter; | 364 ++iter; |
| 365 } else { | 365 } else { |
| 366 ui_test_utils::RunMessageLoop(); | 366 ui_test_utils::RunMessageLoop(); |
| 367 | 367 |
| 368 // Test activity may have modified the set of extension processes during | 368 // Test activity may have modified the set of extension processes during |
| 369 // message processing, so re-start the iteration to catch added/removed | 369 // message processing, so re-start the iteration to catch added/removed |
| 370 // processes. | 370 // processes. |
| 371 iter = manager->begin(); | 371 iter = manager->begin(); |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 return true; | 374 return true; |
| 375 } | 375 } |
| 376 | 376 |
| 377 bool ExtensionBrowserTest::WaitForExtensionInstall() { | 377 bool ExtensionBrowserTest::WaitForExtensionInstall() { |
| 378 int before = extension_installs_observed_; | 378 int before = extension_installs_observed_; |
| 379 ui_test_utils::RegisterAndWait(this, | 379 ui_test_utils::RegisterAndWait(this, |
| 380 chrome::NOTIFICATION_EXTENSION_INSTALLED, | 380 chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 381 NotificationService::AllSources()); | 381 content::NotificationService::AllSources()); |
| 382 return extension_installs_observed_ == (before + 1); | 382 return extension_installs_observed_ == (before + 1); |
| 383 } | 383 } |
| 384 | 384 |
| 385 bool ExtensionBrowserTest::WaitForExtensionInstallError() { | 385 bool ExtensionBrowserTest::WaitForExtensionInstallError() { |
| 386 int before = extension_installs_observed_; | 386 int before = extension_installs_observed_; |
| 387 ui_test_utils::RegisterAndWait(this, | 387 ui_test_utils::RegisterAndWait(this, |
| 388 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 388 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 389 NotificationService::AllSources()); | 389 content::NotificationService::AllSources()); |
| 390 return extension_installs_observed_ == before; | 390 return extension_installs_observed_ == before; |
| 391 } | 391 } |
| 392 | 392 |
| 393 void ExtensionBrowserTest::WaitForExtensionLoad() { | 393 void ExtensionBrowserTest::WaitForExtensionLoad() { |
| 394 ui_test_utils::RegisterAndWait(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 394 ui_test_utils::RegisterAndWait(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 395 NotificationService::AllSources()); | 395 content::NotificationService::AllSources()); |
| 396 WaitForExtensionHostsToLoad(); | 396 WaitForExtensionHostsToLoad(); |
| 397 } | 397 } |
| 398 | 398 |
| 399 bool ExtensionBrowserTest::WaitForExtensionCrash( | 399 bool ExtensionBrowserTest::WaitForExtensionCrash( |
| 400 const std::string& extension_id) { | 400 const std::string& extension_id) { |
| 401 ExtensionService* service = browser()->profile()->GetExtensionService(); | 401 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 402 | 402 |
| 403 if (!service->GetExtensionById(extension_id, true)) { | 403 if (!service->GetExtensionById(extension_id, true)) { |
| 404 // The extension is already unloaded, presumably due to a crash. | 404 // The extension is already unloaded, presumably due to a crash. |
| 405 return true; | 405 return true; |
| 406 } | 406 } |
| 407 ui_test_utils::RegisterAndWait( | 407 ui_test_utils::RegisterAndWait( |
| 408 this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 408 this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
| 409 NotificationService::AllSources()); | 409 content::NotificationService::AllSources()); |
| 410 return (service->GetExtensionById(extension_id, true) == NULL); | 410 return (service->GetExtensionById(extension_id, true) == NULL); |
| 411 } | 411 } |
| 412 | 412 |
| 413 void ExtensionBrowserTest::Observe( | 413 void ExtensionBrowserTest::Observe( |
| 414 int type, | 414 int type, |
| 415 const content::NotificationSource& source, | 415 const content::NotificationSource& source, |
| 416 const content::NotificationDetails& details) { | 416 const content::NotificationDetails& details) { |
| 417 switch (type) { | 417 switch (type) { |
| 418 case chrome::NOTIFICATION_EXTENSION_LOADED: | 418 case chrome::NOTIFICATION_EXTENSION_LOADED: |
| 419 last_loaded_extension_id_ = | 419 last_loaded_extension_id_ = |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 MessageLoopForUI::current()->Quit(); | 483 MessageLoopForUI::current()->Quit(); |
| 484 } | 484 } |
| 485 break; | 485 break; |
| 486 } | 486 } |
| 487 | 487 |
| 488 default: | 488 default: |
| 489 NOTREACHED(); | 489 NOTREACHED(); |
| 490 break; | 490 break; |
| 491 } | 491 } |
| 492 } | 492 } |
| OLD | NEW |