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