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 class CancelableQuitTask : public Task { |
| 18 public: |
| 19 explicit CancelableQuitTask( const std::string& timeout_message) |
| 20 : timeout_message_(timeout_message), |
| 21 cancelled_(false) { |
| 22 } |
| 23 |
| 24 void cancel() { |
| 25 cancelled_ = true; |
| 26 } |
| 27 |
| 28 virtual void Run() { |
| 29 if (cancelled_) { |
| 30 return; |
| 31 } |
| 32 FAIL() << timeout_message_; |
| 33 MessageLoop::current()->Quit(); |
| 34 } |
| 35 |
| 36 private: |
| 37 std::string timeout_message_; |
| 38 bool cancelled_; |
| 39 }; |
| 40 |
| 41 const char kPageWithContentScript[] = |
| 42 "files/devtools/page_with_content_script.html"; |
| 43 |
| 44 // Base class for DevTools tests that test devtools functionality for |
| 45 // extensions and content scripts. |
| 46 class DevToolsExtensionDebugTest : public DevToolsSanityTest, |
| 47 public NotificationObserver { |
| 48 public: |
| 49 DevToolsExtensionDebugTest() : DevToolsSanityTest() { |
| 50 PathService::Get(chrome::DIR_TEST_DATA, &test_extensions_dir_); |
| 51 test_extensions_dir_ = test_extensions_dir_.AppendASCII("devtools"); |
| 52 test_extensions_dir_ = test_extensions_dir_.AppendASCII("extensions"); |
| 53 } |
| 54 |
| 55 protected: |
| 56 // Load an extention from test\data\devtools\extensions\<extension_name> |
| 57 void LoadExtension(const char* extension_name) { |
| 58 FilePath path = test_extensions_dir_.AppendASCII(extension_name); |
| 59 ASSERT_TRUE(LoadExtensionFromPath(path)) << "Failed to load extension."; |
| 60 } |
| 61 |
| 62 private: |
| 63 bool LoadExtensionFromPath(const FilePath& path) { |
| 64 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 65 size_t num_before = service->extensions()->size(); |
| 66 { |
| 67 NotificationRegistrar registrar; |
| 68 registrar.Add(this, NotificationType::EXTENSION_LOADED, |
| 69 NotificationService::AllSources()); |
| 70 CancelableQuitTask* delayed_quit = |
| 71 new CancelableQuitTask("Extension load timed out."); |
| 72 MessageLoop::current()->PostDelayedTask( |
| 73 FROM_HERE, delayed_quit, 2 * TestTimeouts::action_timeout_ms()); |
| 74 service->LoadExtension(path); |
| 75 ui_test_utils::RunMessageLoop(); |
| 76 delayed_quit->cancel(); |
| 77 } |
| 78 size_t num_after = service->extensions()->size(); |
| 79 if (num_after != (num_before + 1)) |
| 80 return false; |
| 81 |
| 82 return WaitForExtensionHostsToLoad(); |
| 83 } |
| 84 |
| 85 bool WaitForExtensionHostsToLoad() { |
| 86 // Wait for all the extension hosts that exist to finish loading. |
| 87 // NOTE: This assumes that the extension host list is not changing while |
| 88 // this method is running. |
| 89 |
| 90 NotificationRegistrar registrar; |
| 91 registrar.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 92 NotificationService::AllSources()); |
| 93 CancelableQuitTask* delayed_quit = |
| 94 new CancelableQuitTask("Extension host load timed out."); |
| 95 MessageLoop::current()->PostDelayedTask( |
| 96 FROM_HERE, delayed_quit, 2 * TestTimeouts::action_timeout_ms()); |
| 97 |
| 98 ExtensionProcessManager* manager = |
| 99 browser()->profile()->GetExtensionProcessManager(); |
| 100 for (ExtensionProcessManager::const_iterator iter = manager->begin(); |
| 101 iter != manager->end();) { |
| 102 if ((*iter)->did_stop_loading()) |
| 103 ++iter; |
| 104 else |
| 105 ui_test_utils::RunMessageLoop(); |
| 106 } |
| 107 |
| 108 delayed_quit->cancel(); |
| 109 return true; |
| 110 } |
| 111 |
| 112 void Observe(NotificationType type, |
| 113 const NotificationSource& source, |
| 114 const NotificationDetails& details) { |
| 115 switch (type.value) { |
| 116 case NotificationType::EXTENSION_LOADED: |
| 117 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: |
| 118 MessageLoopForUI::current()->Quit(); |
| 119 break; |
| 120 default: |
| 121 NOTREACHED(); |
| 122 break; |
| 123 } |
| 124 } |
| 125 |
| 126 FilePath test_extensions_dir_; |
| 127 }; |
| 128 |
| 129 // Tests that a content script is in the scripts list. |
| 130 // This test is disabled, see bug 28961. |
| 131 IN_PROC_BROWSER_TEST_F(DevToolsExtensionDebugTest, |
| 132 TestContentScriptIsPresent) { |
| 133 LoadExtension("simple_content_script"); |
| 134 RunTest("testContentScriptIsPresent", kPageWithContentScript); |
| 135 } |
| 136 |
| 137 } // namespace |
OLD | NEW |