| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/user_script_master.h" | 5 #include "chrome/browser/extensions/user_script_master.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 scoped_ptr<content::TestBrowserThread> file_thread_; | 83 scoped_ptr<content::TestBrowserThread> file_thread_; |
| 84 scoped_ptr<content::TestBrowserThread> ui_thread_; | 84 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 85 | 85 |
| 86 // Updated to the script shared memory when we get notified. | 86 // Updated to the script shared memory when we get notified. |
| 87 base::SharedMemory* shared_memory_; | 87 base::SharedMemory* shared_memory_; |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // Test that we get notified even when there are no scripts. | 90 // Test that we get notified even when there are no scripts. |
| 91 TEST_F(UserScriptMasterTest, NoScripts) { | 91 TEST_F(UserScriptMasterTest, NoScripts) { |
| 92 TestingProfile profile; | 92 TestingProfile profile; |
| 93 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(&profile)); | 93 UserScriptMaster master(&profile); |
| 94 master->StartLoad(); | 94 master.StartLoad(); |
| 95 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 95 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 96 message_loop_.Run(); | 96 message_loop_.Run(); |
| 97 | 97 |
| 98 ASSERT_TRUE(shared_memory_ != NULL); | 98 ASSERT_TRUE(shared_memory_ != NULL); |
| 99 } | 99 } |
| 100 | 100 |
| 101 TEST_F(UserScriptMasterTest, Parse1) { | 101 TEST_F(UserScriptMasterTest, Parse1) { |
| 102 const std::string text( | 102 const std::string text( |
| 103 "// This is my awesome script\n" | 103 "// This is my awesome script\n" |
| 104 "// It does stuff.\n" | 104 "// It does stuff.\n" |
| 105 "// ==UserScript== trailing garbage\n" | 105 "// ==UserScript== trailing garbage\n" |
| 106 "// @name foobar script\n" | 106 "// @name foobar script\n" |
| 107 "// @namespace http://www.google.com/\n" | 107 "// @namespace http://www.google.com/\n" |
| 108 "// @include *mail.google.com*\n" | 108 "// @include *mail.google.com*\n" |
| 109 "// \n" | 109 "// \n" |
| 110 "// @othergarbage\n" | 110 "// @othergarbage\n" |
| 111 "// @include *mail.yahoo.com*\r\n" | 111 "// @include *mail.yahoo.com*\r\n" |
| 112 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK | 112 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK |
| 113 "//@include not-recognized\n" // must have one space after "//" | 113 "//@include not-recognized\n" // must have one space after "//" |
| 114 "// ==/UserScript== trailing garbage\n" | 114 "// ==/UserScript== trailing garbage\n" |
| 115 "\n" | 115 "\n" |
| 116 "\n" | 116 "\n" |
| 117 "alert('hoo!');\n"); | 117 "alert('hoo!');\n"); |
| 118 | 118 |
| 119 UserScript script; | 119 UserScript script; |
| 120 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 120 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 121 text, &script)); | |
| 122 ASSERT_EQ(3U, script.globs().size()); | 121 ASSERT_EQ(3U, script.globs().size()); |
| 123 EXPECT_EQ("*mail.google.com*", script.globs()[0]); | 122 EXPECT_EQ("*mail.google.com*", script.globs()[0]); |
| 124 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); | 123 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); |
| 125 EXPECT_EQ("*mail.msn.com*", script.globs()[2]); | 124 EXPECT_EQ("*mail.msn.com*", script.globs()[2]); |
| 126 } | 125 } |
| 127 | 126 |
| 128 TEST_F(UserScriptMasterTest, Parse2) { | 127 TEST_F(UserScriptMasterTest, Parse2) { |
| 129 const std::string text("default to @include *"); | 128 const std::string text("default to @include *"); |
| 130 | 129 |
| 131 UserScript script; | 130 UserScript script; |
| 132 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 131 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 133 text, &script)); | |
| 134 ASSERT_EQ(1U, script.globs().size()); | 132 ASSERT_EQ(1U, script.globs().size()); |
| 135 EXPECT_EQ("*", script.globs()[0]); | 133 EXPECT_EQ("*", script.globs()[0]); |
| 136 } | 134 } |
| 137 | 135 |
| 138 TEST_F(UserScriptMasterTest, Parse3) { | 136 TEST_F(UserScriptMasterTest, Parse3) { |
| 139 const std::string text( | 137 const std::string text( |
| 140 "// ==UserScript==\n" | 138 "// ==UserScript==\n" |
| 141 "// @include *foo*\n" | 139 "// @include *foo*\n" |
| 142 "// ==/UserScript=="); // no trailing newline | 140 "// ==/UserScript=="); // no trailing newline |
| 143 | 141 |
| 144 UserScript script; | 142 UserScript script; |
| 145 UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script); | 143 UserScriptMaster::ParseMetadataHeader(text, &script); |
| 146 ASSERT_EQ(1U, script.globs().size()); | 144 ASSERT_EQ(1U, script.globs().size()); |
| 147 EXPECT_EQ("*foo*", script.globs()[0]); | 145 EXPECT_EQ("*foo*", script.globs()[0]); |
| 148 } | 146 } |
| 149 | 147 |
| 150 TEST_F(UserScriptMasterTest, Parse4) { | 148 TEST_F(UserScriptMasterTest, Parse4) { |
| 151 const std::string text( | 149 const std::string text( |
| 152 "// ==UserScript==\n" | 150 "// ==UserScript==\n" |
| 153 "// @match http://*.mail.google.com/*\n" | 151 "// @match http://*.mail.google.com/*\n" |
| 154 "// @match \t http://mail.yahoo.com/*\n" | 152 "// @match \t http://mail.yahoo.com/*\n" |
| 155 "// ==/UserScript==\n"); | 153 "// ==/UserScript==\n"); |
| 156 | 154 |
| 157 URLPatternSet expected_patterns; | 155 URLPatternSet expected_patterns; |
| 158 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); | 156 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); |
| 159 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); | 157 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); |
| 160 | 158 |
| 161 UserScript script; | 159 UserScript script; |
| 162 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 160 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 163 text, &script)); | |
| 164 EXPECT_EQ(0U, script.globs().size()); | 161 EXPECT_EQ(0U, script.globs().size()); |
| 165 EXPECT_EQ(expected_patterns, script.url_patterns()); | 162 EXPECT_EQ(expected_patterns, script.url_patterns()); |
| 166 } | 163 } |
| 167 | 164 |
| 168 TEST_F(UserScriptMasterTest, Parse5) { | 165 TEST_F(UserScriptMasterTest, Parse5) { |
| 169 const std::string text( | 166 const std::string text( |
| 170 "// ==UserScript==\n" | 167 "// ==UserScript==\n" |
| 171 "// @match http://*mail.google.com/*\n" | 168 "// @match http://*mail.google.com/*\n" |
| 172 "// ==/UserScript==\n"); | 169 "// ==/UserScript==\n"); |
| 173 | 170 |
| 174 // Invalid @match value. | 171 // Invalid @match value. |
| 175 UserScript script; | 172 UserScript script; |
| 176 EXPECT_FALSE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 173 EXPECT_FALSE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 177 text, &script)); | |
| 178 } | 174 } |
| 179 | 175 |
| 180 TEST_F(UserScriptMasterTest, Parse6) { | 176 TEST_F(UserScriptMasterTest, Parse6) { |
| 181 const std::string text( | 177 const std::string text( |
| 182 "// ==UserScript==\n" | 178 "// ==UserScript==\n" |
| 183 "// @include http://*.mail.google.com/*\n" | 179 "// @include http://*.mail.google.com/*\n" |
| 184 "// @match \t http://mail.yahoo.com/*\n" | 180 "// @match \t http://mail.yahoo.com/*\n" |
| 185 "// ==/UserScript==\n"); | 181 "// ==/UserScript==\n"); |
| 186 | 182 |
| 187 // Allowed to match @include and @match. | 183 // Allowed to match @include and @match. |
| 188 UserScript script; | 184 UserScript script; |
| 189 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 185 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 190 text, &script)); | |
| 191 } | 186 } |
| 192 | 187 |
| 193 TEST_F(UserScriptMasterTest, Parse7) { | 188 TEST_F(UserScriptMasterTest, Parse7) { |
| 194 // Greasemonkey allows there to be any leading text before the comment marker. | 189 // Greasemonkey allows there to be any leading text before the comment marker. |
| 195 const std::string text( | 190 const std::string text( |
| 196 "// ==UserScript==\n" | 191 "// ==UserScript==\n" |
| 197 "adsasdfasf// @name hello\n" | 192 "adsasdfasf// @name hello\n" |
| 198 " // @description\twiggity woo\n" | 193 " // @description\twiggity woo\n" |
| 199 "\t// @match \t http://mail.yahoo.com/*\n" | 194 "\t// @match \t http://mail.yahoo.com/*\n" |
| 200 "// ==/UserScript==\n"); | 195 "// ==/UserScript==\n"); |
| 201 | 196 |
| 202 UserScript script; | 197 UserScript script; |
| 203 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 198 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 204 text, &script)); | |
| 205 ASSERT_EQ("hello", script.name()); | 199 ASSERT_EQ("hello", script.name()); |
| 206 ASSERT_EQ("wiggity woo", script.description()); | 200 ASSERT_EQ("wiggity woo", script.description()); |
| 207 ASSERT_EQ(1U, script.url_patterns().patterns().size()); | 201 ASSERT_EQ(1U, script.url_patterns().patterns().size()); |
| 208 EXPECT_EQ("http://mail.yahoo.com/*", | 202 EXPECT_EQ("http://mail.yahoo.com/*", |
| 209 script.url_patterns().begin()->GetAsString()); | 203 script.url_patterns().begin()->GetAsString()); |
| 210 } | 204 } |
| 211 | 205 |
| 212 TEST_F(UserScriptMasterTest, Parse8) { | 206 TEST_F(UserScriptMasterTest, Parse8) { |
| 213 const std::string text( | 207 const std::string text( |
| 214 "// ==UserScript==\n" | 208 "// ==UserScript==\n" |
| 215 "// @name myscript\n" | 209 "// @name myscript\n" |
| 216 "// @match http://www.google.com/*\n" | 210 "// @match http://www.google.com/*\n" |
| 217 "// @exclude_match http://www.google.com/foo*\n" | 211 "// @exclude_match http://www.google.com/foo*\n" |
| 218 "// ==/UserScript==\n"); | 212 "// ==/UserScript==\n"); |
| 219 | 213 |
| 220 UserScript script; | 214 UserScript script; |
| 221 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 215 EXPECT_TRUE(UserScriptMaster::ParseMetadataHeader(text, &script)); |
| 222 text, &script)); | |
| 223 ASSERT_EQ("myscript", script.name()); | 216 ASSERT_EQ("myscript", script.name()); |
| 224 ASSERT_EQ(1U, script.url_patterns().patterns().size()); | 217 ASSERT_EQ(1U, script.url_patterns().patterns().size()); |
| 225 EXPECT_EQ("http://www.google.com/*", | 218 EXPECT_EQ("http://www.google.com/*", |
| 226 script.url_patterns().begin()->GetAsString()); | 219 script.url_patterns().begin()->GetAsString()); |
| 227 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size()); | 220 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size()); |
| 228 EXPECT_EQ("http://www.google.com/foo*", | 221 EXPECT_EQ("http://www.google.com/foo*", |
| 229 script.exclude_url_patterns().begin()->GetAsString()); | 222 script.exclude_url_patterns().begin()->GetAsString()); |
| 230 } | 223 } |
| 231 | 224 |
| 232 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { | 225 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { |
| 233 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); | 226 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
| 234 const std::string content("\xEF\xBB\xBF alert('hello');"); | 227 const std::string content("\xEF\xBB\xBF alert('hello');"); |
| 235 size_t written = base::WriteFile(path, content.c_str(), content.size()); | 228 size_t written = base::WriteFile(path, content.c_str(), content.size()); |
| 236 ASSERT_EQ(written, content.size()); | 229 ASSERT_EQ(written, content.size()); |
| 237 | 230 |
| 238 UserScript user_script; | 231 UserScript user_script; |
| 239 user_script.js_scripts().push_back(UserScript::File( | 232 user_script.js_scripts().push_back(UserScript::File( |
| 240 temp_dir_.path(), path.BaseName(), GURL())); | 233 temp_dir_.path(), path.BaseName(), GURL())); |
| 241 | 234 |
| 242 UserScriptList user_scripts; | 235 UserScriptList user_scripts; |
| 243 user_scripts.push_back(user_script); | 236 user_scripts.push_back(user_script); |
| 244 | 237 |
| 245 UserScriptMaster::ScriptReloader* script_reloader = | 238 UserScriptMaster::LoadScriptsForTest(&user_scripts); |
| 246 new UserScriptMaster::ScriptReloader(NULL); | |
| 247 script_reloader->AddRef(); | |
| 248 script_reloader->LoadUserScripts(&user_scripts); | |
| 249 script_reloader->Release(); | |
| 250 | 239 |
| 251 EXPECT_EQ(content.substr(3), | 240 EXPECT_EQ(content.substr(3), |
| 252 user_scripts[0].js_scripts()[0].GetContent().as_string()); | 241 user_scripts[0].js_scripts()[0].GetContent().as_string()); |
| 253 } | 242 } |
| 254 | 243 |
| 255 TEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) { | 244 TEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) { |
| 256 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); | 245 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
| 257 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');"); | 246 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');"); |
| 258 size_t written = base::WriteFile(path, content.c_str(), content.size()); | 247 size_t written = base::WriteFile(path, content.c_str(), content.size()); |
| 259 ASSERT_EQ(written, content.size()); | 248 ASSERT_EQ(written, content.size()); |
| 260 | 249 |
| 261 UserScript user_script; | 250 UserScript user_script; |
| 262 user_script.js_scripts().push_back(UserScript::File( | 251 user_script.js_scripts().push_back(UserScript::File( |
| 263 temp_dir_.path(), path.BaseName(), GURL())); | 252 temp_dir_.path(), path.BaseName(), GURL())); |
| 264 | 253 |
| 265 UserScriptList user_scripts; | 254 UserScriptList user_scripts; |
| 266 user_scripts.push_back(user_script); | 255 user_scripts.push_back(user_script); |
| 267 | 256 |
| 268 UserScriptMaster::ScriptReloader* script_reloader = | 257 UserScriptMaster::LoadScriptsForTest(&user_scripts); |
| 269 new UserScriptMaster::ScriptReloader(NULL); | |
| 270 script_reloader->AddRef(); | |
| 271 script_reloader->LoadUserScripts(&user_scripts); | |
| 272 script_reloader->Release(); | |
| 273 | 258 |
| 274 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); | 259 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); |
| 275 } | 260 } |
| 276 | 261 |
| 277 } // namespace extensions | 262 } // namespace extensions |
| OLD | NEW |