OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/path_service.h" |
| 6 #include "base/test/test_timeouts.h" |
| 7 #include "chrome/browser/extensions/extension_host.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/test/ui_test_utils.h" |
| 12 #include "content/browser/debugger/devtools_sanity_unittest.h" |
| 13 #include "content/common/notification_service.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kPageWithContentScript[] = |
| 18 "files/devtools/page_with_content_script.html"; |
| 19 |
| 20 // Base class for DevTools tests that test devtools functionality for |
| 21 // extensions and content scripts. |
| 22 class DevToolsExtensionDebugTest : public DevToolsSanityTest, |
| 23 public NotificationObserver { |
| 24 public: |
| 25 DevToolsExtensionDebugTest() : DevToolsSanityTest() { |
| 26 PathService::Get(chrome::DIR_TEST_DATA, &test_extensions_dir_); |
| 27 test_extensions_dir_ = test_extensions_dir_.AppendASCII("devtools"); |
| 28 test_extensions_dir_ = test_extensions_dir_.AppendASCII("extensions"); |
| 29 } |
| 30 |
| 31 protected: |
| 32 // Load an extention from test\data\devtools\extensions\<extension_name> |
| 33 void LoadExtension(const char* extension_name) { |
| 34 FilePath path = test_extensions_dir_.AppendASCII(extension_name); |
| 35 ASSERT_TRUE(LoadExtensionFromPath(path)) << "Failed to load extension."; |
| 36 } |
| 37 |
| 38 private: |
| 39 bool LoadExtensionFromPath(const FilePath& path) { |
| 40 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 41 size_t num_before = service->extensions()->size(); |
| 42 { |
| 43 NotificationRegistrar registrar; |
| 44 registrar.Add(this, NotificationType::EXTENSION_LOADED, |
| 45 NotificationService::AllSources()); |
| 46 CancelableQuitTask* delayed_quit = |
| 47 new CancelableQuitTask("Extension load timed out."); |
| 48 MessageLoop::current()->PostDelayedTask( |
| 49 FROM_HERE, delayed_quit, 2 * TestTimeouts::action_timeout()); |
| 50 service->LoadExtension(path); |
| 51 ui_test_utils::RunMessageLoop(); |
| 52 delayed_quit->cancel(); |
| 53 } |
| 54 size_t num_after = service->extensions()->size(); |
| 55 if (num_after != (num_before + 1)) |
| 56 return false; |
| 57 |
| 58 return WaitForExtensionHostsToLoad(); |
| 59 } |
| 60 |
| 61 bool WaitForExtensionHostsToLoad() { |
| 62 // Wait for all the extension hosts that exist to finish loading. |
| 63 // NOTE: This assumes that the extension host list is not changing while |
| 64 // this method is running. |
| 65 |
| 66 NotificationRegistrar registrar; |
| 67 registrar.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 68 NotificationService::AllSources()); |
| 69 CancelableQuitTask* delayed_quit = |
| 70 new CancelableQuitTask("Extension host load timed out."); |
| 71 MessageLoop::current()->PostDelayedTask( |
| 72 FROM_HERE, delayed_quit, 2 * TestTimeouts::action_timeout()); |
| 73 |
| 74 ExtensionProcessManager* manager = |
| 75 browser()->profile()->GetExtensionProcessManager(); |
| 76 for (ExtensionProcessManager::const_iterator iter = manager->begin(); |
| 77 iter != manager->end();) { |
| 78 if ((*iter)->did_stop_loading()) |
| 79 ++iter; |
| 80 else |
| 81 ui_test_utils::RunMessageLoop(); |
| 82 } |
| 83 |
| 84 delayed_quit->cancel(); |
| 85 return true; |
| 86 } |
| 87 |
| 88 void Observe(NotificationType type, |
| 89 const NotificationSource& source, |
| 90 const NotificationDetails& details) { |
| 91 switch (type.value) { |
| 92 case NotificationType::EXTENSION_LOADED: |
| 93 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: |
| 94 MessageLoopForUI::current()->Quit(); |
| 95 break; |
| 96 default: |
| 97 NOTREACHED(); |
| 98 break; |
| 99 } |
| 100 } |
| 101 |
| 102 FilePath test_extensions_dir_; |
| 103 }; |
| 104 |
| 105 // Tests that a content script is in the scripts list. |
| 106 // This test is disabled, see bug 28961. |
| 107 IN_PROC_BROWSER_TEST_F(DevToolsExtensionDebugTest, |
| 108 TestContentScriptIsPresent) { |
| 109 LoadExtension("simple_content_script"); |
| 110 RunTest("testContentScriptIsPresent", kPageWithContentScript); |
| 111 } |
| 112 |
| 113 } // namespace |
OLD | NEW |