| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/scoped_observer.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/extensions/browser_action_test_util.h" | 11 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 11 #include "chrome/browser/extensions/extension_apitest.h" | 12 #include "chrome/browser/extensions/extension_apitest.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_test_message_listener.h" | 14 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 14 #include "chrome/browser/extensions/lazy_background_page_test_util.h" | 15 #include "chrome/browser/extensions/lazy_background_page_test_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" | 17 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" |
| 17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 19 #include "chrome/browser/ui/location_bar/location_bar.h" | 20 #include "chrome/browser/ui/location_bar/location_bar.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 22 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" |
| 23 #include "components/bookmarks/browser/bookmark_model.h" | 24 #include "components/bookmarks/browser/bookmark_model.h" |
| 24 #include "components/bookmarks/browser/bookmark_utils.h" | 25 #include "components/bookmarks/browser/bookmark_utils.h" |
| 25 #include "components/bookmarks/test/bookmark_test_helpers.h" | 26 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 26 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
| 29 #include "extensions/browser/extension_host.h" | 30 #include "extensions/browser/extension_host.h" |
| 31 #include "extensions/browser/extension_registry.h" |
| 32 #include "extensions/browser/extension_registry_observer.h" |
| 30 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
| 31 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
| 32 #include "extensions/common/switches.h" | 35 #include "extensions/common/switches.h" |
| 33 #include "net/dns/mock_host_resolver.h" | 36 #include "net/dns/mock_host_resolver.h" |
| 34 #include "net/test/embedded_test_server/embedded_test_server.h" | 37 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 35 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 36 | 39 |
| 37 using extensions::Extension; | 40 using extensions::Extension; |
| 38 | 41 |
| 39 namespace { | 42 namespace { |
| 40 | 43 |
| 41 // This unfortunate bit of silliness is necessary when loading an extension in | 44 // This unfortunate bit of silliness is necessary when loading an extension in |
| 42 // incognito. The goal is to load the extension, enable incognito, then wait | 45 // incognito. The goal is to load the extension, enable incognito, then wait |
| 43 // for both background pages to load and close. The problem is that enabling | 46 // for both background pages to load and close. The problem is that enabling |
| 44 // incognito involves reloading the extension - and the background pages may | 47 // incognito involves reloading the extension - and the background pages may |
| 45 // have already loaded once before then. So we wait until the extension is | 48 // have already loaded once before then. So we wait until the extension is |
| 46 // unloaded before listening to the background page notifications. | 49 // unloaded before listening to the background page notifications. |
| 47 class LoadedIncognitoObserver : public content::NotificationObserver { | 50 class LoadedIncognitoObserver : public extensions::ExtensionRegistryObserver { |
| 48 public: | 51 public: |
| 49 explicit LoadedIncognitoObserver(Profile* profile) : profile_(profile) { | 52 explicit LoadedIncognitoObserver(Profile* profile) |
| 50 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 53 : profile_(profile), extension_registry_observer_(this) { |
| 51 content::Source<Profile>(profile)); | 54 extension_registry_observer_.Add( |
| 55 extensions::ExtensionRegistry::Get(profile_)); |
| 52 } | 56 } |
| 53 | 57 |
| 54 void Wait() { | 58 void Wait() { |
| 55 ASSERT_TRUE(original_complete_.get()); | 59 ASSERT_TRUE(original_complete_.get()); |
| 56 original_complete_->Wait(); | 60 original_complete_->Wait(); |
| 57 incognito_complete_->Wait(); | 61 incognito_complete_->Wait(); |
| 58 } | 62 } |
| 59 | 63 |
| 60 private: | 64 private: |
| 61 | 65 virtual void OnExtensionUnloaded( |
| 62 virtual void Observe( | 66 content::BrowserContext* browser_context, |
| 63 int type, | 67 const Extension* extension, |
| 64 const content::NotificationSource& source, | 68 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE { |
| 65 const content::NotificationDetails& details) OVERRIDE { | |
| 66 original_complete_.reset(new LazyBackgroundObserver(profile_)); | 69 original_complete_.reset(new LazyBackgroundObserver(profile_)); |
| 67 incognito_complete_.reset( | 70 incognito_complete_.reset( |
| 68 new LazyBackgroundObserver(profile_->GetOffTheRecordProfile())); | 71 new LazyBackgroundObserver(profile_->GetOffTheRecordProfile())); |
| 69 } | 72 } |
| 70 | 73 |
| 71 Profile* profile_; | 74 Profile* profile_; |
| 72 content::NotificationRegistrar registrar_; | 75 ScopedObserver<extensions::ExtensionRegistry, |
| 76 extensions::ExtensionRegistryObserver> |
| 77 extension_registry_observer_; |
| 73 scoped_ptr<LazyBackgroundObserver> original_complete_; | 78 scoped_ptr<LazyBackgroundObserver> original_complete_; |
| 74 scoped_ptr<LazyBackgroundObserver> incognito_complete_; | 79 scoped_ptr<LazyBackgroundObserver> incognito_complete_; |
| 75 }; | 80 }; |
| 76 | 81 |
| 77 } // namespace | 82 } // namespace |
| 78 | 83 |
| 79 class LazyBackgroundPageApiTest : public ExtensionApiTest { | 84 class LazyBackgroundPageApiTest : public ExtensionApiTest { |
| 80 public: | 85 public: |
| 81 LazyBackgroundPageApiTest() {} | 86 LazyBackgroundPageApiTest() {} |
| 82 virtual ~LazyBackgroundPageApiTest() {} | 87 virtual ~LazyBackgroundPageApiTest() {} |
| (...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 | 575 |
| 571 // Tests that the lazy background page will be unloaded if the onSuspend event | 576 // Tests that the lazy background page will be unloaded if the onSuspend event |
| 572 // handler calls an API function such as chrome.storage.local.set(). | 577 // handler calls an API function such as chrome.storage.local.set(). |
| 573 // See: http://crbug.com/296834 | 578 // See: http://crbug.com/296834 |
| 574 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnSuspendUseStorageApi) { | 579 IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, OnSuspendUseStorageApi) { |
| 575 EXPECT_TRUE(LoadExtensionAndWait("on_suspend")); | 580 EXPECT_TRUE(LoadExtensionAndWait("on_suspend")); |
| 576 } | 581 } |
| 577 | 582 |
| 578 // TODO: background page with timer. | 583 // TODO: background page with timer. |
| 579 // TODO: background page that interacts with popup. | 584 // TODO: background page that interacts with popup. |
| OLD | NEW |