Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Devlin
2014/08/26 21:59:16
nit of nits: This is actually more of an API test
Mark Dittmer
2014/08/27 22:37:04
File renamed.
| |
| 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" | |
| 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 "*://*/*", // MP_ALL | |
| 26 "http://127.0.0.1/*", // MP_PARTICULAR | |
| 27 "http://nowhere.com/*" // MP_NOWHERE | |
| 28 }; | |
| 29 | |
| 30 // Script matchers for injected into |kBackgroundScriptSource|: | |
| 31 const char* kScriptMatchers[] = { | |
| 32 "{ pageUrl: { hostContains: '' } }", // SM_ALL | |
| 33 "{ pageUrl: { hostEquals: '127.0.0.1' } }", // SM_PARTICULAR | |
| 34 "{ pageUrl: { hostEquals: 'nowhere.com' } }" // SM_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" | |
| 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 ManifestPermissionType { | |
| 76 MP_ALL = 0, | |
|
Devlin
2014/08/26 21:59:16
If this and ScriptMatcherType are always gonna be
Mark Dittmer
2014/08/27 22:37:04
Currently, they fit into the same categorization s
| |
| 77 MP_PARTICULAR, | |
| 78 MP_NOWHERE | |
| 79 }; | |
| 80 | |
| 81 enum ScriptMatcherType { | |
| 82 SM_ALL = 0, | |
| 83 SM_PARTICULAR, | |
| 84 SM_NOWHERE | |
| 85 }; | |
| 86 | |
| 87 // Runs all pending tasks in the renderer associated with |web_contents|. | |
| 88 // Returns true on success. | |
| 89 bool RunAllPendingInRenderer(content::WebContents* web_contents) { | |
| 90 // TODO(devlin): If too many tests start to need this, move it somewhere | |
| 91 // common. | |
| 92 // This is slight hack to achieve a RunPendingInRenderer() method. Since IPCs | |
| 93 // are sent synchronously, anything started prior to this method will finish | |
| 94 // before this method returns (as content::ExecuteScript() is synchronous). | |
| 95 return content::ExecuteScript(web_contents, "1 == 1;"); | |
| 96 } | |
| 97 | |
| 98 } // namespace | |
| 99 | |
| 100 class RequestContentScriptBrowserTest : public ExtensionBrowserTest { | |
| 101 public: | |
| 102 RequestContentScriptBrowserTest(); | |
| 103 | |
| 104 // Performs script injection test on a common local URL using the given | |
| 105 // |manifest_permission| and |script_matcher|. Does not return until | |
| 106 // the renderer should have completed its task and any browser-side reactions | |
| 107 // have been cleared from the task queue. | |
| 108 testing::AssertionResult RunTest(ManifestPermissionType manifest_permission, | |
|
Devlin
2014/08/26 21:59:16
indentation
Mark Dittmer
2014/08/27 22:37:04
Done.
| |
| 109 ScriptMatcherType script_matcher); | |
| 110 | |
| 111 ExtensionTestMessageListener* injection_setup_listener() { | |
| 112 return injection_setup_listener_.get(); | |
| 113 } | |
| 114 ExtensionTestMessageListener* injection_succeeded_listener() { | |
| 115 return injection_succeeded_listener_.get(); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 scoped_ptr<TestExtensionDir> test_extension_dir_; | |
| 120 const Extension* extension_; | |
| 121 scoped_ptr<ExtensionTestMessageListener> injection_setup_listener_; | |
|
Devlin
2014/08/26 21:59:15
Why do these need to be member variables?
Mark Dittmer
2014/08/27 22:37:04
This ensures the lifetime of the listeners for use
Devlin
2014/08/27 23:01:11
See comment on new patch set.
| |
| 122 scoped_ptr<ExtensionTestMessageListener> injection_succeeded_listener_; | |
| 123 bool server_is_initialized_; | |
| 124 }; | |
| 125 | |
| 126 RequestContentScriptBrowserTest::RequestContentScriptBrowserTest() | |
| 127 : extension_(NULL), | |
| 128 server_is_initialized_(false) {} | |
| 129 | |
| 130 testing::AssertionResult RequestContentScriptBrowserTest::RunTest( | |
| 131 ManifestPermissionType manifest_permission, | |
| 132 ScriptMatcherType script_matcher) { | |
| 133 std::string manifest = base::StringPrintf(kManifest, | |
|
Devlin
2014/08/26 21:59:16
Pull this part out into a CreatendLoadExtension(ty
Mark Dittmer
2014/08/28 13:02:41
Done.
| |
| 134 kPermissions[manifest_permission]); | |
| 135 std::string background_src = base::StringPrintf( | |
| 136 kBackgroundScriptSource, | |
| 137 kScriptMatchers[script_matcher]); | |
| 138 | |
| 139 scoped_ptr<TestExtensionDir> dir(new TestExtensionDir); | |
| 140 dir->WriteManifest(manifest); | |
| 141 dir->WriteFile(FILE_PATH_LITERAL("background.js"), background_src); | |
| 142 dir->WriteFile(FILE_PATH_LITERAL("script.js"), | |
| 143 kContentScriptSource); | |
| 144 | |
| 145 const Extension* extension = LoadExtension(dir->unpacked_path()); | |
| 146 if (!extension) | |
| 147 return testing::AssertionFailure() << "Failed to load extension."; | |
| 148 | |
| 149 if (extension_) | |
|
Devlin
2014/08/26 21:59:15
nit: Cleanup _before_ creating the new stuff.
Mark Dittmer
2014/08/28 13:02:41
Done.
| |
| 150 UnloadExtension(extension_->id()); | |
| 151 | |
| 152 test_extension_dir_.reset(dir.release()); | |
| 153 extension_ = extension; | |
| 154 | |
| 155 injection_setup_listener_.reset(new ExtensionTestMessageListener( | |
| 156 kInjectionSetup, | |
| 157 false /* won't reply */)); | |
| 158 injection_succeeded_listener_.reset(new ExtensionTestMessageListener( | |
| 159 kInjectionSucceeded, | |
| 160 false /* won't reply */)); | |
| 161 | |
| 162 injection_setup_listener_->set_extension_id(extension->id()); | |
| 163 injection_succeeded_listener_->set_extension_id(extension->id()); | |
| 164 | |
| 165 if (!server_is_initialized_) { | |
| 166 if (!embedded_test_server()->InitializeAndWaitUntilReady()) { | |
|
Devlin
2014/08/26 21:59:15
Since this will only be done once, just do it in t
Mark Dittmer
2014/08/27 22:37:04
Done.
| |
| 167 return testing::AssertionFailure() << | |
| 168 "Could not initialize embedded test server."; | |
| 169 } | |
| 170 server_is_initialized_ = true; | |
| 171 } | |
| 172 | |
| 173 // Wait for rules to be setup before navigating to trigger script injection. | |
| 174 injection_setup_listener_->WaitUntilSatisfied(); | |
| 175 | |
|
Devlin
2014/08/26 21:59:15
We should also check that injection has *not* occu
Mark Dittmer
2014/08/27 22:37:04
Why? Done, just the same.
| |
| 176 ui_test_utils::NavigateToURL( | |
| 177 browser(), | |
| 178 embedded_test_server()->GetURL("/extensions/test_file.html")); | |
| 179 | |
| 180 content::WebContents* web_contents = | |
| 181 browser() ? browser()->tab_strip_model()->GetActiveWebContents() : NULL; | |
| 182 if (!web_contents) | |
| 183 return testing::AssertionFailure() << "No web contents."; | |
| 184 | |
| 185 // Give the extension plenty of time to inject. | |
| 186 if (!RunAllPendingInRenderer(web_contents)) | |
| 187 return testing::AssertionFailure() << "Could not run pending in renderer."; | |
| 188 | |
| 189 // Make sure all running tasks are complete. | |
| 190 content::RunAllPendingInMessageLoop(); | |
| 191 | |
| 192 return testing::AssertionSuccess(); | |
| 193 } | |
| 194 | |
| 195 // Try different permutations of "match all", "match particular domain (that is | |
| 196 // visited by test)", and "match nonsense domain (not visited by test)" for | |
| 197 // both manifest permissions and injection matcher conditions. | |
| 198 IN_PROC_BROWSER_TEST_F(RequestContentScriptBrowserTest, | |
| 199 PermissionMatcherAgreementInjection) { | |
| 200 // Positive tests: permissions and matcher contain conditions that match URL | |
| 201 // visited during test. | |
| 202 EXPECT_TRUE(RunTest(MP_ALL, SM_ALL)); | |
| 203 ASSERT_TRUE(injection_succeeded_listener()->was_satisfied()); | |
|
Devlin
2014/08/26 21:59:16
Move this into RunTest as a third param (should_in
Mark Dittmer
2014/08/27 22:37:04
Done.
| |
| 204 | |
| 205 EXPECT_TRUE(RunTest(MP_ALL, SM_PARTICULAR)); | |
| 206 ASSERT_TRUE(injection_succeeded_listener()->was_satisfied()); | |
| 207 | |
| 208 EXPECT_TRUE(RunTest(MP_PARTICULAR, SM_ALL)); | |
| 209 ASSERT_TRUE(injection_succeeded_listener()->was_satisfied()); | |
| 210 | |
| 211 EXPECT_TRUE(RunTest(MP_PARTICULAR, SM_PARTICULAR)); | |
| 212 ASSERT_TRUE(injection_succeeded_listener()->was_satisfied()); | |
| 213 | |
| 214 // Negative tests: permissions or matcher (or both) contain conditions that | |
| 215 // do not match URL visited during test. | |
| 216 EXPECT_TRUE(RunTest(MP_NOWHERE, SM_ALL)); | |
| 217 ASSERT_FALSE(injection_succeeded_listener()->was_satisfied()); | |
| 218 | |
| 219 EXPECT_TRUE(RunTest(MP_NOWHERE, SM_PARTICULAR)); | |
| 220 ASSERT_FALSE(injection_succeeded_listener()->was_satisfied()); | |
| 221 | |
| 222 EXPECT_TRUE(RunTest(MP_NOWHERE, SM_NOWHERE)); | |
| 223 ASSERT_FALSE(injection_succeeded_listener()->was_satisfied()); | |
| 224 | |
| 225 EXPECT_TRUE(RunTest(MP_ALL, SM_NOWHERE)); | |
| 226 ASSERT_FALSE(injection_succeeded_listener()->was_satisfied()); | |
| 227 | |
| 228 EXPECT_TRUE(RunTest(MP_PARTICULAR, SM_NOWHERE)); | |
| 229 ASSERT_FALSE(injection_succeeded_listener()->was_satisfied()); | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 // EXPECT_TRUE(Init(MP_PARTICULAR, SM_ALL)); | |
|
Devlin
2014/08/26 21:59:15
??
Mark Dittmer
2014/08/27 22:37:04
Experimentation leftovers. Removed.
| |
| 235 // EXPECT_TRUE(RunTest()); | |
| 236 // ASSERT_TRUE(injection_succeeded_listener()->WaitUntilSatisfied()); | |
| 237 | |
| 238 // EXPECT_TRUE(Init(MP_PARTICULAR, SM_ALL)); | |
| 239 // EXPECT_TRUE(RunTest()); | |
| 240 // ASSERT_TRUE(injection_succeeded_listener()->WaitUntilSatisfied()); | |
| 241 } | |
| 242 | |
| 243 } // namespace extensions | |
| OLD | NEW |