diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-01 05:25:22 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-01 05:25:22 +0000 |
commit | f0488f2fa6fb1dea3b18f806dbd46f6a828063de (patch) | |
tree | 274a930929cbd862a14bfdc3bf92a9c59e468c78 /chrome/browser | |
parent | befd8d82ec47c02d6b17d9fa7b6c1f3bb703150f (diff) | |
download | chromium_src-f0488f2fa6fb1dea3b18f806dbd46f6a828063de.zip chromium_src-f0488f2fa6fb1dea3b18f806dbd46f6a828063de.tar.gz chromium_src-f0488f2fa6fb1dea3b18f806dbd46f6a828063de.tar.bz2 |
Adds a browser_test for extension startup, content script
injection, and user script injection.
Also adds unit tests that EXTENSIONS_READY always gets fired.
Also removes some old test data that we weren't using.
Review URL: http://codereview.chromium.org/149120
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19709 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/extensions/extension_startup_unittest.cc | 204 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service_unittest.cc | 54 |
2 files changed, 255 insertions, 3 deletions
diff --git a/chrome/browser/extensions/extension_startup_unittest.cc b/chrome/browser/extensions/extension_startup_unittest.cc new file mode 100644 index 0000000..ce9b885 --- /dev/null +++ b/chrome/browser/extensions/extension_startup_unittest.cc @@ -0,0 +1,204 @@ +#include <vector> + +#include "base/command_line.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/extensions/extensions_service.h" +#include "chrome/browser/profile.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" +#include "net/base/net_util.h" + +// This file contains high-level regression tests for the extensions system. The +// goal here is not to test everything in depth, but to run the system as close +// as possible end-to-end to find any gaps in test coverage in the lower-level +// unit tests. + +class ExtensionStartupTestBase + : public InProcessBrowserTest, public NotificationObserver { + public: + ExtensionStartupTestBase() + : enable_extensions_(false), enable_user_scripts_(false) { + EnableDOMAutomation(); + } + + protected: + // InProcessBrowserTest + virtual void SetUpCommandLine(CommandLine* command_line) { + FilePath profile_dir; + PathService::Get(chrome::DIR_USER_DATA, &profile_dir); + profile_dir = profile_dir.AppendASCII("Default"); + file_util::CreateDirectory(profile_dir); + + preferences_file_ = profile_dir.AppendASCII("Preferences"); + user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); + extensions_dir_ = profile_dir.AppendASCII("Extensions"); + + if (enable_extensions_) { + command_line->AppendSwitch(switches::kEnableExtensions); + + FilePath src_dir; + PathService::Get(chrome::DIR_TEST_DATA, &src_dir); + src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); + + file_util::CopyFile(src_dir.AppendASCII("Preferences"), + preferences_file_); + file_util::CopyDirectory(src_dir.AppendASCII("Extensions"), + profile_dir, true); // recursive + } + + if (enable_user_scripts_) { + command_line->AppendSwitch(switches::kEnableUserScripts); + + FilePath src_dir; + PathService::Get(chrome::DIR_TEST_DATA, &src_dir); + src_dir = src_dir.AppendASCII("extensions").AppendASCII("good") + .AppendASCII("Extensions") + .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") + .AppendASCII("1.0.0.0"); + + file_util::CreateDirectory(user_scripts_dir_); + file_util::CopyFile(src_dir.AppendASCII("script2.js"), + user_scripts_dir_.AppendASCII("script2.user.js")); + } + } + + // NotificationObserver + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + switch (type.value) { + case NotificationType::EXTENSIONS_READY: + case NotificationType::USER_SCRIPTS_UPDATED: + MessageLoopForUI::current()->Quit(); + break; + } + } + + virtual void TearDown() { + file_util::Delete(preferences_file_, false); + file_util::Delete(user_scripts_dir_, true); + file_util::Delete(extensions_dir_, true); + } + + FilePath preferences_file_; + FilePath extensions_dir_; + FilePath user_scripts_dir_; + bool enable_extensions_; + bool enable_user_scripts_; + NotificationRegistrar registrar_; +}; + + +// ExtensionsStartupTest +// Ensures that we can startup the browser with --enable-extensions and some +// extensions installed and see them run and do basic things. + +class ExtensionsStartupTest : public ExtensionStartupTestBase { + public: + ExtensionsStartupTest() { + enable_extensions_ = true; + } +}; + +IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) { + ExtensionsService* service = browser()->profile()->GetExtensionsService(); + if (!service->is_ready()) { + registrar_.Add(this, NotificationType::EXTENSIONS_READY, + NotificationService::AllSources()); + ui_test_utils::RunMessageLoop(); + registrar_.Remove(this, NotificationType::EXTENSIONS_READY, + NotificationService::AllSources()); + } + ASSERT_EQ(3u, service->extensions()->size()); + ASSERT_TRUE(service->extensions_enabled()); + + UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); + if (!master->ScriptsReady()) { + // Wait for UserScriptMaster to finish its scan. + registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, + NotificationService::AllSources()); + ui_test_utils::RunMessageLoop(); + registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, + NotificationService::AllSources()); + } + ASSERT_TRUE(master->ScriptsReady()); + + FilePath test_file; + PathService::Get(chrome::DIR_TEST_DATA, &test_file); + test_file = test_file.AppendASCII("extensions") + .AppendASCII("test_file.html"); + + // Now we should be able to load a page affected by the content script and see + // the effect. + ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); + + // Test that the content script ran. + bool result = false; + ui_test_utils::ExecuteJavaScriptAndExtractBool( + browser()->GetSelectedTabContents(), L"", + L"window.domAutomationController.send(" + L"document.defaultView.getComputedStyle(document.body, null)." + L"getPropertyValue('background-color') == 'rgb(245, 245, 220)')", + &result); + EXPECT_TRUE(result); + + ui_test_utils::ExecuteJavaScriptAndExtractBool( + browser()->GetSelectedTabContents(), L"", + L"window.domAutomationController.send(document.title == 'Modified')", + &result); + EXPECT_TRUE(result); + + // TODO(aa): Move the stuff in ExtensionBrowserTest here? +} + + +// ExtensionsStartupUserScriptTest +// Tests that we can startup with --enable-user-scripts and run user sripts and +// see them do basic things. + +class ExtensionsStartupUserScriptTest : public ExtensionStartupTestBase { + public: + ExtensionsStartupUserScriptTest() { + enable_user_scripts_ = true; + } +}; + +IN_PROC_BROWSER_TEST_F(ExtensionsStartupUserScriptTest, Test) { + UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); + if (!master->ScriptsReady()) { + // Wait for UserScriptMaster to finish its scan. + registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, + NotificationService::AllSources()); + ui_test_utils::RunMessageLoop(); + registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED, + NotificationService::AllSources()); + } + ASSERT_TRUE(master->ScriptsReady()); + + FilePath test_file; + PathService::Get(chrome::DIR_TEST_DATA, &test_file); + test_file = test_file.AppendASCII("extensions") + .AppendASCII("test_file.html"); + + // Now we should be able to load a page affected by the content script and see + // the effect. + ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); + + // Test that the user script ran. + bool result = false; + ui_test_utils::ExecuteJavaScriptAndExtractBool( + browser()->GetSelectedTabContents(), L"", + L"window.domAutomationController.send(document.title == 'Modified')", + &result); + EXPECT_TRUE(result); +} diff --git a/chrome/browser/extensions/extensions_service_unittest.cc b/chrome/browser/extensions/extensions_service_unittest.cc index 8de8b61..92cd377 100644 --- a/chrome/browser/extensions/extensions_service_unittest.cc +++ b/chrome/browser/extensions/extensions_service_unittest.cc @@ -521,11 +521,13 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectorySuccess) { const UserScriptList& scripts = extension->content_scripts(); const std::vector<std::string>& toolstrips = extension->toolstrips(); ASSERT_EQ(2u, scripts.size()); - EXPECT_EQ(2u, scripts[0].url_patterns().size()); - EXPECT_EQ("http://*.google.com/*", + EXPECT_EQ(3u, scripts[0].url_patterns().size()); + EXPECT_EQ("file://*", scripts[0].url_patterns()[0].GetAsString()); - EXPECT_EQ("https://*.google.com/*", + EXPECT_EQ("http://*.google.com/*", scripts[0].url_patterns()[1].GetAsString()); + EXPECT_EQ("https://*.google.com/*", + scripts[0].url_patterns()[2].GetAsString()); EXPECT_EQ(2u, scripts[0].js_scripts().size()); EXPECT_EQ( NormalizeSeperators(extension->path().AppendASCII("script1.js").value()), @@ -1389,10 +1391,41 @@ TEST_F(ExtensionsServiceTest, ExternalPrefProvider) { EXPECT_EQ(1, visitor.Visit(json_data, ignore_list)); } +class ExtensionsReadyRecorder : public NotificationObserver { + public: + ExtensionsReadyRecorder() : ready_(false) { + registrar_.Add(this, NotificationType::EXTENSIONS_READY, + NotificationService::AllSources()); + } + + void set_ready(bool value) { ready_ = value; } + bool ready() { return ready_; } + + private: + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + switch (type.value) { + case NotificationType::EXTENSIONS_READY: + ready_ = true; + break; + default: + NOTREACHED(); + } + } + + NotificationRegistrar registrar_; + bool ready_; +}; + // Test that we get enabled/disabled correctly for all the pref/command-line // combinations. We don't want to derive from the ExtensionsServiceTest class // for this test, so we use ExtensionsServiceTestSimple. +// +// Also tests that we always fire EXTENSIONS_READY, no matter whether we are +// enabled or not. TEST(ExtensionsServiceTestSimple, Enabledness) { + ExtensionsReadyRecorder recorder; TestingProfile profile; MessageLoop loop; scoped_ptr<CommandLine> command_line; @@ -1405,20 +1438,35 @@ TEST(ExtensionsServiceTestSimple, Enabledness) { service = new ExtensionsService(&profile, command_line.get(), profile.GetPrefs(), install_dir, &loop, &loop); EXPECT_FALSE(service->extensions_enabled()); + service->Init(); + loop.RunAllPending(); + EXPECT_TRUE(recorder.ready()); // If either the command line or pref is set, we are enabled. + recorder.set_ready(false); command_line->AppendSwitch(switches::kEnableExtensions); service = new ExtensionsService(&profile, command_line.get(), profile.GetPrefs(), install_dir, &loop, &loop); EXPECT_TRUE(service->extensions_enabled()); + service->Init(); + loop.RunAllPending(); + EXPECT_TRUE(recorder.ready()); + recorder.set_ready(false); profile.GetPrefs()->SetBoolean(prefs::kEnableExtensions, true); service = new ExtensionsService(&profile, command_line.get(), profile.GetPrefs(), install_dir, &loop, &loop); EXPECT_TRUE(service->extensions_enabled()); + service->Init(); + loop.RunAllPending(); + EXPECT_TRUE(recorder.ready()); + recorder.set_ready(false); command_line.reset(new CommandLine(L"")); service = new ExtensionsService(&profile, command_line.get(), profile.GetPrefs(), install_dir, &loop, &loop); EXPECT_TRUE(service->extensions_enabled()); + service->Init(); + loop.RunAllPending(); + EXPECT_TRUE(recorder.ready()); } |