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