Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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/files/file_path.h" | |
| 6 #include "base/macros.h" | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
| 10 #include "chrome/browser/extensions/test_extension_dir.h" | |
| 11 #include "chrome/browser/ui/browser.h" | |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 13 #include "chrome/test/base/ui_test_utils.h" | |
| 14 #include "content/public/test/browser_test_utils.h" | |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Manifest permissions injected into |kManifest|: | |
| 23 const char* kPermissions[] = { | |
| 24 "*://*/*", // ALL | |
| 25 "http://127.0.0.1/*", // PARTICULAR | |
| 26 "http://nowhere.com/*" // NOWHERE | |
| 27 }; | |
| 28 | |
| 29 // Script matchers for injected into |kBackgroundScriptSource|: | |
| 30 const char* kScriptMatchers[] = { | |
| 31 "{ pageUrl: { hostContains: '' } }", // ALL | |
| 32 "{ pageUrl: { hostEquals: '127.0.0.1' } }", // PARTICULAR | |
| 33 "{ pageUrl: { hostEquals: 'nowhere.com' } }" // NOWHERE | |
| 34 }; | |
| 35 | |
| 36 enum PermissionOrMatcherType { | |
| 37 ALL = 0, | |
| 38 PARTICULAR, | |
| 39 NOWHERE | |
| 40 }; | |
| 41 | |
| 42 // JSON/JS sources: | |
| 43 const char kManifest[] = | |
| 44 "{\n" | |
| 45 " \"name\": \"Test DeclarativeContentScript\",\n" | |
| 46 " \"manifest_version\": 2,\n" | |
| 47 " \"version\": \"1.0\",\n" | |
| 48 " \"description\": \"Test declarative content script interface\",\n" | |
| 49 " \"permissions\": [\"declarativeContent\", \"%s\"],\n" | |
| 50 " \"background\": {\n" | |
| 51 " \"scripts\": [\"background.js\"]\n" | |
| 52 " }\n" | |
| 53 "}\n"; | |
| 54 const char kBackgroundScriptSource[] = | |
| 55 "var declarativeContent = chrome.declarativeContent;\n" | |
| 56 "var PageStateMatcher = declarativeContent.PageStateMatcher;\n" | |
| 57 "var RequestContentScript = declarativeContent.RequestContentScript;\n" | |
| 58 "var onPageChanged = declarativeContent.onPageChanged;\n" | |
| 59 "onPageChanged.removeRules(undefined, function() {\n" | |
| 60 " onPageChanged.addRules(\n" | |
| 61 " [{\n" | |
| 62 " conditions: [new PageStateMatcher(%s)],\n" | |
| 63 " actions: [new RequestContentScript({js: ['script.js']}\n" | |
| 64 " )]\n" | |
| 65 " }],\n" | |
| 66 " function(details) {\n" | |
| 67 " if (!chrome.runtime.lastError)\n" | |
| 68 " chrome.test.sendMessage('injection setup');\n" | |
| 69 " }\n" | |
| 70 " );\n" | |
| 71 "});\n"; | |
| 72 const char kContentScriptSource[] = | |
| 73 "chrome.test.sendMessage('injection succeeded');\n"; | |
| 74 | |
| 75 // Messages from scripts: | |
| 76 const char kInjectionSetup[] = "injection setup"; | |
| 77 const char kInjectionSucceeded[] = "injection succeeded"; | |
| 78 | |
| 79 // Runs all pending tasks in the renderer associated with |web_contents|. | |
| 80 // Returns true on success. | |
| 81 bool RunAllPendingInRenderer(content::WebContents* web_contents) { | |
| 82 // TODO(devlin): If too many tests start to need this, move it somewhere | |
| 83 // common. | |
| 84 // This is slight hack to achieve a RunPendingInRenderer() method. Since IPCs | |
| 85 // are sent synchronously, anything started prior to this method will finish | |
| 86 // before this method returns (as content::ExecuteScript() is synchronous). | |
| 87 return content::ExecuteScript(web_contents, "1 == 1;"); | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 class RequestContentScriptAPITest : public ExtensionBrowserTest { | |
| 93 public: | |
| 94 RequestContentScriptAPITest(); | |
| 95 virtual ~RequestContentScriptAPITest() {} | |
| 96 | |
| 97 // Performs script injection test on a common local URL using the given | |
| 98 // |manifest_permission| and |script_matcher|. Does not return until | |
| 99 // the renderer should have completed its task and any browser-side reactions | |
| 100 // have been cleared from the task queue. | |
| 101 testing::AssertionResult RunTest(PermissionOrMatcherType manifest_permission, | |
| 102 PermissionOrMatcherType script_matcher, | |
| 103 bool should_inject); | |
| 104 | |
| 105 private: | |
| 106 testing::AssertionResult CreateAndLoadExtension( | |
| 107 PermissionOrMatcherType manifest_permission, | |
| 108 PermissionOrMatcherType script_matcher); | |
| 109 | |
| 110 scoped_ptr<TestExtensionDir> test_extension_dir_; | |
| 111 const Extension* extension_; | |
| 112 }; | |
| 113 | |
| 114 RequestContentScriptAPITest::RequestContentScriptAPITest() | |
| 115 : extension_(NULL) {} | |
| 116 | |
| 117 testing::AssertionResult RequestContentScriptAPITest::RunTest( | |
| 118 PermissionOrMatcherType manifest_permission, | |
| 119 PermissionOrMatcherType script_matcher, | |
| 120 bool should_inject) { | |
| 121 scoped_ptr<ExtensionTestMessageListener> injection_setup_listener; | |
| 122 scoped_ptr<ExtensionTestMessageListener> injection_succeeded_listener; | |
|
Devlin
2014/08/28 21:38:10
No reason for scoped_ptrs here.
Mark Dittmer
2014/08/28 23:49:47
Done.
| |
| 123 | |
| 124 if (extension_) | |
| 125 UnloadExtension(extension_->id()); | |
| 126 EXPECT_TRUE(CreateAndLoadExtension(manifest_permission, script_matcher)); | |
|
Devlin
2014/08/28 21:38:10
This should be something like:
if (!CreateAndLoadE
Mark Dittmer
2014/08/28 23:49:46
Done.
| |
| 127 | |
| 128 // Wait for rules to be setup before navigating to trigger script injection. | |
| 129 injection_setup_listener.reset(new ExtensionTestMessageListener( | |
|
Devlin
2014/08/28 21:38:10
This looks racy to me. We should have the injecti
Mark Dittmer
2014/08/28 23:49:47
Done.
| |
| 130 kInjectionSetup, | |
| 131 false /* won't reply */)); | |
| 132 injection_setup_listener->set_extension_id(extension_->id()); | |
| 133 injection_setup_listener->WaitUntilSatisfied(); | |
| 134 | |
| 135 // Setup listener for actual injection of script. | |
| 136 injection_succeeded_listener.reset(new ExtensionTestMessageListener( | |
| 137 kInjectionSucceeded, | |
| 138 false /* won't reply */)); | |
| 139 injection_succeeded_listener->set_extension_id(extension_->id()); | |
| 140 | |
| 141 ui_test_utils::NavigateToURL( | |
| 142 browser(), | |
| 143 embedded_test_server()->GetURL("/extensions/test_file.html")); | |
| 144 | |
| 145 content::WebContents* web_contents = | |
| 146 browser() ? browser()->tab_strip_model()->GetActiveWebContents() : NULL; | |
| 147 if (!web_contents) | |
| 148 return testing::AssertionFailure() << "No web contents."; | |
| 149 | |
| 150 // Give the extension plenty of time to inject. | |
| 151 if (!RunAllPendingInRenderer(web_contents)) | |
| 152 return testing::AssertionFailure() << "Could not run pending in renderer."; | |
| 153 | |
| 154 // Make sure all running tasks are complete. | |
| 155 content::RunAllPendingInMessageLoop(); | |
| 156 | |
| 157 if (injection_succeeded_listener->was_satisfied() != should_inject) { | |
| 158 return testing::AssertionFailure() | |
| 159 << (should_inject ? | |
| 160 "Expected injection, but got none." : | |
| 161 "Expected no injection, but got one."); | |
| 162 } | |
| 163 | |
| 164 return testing::AssertionSuccess(); | |
| 165 } | |
| 166 | |
| 167 testing::AssertionResult RequestContentScriptAPITest::CreateAndLoadExtension( | |
| 168 PermissionOrMatcherType manifest_permission, | |
| 169 PermissionOrMatcherType script_matcher) { | |
| 170 std::string manifest = base::StringPrintf(kManifest, | |
| 171 kPermissions[manifest_permission]); | |
| 172 std::string background_src = base::StringPrintf( | |
| 173 kBackgroundScriptSource, | |
| 174 kScriptMatchers[script_matcher]); | |
| 175 | |
| 176 scoped_ptr<TestExtensionDir> dir(new TestExtensionDir); | |
| 177 dir->WriteManifest(manifest); | |
| 178 dir->WriteFile(FILE_PATH_LITERAL("background.js"), background_src); | |
| 179 dir->WriteFile(FILE_PATH_LITERAL("script.js"), | |
| 180 kContentScriptSource); | |
| 181 | |
| 182 const Extension* extension = LoadExtension(dir->unpacked_path()); | |
| 183 if (!extension) | |
| 184 return testing::AssertionFailure() << "Failed to load extension."; | |
| 185 | |
| 186 test_extension_dir_.reset(dir.release()); | |
| 187 extension_ = extension; | |
| 188 | |
| 189 return testing::AssertionSuccess(); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 // Try different permutations of "match all", "match particular domain (that is | |
| 194 // visited by test)", and "match nonsense domain (not visited by test)" for | |
| 195 // both manifest permissions and injection matcher conditions. | |
| 196 IN_PROC_BROWSER_TEST_F(RequestContentScriptAPITest, | |
| 197 PermissionMatcherAgreementInjection) { | |
| 198 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 199 | |
| 200 // Positive tests: permissions and matcher contain conditions that match URL | |
| 201 // visited during test. | |
| 202 EXPECT_TRUE(RunTest(ALL, ALL, true)); | |
| 203 EXPECT_TRUE(RunTest(ALL, PARTICULAR, true)); | |
| 204 EXPECT_TRUE(RunTest(PARTICULAR, ALL, true)); | |
| 205 EXPECT_TRUE(RunTest(PARTICULAR, PARTICULAR, true)); | |
| 206 | |
| 207 // Negative tests: permissions or matcher (or both) contain conditions that | |
| 208 // do not match URL visited during test. | |
| 209 EXPECT_TRUE(RunTest(NOWHERE, ALL, false)); | |
| 210 EXPECT_TRUE(RunTest(NOWHERE, PARTICULAR, false)); | |
| 211 EXPECT_TRUE(RunTest(NOWHERE, NOWHERE, false)); | |
| 212 EXPECT_TRUE(RunTest(ALL, NOWHERE, false)); | |
| 213 EXPECT_TRUE(RunTest(PARTICULAR, NOWHERE, false)); | |
|
Devlin
2014/08/28 21:38:10
I wonder if it would be worthwhile to ever test an
Mark Dittmer
2014/08/28 23:49:47
Done.
| |
| 214 } | |
| 215 | |
| 216 } // namespace extensions | |
| OLD | NEW |