OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 <fstream> | 7 #include <fstream> |
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 "chrome/browser/chrome_thread.h" |
14 #include "chrome/common/notification_registrar.h" | 15 #include "chrome/common/notification_registrar.h" |
15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 // Test bringing up a master on a specific directory, putting a script | 19 // Test bringing up a master on a specific directory, putting a script |
19 // in there, etc. | 20 // in there, etc. |
20 | 21 |
21 class UserScriptMasterTest : public testing::Test, | 22 class UserScriptMasterTest : public testing::Test, |
22 public NotificationObserver { | 23 public NotificationObserver { |
23 public: | 24 public: |
24 UserScriptMasterTest() | 25 UserScriptMasterTest() |
25 : message_loop_(MessageLoop::TYPE_UI), | 26 : message_loop_(MessageLoop::TYPE_UI), |
26 shared_memory_(NULL) { | 27 shared_memory_(NULL) { |
27 } | 28 } |
28 | 29 |
29 virtual void SetUp() { | 30 virtual void SetUp() { |
30 // Name a subdirectory of the temp directory. | 31 // Name a subdirectory of the temp directory. |
31 FilePath tmp_dir; | 32 FilePath tmp_dir; |
32 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir)); | 33 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir)); |
33 script_dir_ = tmp_dir.AppendASCII("UserScriptTest"); | 34 script_dir_ = tmp_dir.AppendASCII("UserScriptTest"); |
34 | 35 |
35 // Create a fresh, empty copy of this directory. | 36 // Create a fresh, empty copy of this directory. |
36 file_util::Delete(script_dir_, true); | 37 file_util::Delete(script_dir_, true); |
37 file_util::CreateDirectory(script_dir_); | 38 file_util::CreateDirectory(script_dir_); |
38 | 39 |
39 // Register for all user script notifications. | 40 // Register for all user script notifications. |
40 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, | 41 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, |
41 NotificationService::AllSources()); | 42 NotificationService::AllSources()); |
| 43 |
| 44 // UserScriptMaster posts tasks to the file thread so make the current |
| 45 // thread look like one. |
| 46 file_thread_.reset(new ChromeThread( |
| 47 ChromeThread::FILE, MessageLoop::current())); |
42 } | 48 } |
43 | 49 |
44 virtual void TearDown() { | 50 virtual void TearDown() { |
45 // Clean up test directory. | 51 // Clean up test directory. |
46 ASSERT_TRUE(file_util::Delete(script_dir_, true)); | 52 ASSERT_TRUE(file_util::Delete(script_dir_, true)); |
47 ASSERT_FALSE(file_util::PathExists(script_dir_)); | 53 ASSERT_FALSE(file_util::PathExists(script_dir_)); |
| 54 file_thread_.reset(); |
48 } | 55 } |
49 | 56 |
50 virtual void Observe(NotificationType type, | 57 virtual void Observe(NotificationType type, |
51 const NotificationSource& source, | 58 const NotificationSource& source, |
52 const NotificationDetails& details) { | 59 const NotificationDetails& details) { |
53 DCHECK(type == NotificationType::USER_SCRIPTS_UPDATED); | 60 DCHECK(type == NotificationType::USER_SCRIPTS_UPDATED); |
54 | 61 |
55 shared_memory_ = Details<base::SharedMemory>(details).ptr(); | 62 shared_memory_ = Details<base::SharedMemory>(details).ptr(); |
56 if (MessageLoop::current() == &message_loop_) | 63 if (MessageLoop::current() == &message_loop_) |
57 MessageLoop::current()->Quit(); | 64 MessageLoop::current()->Quit(); |
58 } | 65 } |
59 | 66 |
60 NotificationRegistrar registrar_; | 67 NotificationRegistrar registrar_; |
61 | 68 |
62 // MessageLoop used in tests. | 69 // MessageLoop used in tests. |
63 MessageLoop message_loop_; | 70 MessageLoop message_loop_; |
64 | 71 |
| 72 scoped_ptr<ChromeThread> file_thread_; |
| 73 |
65 // Directory containing user scripts. | 74 // Directory containing user scripts. |
66 FilePath script_dir_; | 75 FilePath script_dir_; |
67 | 76 |
68 // Updated to the script shared memory when we get notified. | 77 // Updated to the script shared memory when we get notified. |
69 base::SharedMemory* shared_memory_; | 78 base::SharedMemory* shared_memory_; |
70 }; | 79 }; |
71 | 80 |
72 // Test that we get notified even when there are no scripts. | 81 // Test that we get notified even when there are no scripts. |
73 TEST_F(UserScriptMasterTest, NoScripts) { | 82 TEST_F(UserScriptMasterTest, NoScripts) { |
74 | 83 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); |
75 scoped_refptr<UserScriptMaster> master( | |
76 new UserScriptMaster(MessageLoop::current(), script_dir_)); | |
77 master->StartScan(); | 84 master->StartScan(); |
78 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 85 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
79 message_loop_.Run(); | 86 message_loop_.Run(); |
80 | 87 |
81 ASSERT_TRUE(shared_memory_ != NULL); | 88 ASSERT_TRUE(shared_memory_ != NULL); |
82 } | 89 } |
83 | 90 |
84 // TODO(shess): Disabled on Linux because of missing DirectoryWatcher. | 91 // TODO(shess): Disabled on Linux because of missing DirectoryWatcher. |
85 #if defined(OS_WIN) || defined(OS_MACOSX) | 92 #if defined(OS_WIN) || defined(OS_MACOSX) |
86 // Test that we get notified about new scripts after they're added. | 93 // Test that we get notified about new scripts after they're added. |
87 TEST_F(UserScriptMasterTest, NewScripts) { | 94 TEST_F(UserScriptMasterTest, NewScripts) { |
88 scoped_refptr<UserScriptMaster> master( | 95 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); |
89 new UserScriptMaster(MessageLoop::current(), script_dir_)); | |
90 | 96 |
91 FilePath path = script_dir_.AppendASCII("script.user.js"); | 97 FilePath path = script_dir_.AppendASCII("script.user.js"); |
92 | 98 |
93 const char content[] = "some content"; | 99 const char content[] = "some content"; |
94 size_t written = file_util::WriteFile(path, content, sizeof(content)); | 100 size_t written = file_util::WriteFile(path, content, sizeof(content)); |
95 ASSERT_EQ(written, sizeof(content)); | 101 ASSERT_EQ(written, sizeof(content)); |
96 | 102 |
97 // Post a delayed task so that we fail rather than hanging if things | 103 // Post a delayed task so that we fail rather than hanging if things |
98 // don't work. | 104 // don't work. |
99 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, 5000); | 105 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, 5000); |
100 | 106 |
101 message_loop_.Run(); | 107 message_loop_.Run(); |
102 | 108 |
103 ASSERT_TRUE(shared_memory_ != NULL); | 109 ASSERT_TRUE(shared_memory_ != NULL); |
104 } | 110 } |
105 #endif | 111 #endif |
106 | 112 |
107 // Test that we get notified about scripts if they're already in the test dir. | 113 // Test that we get notified about scripts if they're already in the test dir. |
108 TEST_F(UserScriptMasterTest, ExistingScripts) { | 114 TEST_F(UserScriptMasterTest, ExistingScripts) { |
109 FilePath path = script_dir_.AppendASCII("script.user.js"); | 115 FilePath path = script_dir_.AppendASCII("script.user.js"); |
110 | 116 |
111 const char content[] = "some content"; | 117 const char content[] = "some content"; |
112 size_t written = file_util::WriteFile(path, content, sizeof(content)); | 118 size_t written = file_util::WriteFile(path, content, sizeof(content)); |
113 ASSERT_EQ(written, sizeof(content)); | 119 ASSERT_EQ(written, sizeof(content)); |
114 | 120 |
115 scoped_refptr<UserScriptMaster> master( | 121 scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); |
116 new UserScriptMaster(MessageLoop::current(), script_dir_)); | |
117 master->StartScan(); | 122 master->StartScan(); |
118 | 123 |
119 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 124 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
120 message_loop_.Run(); | 125 message_loop_.Run(); |
121 | 126 |
122 ASSERT_TRUE(shared_memory_ != NULL); | 127 ASSERT_TRUE(shared_memory_ != NULL); |
123 } | 128 } |
124 | 129 |
125 TEST_F(UserScriptMasterTest, Parse1) { | 130 TEST_F(UserScriptMasterTest, Parse1) { |
126 const std::string text( | 131 const std::string text( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 "// ==UserScript==\n" | 211 "// ==UserScript==\n" |
207 "// @include http://*.mail.google.com/*\n" | 212 "// @include http://*.mail.google.com/*\n" |
208 "// @match \t http://mail.yahoo.com/*\n" | 213 "// @match \t http://mail.yahoo.com/*\n" |
209 "// ==/UserScript==\n"); | 214 "// ==/UserScript==\n"); |
210 | 215 |
211 // Not allowed to mix @include and @value. | 216 // Not allowed to mix @include and @value. |
212 UserScript script; | 217 UserScript script; |
213 EXPECT_FALSE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 218 EXPECT_FALSE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
214 text, &script)); | 219 text, &script)); |
215 } | 220 } |
OLD | NEW |