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 "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_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/memory/scoped_temp_dir.h" |
14 #include "chrome/test/testing_profile.h" | 15 #include "chrome/test/testing_profile.h" |
15 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
16 #include "content/common/notification_registrar.h" | 17 #include "content/common/notification_registrar.h" |
17 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 // Test bringing up a master on a specific directory, putting a script | 21 // Test bringing up a master on a specific directory, putting a script |
21 // in there, etc. | 22 // in there, etc. |
22 | 23 |
23 class UserScriptMasterTest : public testing::Test, | 24 class UserScriptMasterTest : public testing::Test, |
24 public NotificationObserver { | 25 public NotificationObserver { |
25 public: | 26 public: |
26 UserScriptMasterTest() | 27 UserScriptMasterTest() |
27 : message_loop_(MessageLoop::TYPE_UI), | 28 : message_loop_(MessageLoop::TYPE_UI), |
28 shared_memory_(NULL) { | 29 shared_memory_(NULL) { |
29 } | 30 } |
30 | 31 |
31 virtual void SetUp() { | 32 virtual void SetUp() { |
32 // Name a subdirectory of the temp directory. | 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
33 FilePath tmp_dir; | |
34 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir)); | |
35 script_dir_ = tmp_dir.AppendASCII("UserScriptTest"); | |
36 | |
37 // Create a fresh, empty copy of this directory. | |
38 file_util::Delete(script_dir_, true); | |
39 file_util::CreateDirectory(script_dir_); | |
40 | 34 |
41 // Register for all user script notifications. | 35 // Register for all user script notifications. |
42 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, | 36 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, |
43 NotificationService::AllSources()); | 37 NotificationService::AllSources()); |
44 | 38 |
45 // UserScriptMaster posts tasks to the file thread so make the current | 39 // UserScriptMaster posts tasks to the file thread so make the current |
46 // thread look like one. | 40 // thread look like one. |
47 file_thread_.reset(new BrowserThread( | 41 file_thread_.reset(new BrowserThread( |
48 BrowserThread::FILE, MessageLoop::current())); | 42 BrowserThread::FILE, MessageLoop::current())); |
49 } | 43 } |
50 | 44 |
51 virtual void TearDown() { | 45 virtual void TearDown() { |
52 // Clean up test directory. | |
53 ASSERT_TRUE(file_util::Delete(script_dir_, true)); | |
54 ASSERT_FALSE(file_util::PathExists(script_dir_)); | |
55 file_thread_.reset(); | 46 file_thread_.reset(); |
56 } | 47 } |
57 | 48 |
58 virtual void Observe(NotificationType type, | 49 virtual void Observe(NotificationType type, |
59 const NotificationSource& source, | 50 const NotificationSource& source, |
60 const NotificationDetails& details) { | 51 const NotificationDetails& details) { |
61 DCHECK(type == NotificationType::USER_SCRIPTS_UPDATED); | 52 DCHECK(type == NotificationType::USER_SCRIPTS_UPDATED); |
62 | 53 |
63 shared_memory_ = Details<base::SharedMemory>(details).ptr(); | 54 shared_memory_ = Details<base::SharedMemory>(details).ptr(); |
64 if (MessageLoop::current() == &message_loop_) | 55 if (MessageLoop::current() == &message_loop_) |
65 MessageLoop::current()->Quit(); | 56 MessageLoop::current()->Quit(); |
66 } | 57 } |
67 | 58 |
| 59 // Directory containing user scripts. |
| 60 ScopedTempDir temp_dir_; |
| 61 |
68 NotificationRegistrar registrar_; | 62 NotificationRegistrar registrar_; |
69 | 63 |
70 // MessageLoop used in tests. | 64 // MessageLoop used in tests. |
71 MessageLoop message_loop_; | 65 MessageLoop message_loop_; |
72 | 66 |
73 scoped_ptr<BrowserThread> file_thread_; | 67 scoped_ptr<BrowserThread> file_thread_; |
74 | 68 |
75 // Directory containing user scripts. | |
76 FilePath script_dir_; | |
77 | |
78 // Updated to the script shared memory when we get notified. | 69 // Updated to the script shared memory when we get notified. |
79 base::SharedMemory* shared_memory_; | 70 base::SharedMemory* shared_memory_; |
80 }; | 71 }; |
81 | 72 |
82 // Test that we get notified even when there are no scripts. | 73 // Test that we get notified even when there are no scripts. |
83 TEST_F(UserScriptMasterTest, NoScripts) { | 74 TEST_F(UserScriptMasterTest, NoScripts) { |
84 TestingProfile profile; | 75 TestingProfile profile; |
85 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_, | 76 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(temp_dir_.path(), |
86 &profile)); | 77 &profile)); |
87 master->StartScan(); | 78 master->StartScan(); |
88 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 79 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
89 message_loop_.Run(); | 80 message_loop_.Run(); |
90 | 81 |
91 ASSERT_TRUE(shared_memory_ != NULL); | 82 ASSERT_TRUE(shared_memory_ != NULL); |
92 } | 83 } |
93 | 84 |
94 // Test that we get notified about scripts if they're already in the test dir. | 85 // Test that we get notified about scripts if they're already in the test dir. |
95 TEST_F(UserScriptMasterTest, ExistingScripts) { | 86 TEST_F(UserScriptMasterTest, ExistingScripts) { |
96 TestingProfile profile; | 87 TestingProfile profile; |
97 FilePath path = script_dir_.AppendASCII("script.user.js"); | 88 FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
98 | 89 |
99 const char content[] = "some content"; | 90 const char content[] = "some content"; |
100 size_t written = file_util::WriteFile(path, content, sizeof(content)); | 91 size_t written = file_util::WriteFile(path, content, sizeof(content)); |
101 ASSERT_EQ(written, sizeof(content)); | 92 ASSERT_EQ(written, sizeof(content)); |
102 | 93 |
103 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_, | 94 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(temp_dir_.path(), |
104 &profile)); | 95 &profile)); |
105 master->StartScan(); | 96 master->StartScan(); |
106 | 97 |
107 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 98 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
108 message_loop_.Run(); | 99 message_loop_.Run(); |
109 | 100 |
110 ASSERT_TRUE(shared_memory_ != NULL); | 101 ASSERT_TRUE(shared_memory_ != NULL); |
111 } | 102 } |
112 | 103 |
113 TEST_F(UserScriptMasterTest, Parse1) { | 104 TEST_F(UserScriptMasterTest, Parse1) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 206 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
216 text, &script)); | 207 text, &script)); |
217 ASSERT_EQ("hello", script.name()); | 208 ASSERT_EQ("hello", script.name()); |
218 ASSERT_EQ("wiggity woo", script.description()); | 209 ASSERT_EQ("wiggity woo", script.description()); |
219 ASSERT_EQ(1U, script.url_patterns().size()); | 210 ASSERT_EQ(1U, script.url_patterns().size()); |
220 EXPECT_EQ("http://mail.yahoo.com/*", | 211 EXPECT_EQ("http://mail.yahoo.com/*", |
221 script.url_patterns()[0].GetAsString()); | 212 script.url_patterns()[0].GetAsString()); |
222 } | 213 } |
223 | 214 |
224 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { | 215 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { |
225 FilePath path = script_dir_.AppendASCII("script.user.js"); | 216 FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
226 | 217 |
227 const std::string content( | 218 const std::string content( |
228 "\xEF\xBB\xBF// ==UserScript==\n" | 219 "\xEF\xBB\xBF// ==UserScript==\n" |
229 "// @match http://*.mail.google.com/*\n" | 220 "// @match http://*.mail.google.com/*\n" |
230 "// ==/UserScript==\n"); | 221 "// ==/UserScript==\n"); |
231 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); | 222 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); |
232 ASSERT_EQ(written, content.size()); | 223 ASSERT_EQ(written, content.size()); |
233 | 224 |
234 UserScriptList script_list; | 225 UserScriptList script_list; |
235 UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( | 226 UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( |
236 script_dir_, &script_list); | 227 temp_dir_.path(), &script_list); |
237 ASSERT_EQ(1U, script_list.size()); | 228 ASSERT_EQ(1U, script_list.size()); |
238 | 229 |
239 EXPECT_EQ(content.substr(3), | 230 EXPECT_EQ(content.substr(3), |
240 script_list[0].js_scripts()[0].GetContent().as_string()); | 231 script_list[0].js_scripts()[0].GetContent().as_string()); |
241 EXPECT_EQ("http://*.mail.google.com/*", | 232 EXPECT_EQ("http://*.mail.google.com/*", |
242 script_list[0].url_patterns()[0].GetAsString()); | 233 script_list[0].url_patterns()[0].GetAsString()); |
243 } | 234 } |
244 | 235 |
245 TEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) { | 236 TEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) { |
246 FilePath path = script_dir_.AppendASCII("script.user.js"); | 237 FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
247 | 238 |
248 const std::string content( | 239 const std::string content( |
249 "// ==UserScript==\n" | 240 "// ==UserScript==\n" |
250 "// @match http://*.mail.google.com/*\n" | 241 "// @match http://*.mail.google.com/*\n" |
251 "// ==/UserScript==\n" | 242 "// ==/UserScript==\n" |
252 "// @bom \xEF\xBB\xBF"); | 243 "// @bom \xEF\xBB\xBF"); |
253 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); | 244 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); |
254 ASSERT_EQ(written, content.size()); | 245 ASSERT_EQ(written, content.size()); |
255 | 246 |
256 UserScriptList script_list; | 247 UserScriptList script_list; |
257 UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( | 248 UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( |
258 script_dir_, &script_list); | 249 temp_dir_.path(), &script_list); |
259 ASSERT_EQ(1U, script_list.size()); | 250 ASSERT_EQ(1U, script_list.size()); |
260 | 251 |
261 EXPECT_EQ(content, script_list[0].js_scripts()[0].GetContent().as_string()); | 252 EXPECT_EQ(content, script_list[0].js_scripts()[0].GetContent().as_string()); |
262 EXPECT_EQ("http://*.mail.google.com/*", | 253 EXPECT_EQ("http://*.mail.google.com/*", |
263 script_list[0].url_patterns()[0].GetAsString()); | 254 script_list[0].url_patterns()[0].GetAsString()); |
264 } | 255 } |
OLD | NEW |