| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/convert_user_script.h" | 14 #include "chrome/browser/extensions/convert_user_script.h" |
| 14 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 21 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 21 int schemes = URLPattern::SCHEME_ALL; | 22 int schemes = URLPattern::SCHEME_ALL; |
| 22 extent->AddPattern(URLPattern(schemes, pattern)); | 23 extent->AddPattern(URLPattern(schemes, pattern)); |
| 23 } | 24 } |
| 24 | 25 |
| 25 } | 26 } |
| 26 | 27 |
| 27 TEST(ExtensionFromUserScript, Basic) { | 28 TEST(ExtensionFromUserScript, Basic) { |
| 28 FilePath test_file; | 29 FilePath test_file; |
| 29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 30 test_file = test_file.AppendASCII("extensions") | 31 test_file = test_file.AppendASCII("extensions") |
| 31 .AppendASCII("user_script_basic.user.js"); | 32 .AppendASCII("user_script_basic.user.js"); |
| 32 | 33 |
| 33 std::string error; | 34 string16 error; |
| 34 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 35 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 35 test_file, GURL("http://www.google.com/foo"), &error)); | 36 test_file, GURL("http://www.google.com/foo"), &error)); |
| 36 | 37 |
| 37 ASSERT_TRUE(extension.get()); | 38 ASSERT_TRUE(extension.get()); |
| 38 EXPECT_EQ("", error); | 39 EXPECT_EQ(ASCIIToUTF16(""), error); |
| 39 | 40 |
| 40 // Use a temp dir so that the extensions dir will clean itself up. | 41 // Use a temp dir so that the extensions dir will clean itself up. |
| 41 ScopedTempDir ext_dir; | 42 ScopedTempDir ext_dir; |
| 42 EXPECT_TRUE(ext_dir.Set(extension->path())); | 43 EXPECT_TRUE(ext_dir.Set(extension->path())); |
| 43 | 44 |
| 44 // Validate generated extension metadata. | 45 // Validate generated extension metadata. |
| 45 EXPECT_EQ("My user script", extension->name()); | 46 EXPECT_EQ("My user script", extension->name()); |
| 46 EXPECT_EQ("2.2.2", extension->VersionString()); | 47 EXPECT_EQ("2.2.2", extension->VersionString()); |
| 47 EXPECT_EQ("Does totally awesome stuff.", extension->description()); | 48 EXPECT_EQ("Does totally awesome stuff.", extension->description()); |
| 48 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", | 49 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", |
| (...skipping 17 matching lines...) Expand all Loading... |
| 66 EXPECT_TRUE(file_util::PathExists( | 67 EXPECT_TRUE(file_util::PathExists( |
| 67 extension->path().Append(Extension::kManifestFilename))); | 68 extension->path().Append(Extension::kManifestFilename))); |
| 68 } | 69 } |
| 69 | 70 |
| 70 TEST(ExtensionFromUserScript, NoMetdata) { | 71 TEST(ExtensionFromUserScript, NoMetdata) { |
| 71 FilePath test_file; | 72 FilePath test_file; |
| 72 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 73 test_file = test_file.AppendASCII("extensions") | 74 test_file = test_file.AppendASCII("extensions") |
| 74 .AppendASCII("user_script_no_metadata.user.js"); | 75 .AppendASCII("user_script_no_metadata.user.js"); |
| 75 | 76 |
| 76 std::string error; | 77 string16 error; |
| 77 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 78 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 78 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); | 79 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); |
| 79 | 80 |
| 80 ASSERT_TRUE(extension.get()); | 81 ASSERT_TRUE(extension.get()); |
| 81 EXPECT_EQ("", error); | 82 EXPECT_EQ(ASCIIToUTF16(""), error); |
| 82 | 83 |
| 83 // Use a temp dir so that the extensions dir will clean itself up. | 84 // Use a temp dir so that the extensions dir will clean itself up. |
| 84 ScopedTempDir ext_dir; | 85 ScopedTempDir ext_dir; |
| 85 EXPECT_TRUE(ext_dir.Set(extension->path())); | 86 EXPECT_TRUE(ext_dir.Set(extension->path())); |
| 86 | 87 |
| 87 // Validate generated extension metadata. | 88 // Validate generated extension metadata. |
| 88 EXPECT_EQ("bar.user.js", extension->name()); | 89 EXPECT_EQ("bar.user.js", extension->name()); |
| 89 EXPECT_EQ("1.0", extension->VersionString()); | 90 EXPECT_EQ("1.0", extension->VersionString()); |
| 90 EXPECT_EQ("", extension->description()); | 91 EXPECT_EQ("", extension->description()); |
| 91 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", | 92 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", |
| (...skipping 17 matching lines...) Expand all Loading... |
| 109 extension->path().Append(Extension::kManifestFilename))); | 110 extension->path().Append(Extension::kManifestFilename))); |
| 110 } | 111 } |
| 111 | 112 |
| 112 TEST(ExtensionFromUserScript, NotUTF8) { | 113 TEST(ExtensionFromUserScript, NotUTF8) { |
| 113 FilePath test_file; | 114 FilePath test_file; |
| 114 | 115 |
| 115 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 116 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 116 test_file = test_file.AppendASCII("extensions") | 117 test_file = test_file.AppendASCII("extensions") |
| 117 .AppendASCII("user_script_not_utf8.user.js"); | 118 .AppendASCII("user_script_not_utf8.user.js"); |
| 118 | 119 |
| 119 std::string error; | 120 string16 error; |
| 120 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 121 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 121 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); | 122 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); |
| 122 | 123 |
| 123 ASSERT_FALSE(extension.get()); | 124 ASSERT_FALSE(extension.get()); |
| 124 EXPECT_EQ("User script must be UTF8 encoded.", error); | 125 EXPECT_EQ(ASCIIToUTF16("User script must be UTF8 encoded."), error); |
| 125 } | 126 } |
| 126 | 127 |
| 127 TEST(ExtensionFromUserScript, RunAtDocumentStart) { | 128 TEST(ExtensionFromUserScript, RunAtDocumentStart) { |
| 128 FilePath test_file; | 129 FilePath test_file; |
| 129 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 130 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 130 test_file = test_file.AppendASCII("extensions") | 131 test_file = test_file.AppendASCII("extensions") |
| 131 .AppendASCII("user_script_run_at_start.user.js"); | 132 .AppendASCII("user_script_run_at_start.user.js"); |
| 132 | 133 |
| 133 std::string error; | 134 string16 error; |
| 134 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 135 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 135 test_file, GURL("http://www.google.com/foo"), &error)); | 136 test_file, GURL("http://www.google.com/foo"), &error)); |
| 136 | 137 |
| 137 ASSERT_TRUE(extension.get()); | 138 ASSERT_TRUE(extension.get()); |
| 138 EXPECT_EQ("", error); | 139 EXPECT_EQ(ASCIIToUTF16(""), error); |
| 139 | 140 |
| 140 // Use a temp dir so that the extensions dir will clean itself up. | 141 // Use a temp dir so that the extensions dir will clean itself up. |
| 141 ScopedTempDir ext_dir; | 142 ScopedTempDir ext_dir; |
| 142 EXPECT_TRUE(ext_dir.Set(extension->path())); | 143 EXPECT_TRUE(ext_dir.Set(extension->path())); |
| 143 | 144 |
| 144 // Validate generated extension metadata. | 145 // Validate generated extension metadata. |
| 145 EXPECT_EQ("Document Start Test", extension->name()); | 146 EXPECT_EQ("Document Start Test", extension->name()); |
| 146 EXPECT_EQ("This script tests document-start", extension->description()); | 147 EXPECT_EQ("This script tests document-start", extension->description()); |
| 147 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=", | 148 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=", |
| 148 extension->public_key()); | 149 extension->public_key()); |
| 149 | 150 |
| 150 // Validate run location. | 151 // Validate run location. |
| 151 ASSERT_EQ(1u, extension->content_scripts().size()); | 152 ASSERT_EQ(1u, extension->content_scripts().size()); |
| 152 const UserScript& script = extension->content_scripts()[0]; | 153 const UserScript& script = extension->content_scripts()[0]; |
| 153 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location()); | 154 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location()); |
| 154 } | 155 } |
| 155 | 156 |
| 156 TEST(ExtensionFromUserScript, RunAtDocumentEnd) { | 157 TEST(ExtensionFromUserScript, RunAtDocumentEnd) { |
| 157 FilePath test_file; | 158 FilePath test_file; |
| 158 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 159 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 159 test_file = test_file.AppendASCII("extensions") | 160 test_file = test_file.AppendASCII("extensions") |
| 160 .AppendASCII("user_script_run_at_end.user.js"); | 161 .AppendASCII("user_script_run_at_end.user.js"); |
| 161 | 162 |
| 162 std::string error; | 163 string16 error; |
| 163 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 164 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 164 test_file, GURL("http://www.google.com/foo"), &error)); | 165 test_file, GURL("http://www.google.com/foo"), &error)); |
| 165 | 166 |
| 166 ASSERT_TRUE(extension.get()); | 167 ASSERT_TRUE(extension.get()); |
| 167 EXPECT_EQ("", error); | 168 EXPECT_EQ(ASCIIToUTF16(""), error); |
| 168 | 169 |
| 169 // Use a temp dir so that the extensions dir will clean itself up. | 170 // Use a temp dir so that the extensions dir will clean itself up. |
| 170 ScopedTempDir ext_dir; | 171 ScopedTempDir ext_dir; |
| 171 EXPECT_TRUE(ext_dir.Set(extension->path())); | 172 EXPECT_TRUE(ext_dir.Set(extension->path())); |
| 172 | 173 |
| 173 // Validate generated extension metadata. | 174 // Validate generated extension metadata. |
| 174 EXPECT_EQ("Document End Test", extension->name()); | 175 EXPECT_EQ("Document End Test", extension->name()); |
| 175 EXPECT_EQ("This script tests document-end", extension->description()); | 176 EXPECT_EQ("This script tests document-end", extension->description()); |
| 176 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=", | 177 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=", |
| 177 extension->public_key()); | 178 extension->public_key()); |
| 178 | 179 |
| 179 // Validate run location. | 180 // Validate run location. |
| 180 ASSERT_EQ(1u, extension->content_scripts().size()); | 181 ASSERT_EQ(1u, extension->content_scripts().size()); |
| 181 const UserScript& script = extension->content_scripts()[0]; | 182 const UserScript& script = extension->content_scripts()[0]; |
| 182 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location()); | 183 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location()); |
| 183 } | 184 } |
| 184 | 185 |
| 185 TEST(ExtensionFromUserScript, RunAtDocumentIdle) { | 186 TEST(ExtensionFromUserScript, RunAtDocumentIdle) { |
| 186 FilePath test_file; | 187 FilePath test_file; |
| 187 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 188 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
| 188 test_file = test_file.AppendASCII("extensions") | 189 test_file = test_file.AppendASCII("extensions") |
| 189 .AppendASCII("user_script_run_at_idle.user.js"); | 190 .AppendASCII("user_script_run_at_idle.user.js"); |
| 190 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value(); | 191 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value(); |
| 191 | 192 |
| 192 std::string error; | 193 string16 error; |
| 193 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 194 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
| 194 test_file, GURL("http://www.google.com/foo"), &error)); | 195 test_file, GURL("http://www.google.com/foo"), &error)); |
| 195 | 196 |
| 196 ASSERT_TRUE(extension.get()); | 197 ASSERT_TRUE(extension.get()); |
| 197 EXPECT_EQ("", error); | 198 EXPECT_EQ(ASCIIToUTF16(""), error); |
| 198 | 199 |
| 199 // Use a temp dir so that the extensions dir will clean itself up. | 200 // Use a temp dir so that the extensions dir will clean itself up. |
| 200 ScopedTempDir ext_dir; | 201 ScopedTempDir ext_dir; |
| 201 EXPECT_TRUE(ext_dir.Set(extension->path())); | 202 EXPECT_TRUE(ext_dir.Set(extension->path())); |
| 202 | 203 |
| 203 // Validate generated extension metadata. | 204 // Validate generated extension metadata. |
| 204 EXPECT_EQ("Document Idle Test", extension->name()); | 205 EXPECT_EQ("Document Idle Test", extension->name()); |
| 205 EXPECT_EQ("This script tests document-idle", extension->description()); | 206 EXPECT_EQ("This script tests document-idle", extension->description()); |
| 206 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", | 207 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", |
| 207 extension->public_key()); | 208 extension->public_key()); |
| 208 | 209 |
| 209 // Validate run location. | 210 // Validate run location. |
| 210 ASSERT_EQ(1u, extension->content_scripts().size()); | 211 ASSERT_EQ(1u, extension->content_scripts().size()); |
| 211 const UserScript& script = extension->content_scripts()[0]; | 212 const UserScript& script = extension->content_scripts()[0]; |
| 212 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 213 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
| 213 } | 214 } |
| OLD | NEW |