| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/message_loop.h" |
| 5 #include "base/ref_counted.h" | 6 #include "base/ref_counted.h" |
| 6 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 7 #include "chrome/browser/renderer_host/render_view_host.h" | 8 #include "chrome/browser/renderer_host/render_view_host.h" |
| 9 #include "chrome/browser/profile.h" |
| 8 #include "chrome/browser/extensions/extension_error_reporter.h" | 10 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 9 #include "chrome/browser/extensions/extension_view.h" | 11 #include "chrome/browser/extensions/extension_view.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" | 12 #include "chrome/browser/extensions/extensions_service.h" |
| 11 #include "chrome/browser/extensions/test_extension_loader.h" | |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/notification_service.h" |
| 13 #include "chrome/test/in_process_browser_test.h" | 15 #include "chrome/test/in_process_browser_test.h" |
| 14 #include "chrome/test/ui_test_utils.h" | 16 #include "chrome/test/ui_test_utils.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // How long to wait for the extension to put up a javascript alert before giving | 20 // How long to wait for the extension to put up a javascript alert before giving |
| 19 // up. | 21 // up. |
| 20 const int kAlertTimeoutMs = 20000; | 22 const int kAlertTimeoutMs = 20000; |
| 21 | 23 |
| 24 // How long to wait for the extension to load before giving up. |
| 25 const int kLoadTimeoutMs = 10000; |
| 26 |
| 22 // The extension we're using as our test case. | 27 // The extension we're using as our test case. |
| 23 const char* kExtensionId = "00123456789abcdef0123456789abcdef0123456"; | 28 const char* kExtensionId = "00123456789abcdef0123456789abcdef0123456"; |
| 24 | 29 |
| 25 }; // namespace | |
| 26 | |
| 27 // This class starts up an extension process and waits until it tries to put | 30 // This class starts up an extension process and waits until it tries to put |
| 28 // up a javascript alert. | 31 // up a javascript alert. |
| 29 class MockExtensionView : public ExtensionView { | 32 class MockExtensionView : public ExtensionView { |
| 30 public: | 33 public: |
| 31 MockExtensionView(Extension* extension, const GURL& url, Profile* profile) | 34 MockExtensionView(Extension* extension, const GURL& url, Profile* profile) |
| 32 : ExtensionView(extension, url, profile), got_message_(false) { | 35 : ExtensionView(extension, url, profile), got_message_(false) { |
| 33 InitHidden(); | 36 InitHidden(); |
| 34 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 37 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 35 new MessageLoop::QuitTask, kAlertTimeoutMs); | 38 new MessageLoop::QuitTask, kAlertTimeoutMs); |
| 36 ui_test_utils::RunMessageLoop(); | 39 ui_test_utils::RunMessageLoop(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 50 | 53 |
| 51 // Call super, otherwise we'll leak reply_msg. | 54 // Call super, otherwise we'll leak reply_msg. |
| 52 ExtensionView::RunJavaScriptMessage( | 55 ExtensionView::RunJavaScriptMessage( |
| 53 message, default_prompt, frame_url, flags, | 56 message, default_prompt, frame_url, flags, |
| 54 reply_msg, did_suppress_message); | 57 reply_msg, did_suppress_message); |
| 55 } | 58 } |
| 56 | 59 |
| 57 bool got_message_; | 60 bool got_message_; |
| 58 }; | 61 }; |
| 59 | 62 |
| 63 // This class waits for a specific extension to be loaded. |
| 64 class ExtensionLoadedObserver : public NotificationObserver { |
| 65 public: |
| 66 explicit ExtensionLoadedObserver() : extension_(NULL) { |
| 67 registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, |
| 68 NotificationService::AllSources()); |
| 69 } |
| 70 |
| 71 Extension* WaitForExtension() { |
| 72 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 73 new MessageLoop::QuitTask, kLoadTimeoutMs); |
| 74 ui_test_utils::RunMessageLoop(); |
| 75 return extension_; |
| 76 } |
| 77 |
| 78 private: |
| 79 virtual void Observe(NotificationType type, const NotificationSource& source, |
| 80 const NotificationDetails& details) { |
| 81 if (type == NotificationType::EXTENSIONS_LOADED) { |
| 82 ExtensionList* extensions = Details<ExtensionList>(details).ptr(); |
| 83 for (size_t i = 0; i < (*extensions).size(); i++) { |
| 84 if ((*extensions)[i]->id() == kExtensionId) { |
| 85 extension_ = (*extensions)[i]; |
| 86 MessageLoopForUI::current()->Quit(); |
| 87 break; |
| 88 } |
| 89 } |
| 90 } else { |
| 91 NOTREACHED(); |
| 92 } |
| 93 } |
| 94 |
| 95 NotificationRegistrar registrar_; |
| 96 Extension* extension_; |
| 97 }; |
| 98 |
| 99 } // namespace |
| 100 |
| 60 class ExtensionViewTest : public InProcessBrowserTest { | 101 class ExtensionViewTest : public InProcessBrowserTest { |
| 61 public: | 102 public: |
| 62 virtual void SetUp() { | 103 virtual void SetUp() { |
| 63 // Initialize the error reporter here, otherwise BrowserMain will create it | 104 // Initialize the error reporter here, otherwise BrowserMain will create it |
| 64 // with the wrong MessageLoop. | 105 // with the wrong MessageLoop. |
| 65 ExtensionErrorReporter::Init(false); | 106 ExtensionErrorReporter::Init(false); |
| 66 | 107 |
| 67 // Use single-process in an attempt to speed it up and make it less flaky. | 108 // Use single-process in an attempt to speed it up and make it less flaky. |
| 68 //EnableSingleProcess(); | 109 //EnableSingleProcess(); |
| 69 | 110 |
| 70 InProcessBrowserTest::SetUp(); | 111 InProcessBrowserTest::SetUp(); |
| 71 } | 112 } |
| 72 }; | 113 }; |
| 73 | 114 |
| 74 // Tests that ExtensionView starts an extension process and runs the script | 115 // Tests that ExtensionView starts an extension process and runs the script |
| 75 // contained in the extension's "index.html" file. | 116 // contained in the extension's "index.html" file. |
| 76 IN_PROC_BROWSER_TEST_F(ExtensionViewTest, Index) { | 117 IN_PROC_BROWSER_TEST_F(ExtensionViewTest, Index) { |
| 118 // Create an observer first to be sure we have the notification registered |
| 119 // before it's sent. |
| 120 ExtensionLoadedObserver observer; |
| 121 |
| 77 // Get the path to our extension. | 122 // Get the path to our extension. |
| 78 FilePath path; | 123 FilePath path; |
| 79 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 124 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 80 path = path.AppendASCII("extensions"). | 125 path = path.AppendASCII("extensions"). |
| 81 AppendASCII("good").AppendASCII("extension1").AppendASCII("1"); | 126 AppendASCII("good").AppendASCII("extension1").AppendASCII("1"); |
| 82 ASSERT_TRUE(file_util::DirectoryExists(path)); // sanity check | 127 ASSERT_TRUE(file_util::DirectoryExists(path)); // sanity check |
| 83 | 128 |
| 84 // Wait for the extension to load and grab a pointer to it. | 129 // Load it. |
| 85 TestExtensionLoader loader(browser()->profile()); | 130 Profile* profile = browser()->profile(); |
| 86 Extension* extension = loader.Load(kExtensionId, path); | 131 profile->GetExtensionsService()->Init(); |
| 132 profile->GetExtensionsService()->LoadExtension(path); |
| 133 |
| 134 // Now wait for it to load, and grab a pointer to it. |
| 135 Extension* extension = observer.WaitForExtension(); |
| 87 ASSERT_TRUE(extension); | 136 ASSERT_TRUE(extension); |
| 88 GURL url = Extension::GetResourceURL(extension->url(), "toolstrip1.html"); | 137 GURL url = Extension::GetResourceURL(extension->url(), "toolstrip1.html"); |
| 89 | 138 |
| 90 // Start the extension process and wait for it to show a javascript alert. | 139 // Start the extension process and wait for it to show a javascript alert. |
| 91 MockExtensionView view(extension, url, browser()->profile()); | 140 MockExtensionView view(extension, url, profile); |
| 92 EXPECT_TRUE(view.got_message()); | 141 EXPECT_TRUE(view.got_message()); |
| 93 } | 142 } |
| OLD | NEW |