summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-21 16:32:40 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-21 16:32:40 +0000
commit82b8664b1250c39412af70c6278061ab5c346e01 (patch)
treec27101dbebbf0475c5dcb55654c621c49f8508b7 /chrome/browser/extensions
parent167b1403caab4d655888fe9798aaf9b525e7d4ff (diff)
downloadchromium_src-82b8664b1250c39412af70c6278061ab5c346e01.zip
chromium_src-82b8664b1250c39412af70c6278061ab5c346e01.tar.gz
chromium_src-82b8664b1250c39412af70c6278061ab5c346e01.tar.bz2
Committing issue 39299:
Test for extension user script load and inject. This add a test that a user script actually gets injected into pages and runs. Before we only had unit tests for the various classes and weren't testing the system end-to-end. Review URL: http://codereview.chromium.org/51003 Patch from Steve Krulewitz <skrulx@gmail.com>. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12247 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rwxr-xr-xchrome/browser/extensions/extension_user_script_inject_unittest.cc57
-rwxr-xr-xchrome/browser/extensions/extension_view_unittest.cc63
-rwxr-xr-xchrome/browser/extensions/test_extension_loader.cc64
-rwxr-xr-xchrome/browser/extensions/test_extension_loader.h36
4 files changed, 164 insertions, 56 deletions
diff --git a/chrome/browser/extensions/extension_user_script_inject_unittest.cc b/chrome/browser/extensions/extension_user_script_inject_unittest.cc
new file mode 100755
index 0000000..8c1990b
--- /dev/null
+++ b/chrome/browser/extensions/extension_user_script_inject_unittest.cc
@@ -0,0 +1,57 @@
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/browser.h"
+#include "chrome/browser/extensions/extension_error_reporter.h"
+#include "chrome/browser/extensions/test_extension_loader.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/test/in_process_browser_test.h"
+#include "chrome/test/ui_test_utils.h"
+#include "net/base/net_util.h"
+
+namespace {
+
+// The extension we're using as our test case.
+const char* kExtensionId = "00123456789abcdef0123456789abcdef0123456";
+
+} // namespace
+
+class ExtensionUserScriptInjectTest : public InProcessBrowserTest {
+ public:
+ virtual void SetUp() {
+ // Initialize the error reporter here, otherwise BrowserMain will create it
+ // with the wrong MessageLoop.
+ ExtensionErrorReporter::Init(false);
+
+ InProcessBrowserTest::SetUp();
+ }
+};
+
+// Tests that an extension's user script gets injected into content.
+IN_PROC_BROWSER_TEST_F(ExtensionUserScriptInjectTest, Index) {
+ // Get the path to our extension.
+ FilePath extension_path;
+ ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path));
+ extension_path = extension_path.AppendASCII("extensions").
+ AppendASCII("user_script_inject").AppendASCII("1");
+ ASSERT_TRUE(file_util::DirectoryExists(extension_path)); // sanity check
+
+ // Get the file URL to our test page.
+ FilePath test_page_path;
+ ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_page_path));
+ test_page_path = test_page_path.AppendASCII("extensions").
+ AppendASCII("user_script_inject_page.html");
+ ASSERT_TRUE(file_util::PathExists(test_page_path)); // sanity check
+ GURL test_page_url = net::FilePathToFileURL(test_page_path);
+
+ // Load it.
+ TestExtensionLoader loader(browser()->profile());
+ Extension* extension = loader.Load(kExtensionId, extension_path);
+ ASSERT_TRUE(extension);
+
+ // The injected user script will set the page title upon execution.
+ ui_test_utils::NavigateToURL(browser(), test_page_url);
+ TabContents* tabContents = browser()->GetSelectedTabContents();
+ EXPECT_EQ(ASCIIToUTF16("user script executed."), tabContents->GetTitle());
+}
diff --git a/chrome/browser/extensions/extension_view_unittest.cc b/chrome/browser/extensions/extension_view_unittest.cc
index c23cdf7..cc53a46 100755
--- a/chrome/browser/extensions/extension_view_unittest.cc
+++ b/chrome/browser/extensions/extension_view_unittest.cc
@@ -2,15 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/message_loop.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/renderer_host/render_view_host.h"
-#include "chrome/browser/profile.h"
#include "chrome/browser/extensions/extension_error_reporter.h"
#include "chrome/browser/extensions/extension_view.h"
#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/browser/extensions/test_extension_loader.h"
#include "chrome/common/chrome_paths.h"
-#include "chrome/common/notification_service.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
@@ -20,12 +18,11 @@ namespace {
// up.
const int kAlertTimeoutMs = 20000;
-// How long to wait for the extension to load before giving up.
-const int kLoadTimeoutMs = 10000;
-
// The extension we're using as our test case.
const char* kExtensionId = "00123456789abcdef0123456789abcdef0123456";
+} // namespace
+
// This class starts up an extension process and waits until it tries to put
// up a javascript alert.
class MockExtensionView : public ExtensionView {
@@ -59,44 +56,6 @@ class MockExtensionView : public ExtensionView {
bool got_message_;
};
-// This class waits for a specific extension to be loaded.
-class ExtensionLoadedObserver : public NotificationObserver {
- public:
- explicit ExtensionLoadedObserver() : extension_(NULL) {
- registrar_.Add(this, NotificationType::EXTENSIONS_LOADED,
- NotificationService::AllSources());
- }
-
- Extension* WaitForExtension() {
- MessageLoop::current()->PostDelayedTask(FROM_HERE,
- new MessageLoop::QuitTask, kLoadTimeoutMs);
- ui_test_utils::RunMessageLoop();
- return extension_;
- }
-
- private:
- virtual void Observe(NotificationType type, const NotificationSource& source,
- const NotificationDetails& details) {
- if (type == NotificationType::EXTENSIONS_LOADED) {
- ExtensionList* extensions = Details<ExtensionList>(details).ptr();
- for (size_t i = 0; i < (*extensions).size(); i++) {
- if ((*extensions)[i]->id() == kExtensionId) {
- extension_ = (*extensions)[i];
- MessageLoopForUI::current()->Quit();
- break;
- }
- }
- } else {
- NOTREACHED();
- }
- }
-
- NotificationRegistrar registrar_;
- Extension* extension_;
-};
-
-} // namespace
-
class ExtensionViewTest : public InProcessBrowserTest {
public:
virtual void SetUp() {
@@ -114,10 +73,6 @@ class ExtensionViewTest : public InProcessBrowserTest {
// Tests that ExtensionView starts an extension process and runs the script
// contained in the extension's "index.html" file.
IN_PROC_BROWSER_TEST_F(ExtensionViewTest, Index) {
- // Create an observer first to be sure we have the notification registered
- // before it's sent.
- ExtensionLoadedObserver observer;
-
// Get the path to our extension.
FilePath path;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
@@ -125,17 +80,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionViewTest, Index) {
AppendASCII("good").AppendASCII("extension1").AppendASCII("1");
ASSERT_TRUE(file_util::DirectoryExists(path)); // sanity check
- // Load it.
- Profile* profile = browser()->profile();
- profile->GetExtensionsService()->Init();
- profile->GetExtensionsService()->LoadExtension(path);
-
- // Now wait for it to load, and grab a pointer to it.
- Extension* extension = observer.WaitForExtension();
+ // Wait for the extension to load and grab a pointer to it.
+ TestExtensionLoader loader(browser()->profile());
+ Extension* extension = loader.Load(kExtensionId, path);
ASSERT_TRUE(extension);
GURL url = Extension::GetResourceURL(extension->url(), "toolstrip1.html");
// Start the extension process and wait for it to show a javascript alert.
- MockExtensionView view(url, profile);
+ MockExtensionView view(url, browser()->profile());
EXPECT_TRUE(view.got_message());
}
diff --git a/chrome/browser/extensions/test_extension_loader.cc b/chrome/browser/extensions/test_extension_loader.cc
new file mode 100755
index 0000000..4012833
--- /dev/null
+++ b/chrome/browser/extensions/test_extension_loader.cc
@@ -0,0 +1,64 @@
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/test_extension_loader.h"
+
+#include "base/file_path.h"
+#include "base/message_loop.h"
+#include "chrome/browser/profile.h"
+#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/test/ui_test_utils.h"
+
+namespace {
+
+// How long to wait for the extension to load before giving up.
+const int kLoadTimeoutMs = 5000;
+
+} // namespace
+
+TestExtensionLoader::TestExtensionLoader(Profile* profile)
+ : profile_(profile),
+ extension_(NULL),
+ loading_extension_id_(NULL) {
+ registrar_.Add(this, NotificationType::EXTENSIONS_LOADED,
+ NotificationService::AllSources());
+
+ profile_->GetExtensionsService()->Init();
+ DCHECK(profile_->GetExtensionsService()->extensions()->empty());
+}
+
+Extension* TestExtensionLoader::Load(const char* extension_id,
+ const FilePath& path) {
+ loading_extension_id_ = extension_id;
+
+ // Load the extension.
+ profile_->GetExtensionsService()->LoadExtension(path);
+
+ // Wait for the load to complete. Stick a QuitTask into the message loop
+ // with the timeout so it will exit if the extension never loads.
+ extension_ = NULL;
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ new MessageLoop::QuitTask, kLoadTimeoutMs);
+ ui_test_utils::RunMessageLoop();
+
+ return extension_;
+}
+
+void TestExtensionLoader::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (type == NotificationType::EXTENSIONS_LOADED) {
+ ExtensionList* extensions = Details<ExtensionList>(details).ptr();
+ for (size_t i = 0; i < (*extensions).size(); i++) {
+ if ((*extensions)[i]->id() == loading_extension_id_) {
+ extension_ = (*extensions)[i];
+ MessageLoopForUI::current()->Quit();
+ break;
+ }
+ }
+ } else {
+ NOTREACHED();
+ }
+}
diff --git a/chrome/browser/extensions/test_extension_loader.h b/chrome/browser/extensions/test_extension_loader.h
new file mode 100755
index 0000000..36792b0
--- /dev/null
+++ b/chrome/browser/extensions/test_extension_loader.h
@@ -0,0 +1,36 @@
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_LOADER_H_
+#define CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_LOADER_H_
+
+#include "base/basictypes.h"
+#include "chrome/browser/extensions/extension.h"
+#include "chrome/common/notification_observer.h"
+#include "chrome/common/notification_registrar.h"
+
+class Extension;
+class FilePath;
+class Profile;
+
+class TestExtensionLoader : public NotificationObserver {
+ public:
+ explicit TestExtensionLoader(Profile* profile);
+
+ Extension* Load(const char* extension_id, const FilePath& path);
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ private:
+ Profile* profile_;
+ Extension* extension_;
+ NotificationRegistrar registrar_;
+ const char* loading_extension_id_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestExtensionLoader);
+};
+
+#endif // CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_LOADER_H_