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/greasemonkey_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/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 // Test bringing up a master on a specific directory, putting a script in there,
etc. | 17 // Test bringing up a master on a specific directory, putting a script in there,
etc. |
18 | 18 |
19 class GreasemonkeyMasterTest : public testing::Test, | 19 class UserScriptMasterTest : public testing::Test, |
20 public NotificationObserver { | 20 public NotificationObserver { |
21 public: | 21 public: |
22 GreasemonkeyMasterTest() : shared_memory_(NULL) {} | 22 UserScriptMasterTest() : shared_memory_(NULL) {} |
23 | 23 |
24 virtual void SetUp() { | 24 virtual void SetUp() { |
25 // Name a subdirectory of the temp directory. | 25 // Name a subdirectory of the temp directory. |
26 std::wstring path_str; | 26 std::wstring path_str; |
27 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_str)); | 27 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_str)); |
28 script_dir_ = FilePath(path_str).Append( | 28 script_dir_ = FilePath(path_str).Append( |
29 FILE_PATH_LITERAL("GreasemonkeyTest")); | 29 FILE_PATH_LITERAL("UserScriptTest")); |
30 | 30 |
31 // Create a fresh, empty copy of this directory. | 31 // Create a fresh, empty copy of this directory. |
32 file_util::Delete(script_dir_.value(), true); | 32 file_util::Delete(script_dir_.value(), true); |
33 file_util::CreateDirectory(script_dir_.value()); | 33 file_util::CreateDirectory(script_dir_.value()); |
34 | 34 |
35 // Register for all user script notifications. | 35 // Register for all user script notifications. |
36 NotificationService::current()->AddObserver(this, | 36 NotificationService::current()->AddObserver(this, |
37 NOTIFY_GREASEMONKEY_SCRIPTS_LOADED, | 37 NOTIFY_USER_SCRIPTS_LOADED, |
38 NotificationService::AllSources()); | 38 NotificationService::AllSources()); |
39 } | 39 } |
40 | 40 |
41 virtual void TearDown() { | 41 virtual void TearDown() { |
42 NotificationService::current()->RemoveObserver(this, | 42 NotificationService::current()->RemoveObserver(this, |
43 NOTIFY_GREASEMONKEY_SCRIPTS_LOADED, | 43 NOTIFY_USER_SCRIPTS_LOADED, |
44 NotificationService::AllSources()); | 44 NotificationService::AllSources()); |
45 | 45 |
46 // Clean up test directory. | 46 // Clean up test directory. |
47 ASSERT_TRUE(file_util::Delete(script_dir_.value(), true)); | 47 ASSERT_TRUE(file_util::Delete(script_dir_.value(), true)); |
48 ASSERT_FALSE(file_util::PathExists(script_dir_.value())); | 48 ASSERT_FALSE(file_util::PathExists(script_dir_.value())); |
49 } | 49 } |
50 | 50 |
51 virtual void Observe(NotificationType type, | 51 virtual void Observe(NotificationType type, |
52 const NotificationSource& source, | 52 const NotificationSource& source, |
53 const NotificationDetails& details) { | 53 const NotificationDetails& details) { |
54 DCHECK(type == NOTIFY_GREASEMONKEY_SCRIPTS_LOADED); | 54 DCHECK(type == NOTIFY_USER_SCRIPTS_LOADED); |
55 | 55 |
56 shared_memory_ = Details<base::SharedMemory>(details).ptr(); | 56 shared_memory_ = Details<base::SharedMemory>(details).ptr(); |
57 if (MessageLoop::current() == &message_loop_) | 57 if (MessageLoop::current() == &message_loop_) |
58 MessageLoop::current()->Quit(); | 58 MessageLoop::current()->Quit(); |
59 } | 59 } |
60 | 60 |
61 // MessageLoop used in tests. | 61 // MessageLoop used in tests. |
62 MessageLoop message_loop_; | 62 MessageLoop message_loop_; |
63 | 63 |
64 // Directory containing user scripts. | 64 // Directory containing user scripts. |
65 FilePath script_dir_; | 65 FilePath script_dir_; |
66 | 66 |
67 // Updated to the script shared memory when we get notified. | 67 // Updated to the script shared memory when we get notified. |
68 base::SharedMemory* shared_memory_; | 68 base::SharedMemory* shared_memory_; |
69 }; | 69 }; |
70 | 70 |
71 // Test that we *don't* get spurious notifications. | 71 // Test that we *don't* get spurious notifications. |
72 TEST_F(GreasemonkeyMasterTest, NoScripts) { | 72 TEST_F(UserScriptMasterTest, NoScripts) { |
73 // Set shared_memory_ to something non-NULL, so we can check it became NULL. | 73 // Set shared_memory_ to something non-NULL, so we can check it became NULL. |
74 shared_memory_ = reinterpret_cast<base::SharedMemory*>(1); | 74 shared_memory_ = reinterpret_cast<base::SharedMemory*>(1); |
75 | 75 |
76 scoped_refptr<GreasemonkeyMaster> master( | 76 scoped_refptr<UserScriptMaster> master( |
77 new GreasemonkeyMaster(MessageLoop::current(), script_dir_)); | 77 new UserScriptMaster(MessageLoop::current(), script_dir_)); |
78 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 78 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
79 message_loop_.Run(); | 79 message_loop_.Run(); |
80 | 80 |
81 // There were no scripts in the script dir, so we shouldn't have gotten | 81 // There were no scripts in the script dir, so we shouldn't have gotten |
82 // a notification. | 82 // a notification. |
83 ASSERT_EQ(NULL, shared_memory_); | 83 ASSERT_EQ(NULL, shared_memory_); |
84 } | 84 } |
85 | 85 |
86 // Test that we get notified about new scripts after they're added. | 86 // Test that we get notified about new scripts after they're added. |
87 TEST_F(GreasemonkeyMasterTest, NewScripts) { | 87 TEST_F(UserScriptMasterTest, NewScripts) { |
88 scoped_refptr<GreasemonkeyMaster> master( | 88 scoped_refptr<UserScriptMaster> master( |
89 new GreasemonkeyMaster(MessageLoop::current(), script_dir_)); | 89 new UserScriptMaster(MessageLoop::current(), script_dir_)); |
90 | 90 |
91 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); | 91 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); |
92 | 92 |
93 std::ofstream file; | 93 std::ofstream file; |
94 file.open(WideToUTF8(path.value()).c_str()); | 94 file.open(WideToUTF8(path.value()).c_str()); |
95 file << "some content"; | 95 file << "some content"; |
96 file.close(); | 96 file.close(); |
97 | 97 |
98 message_loop_.Run(); | 98 message_loop_.Run(); |
99 | 99 |
100 ASSERT_TRUE(shared_memory_ != NULL); | 100 ASSERT_TRUE(shared_memory_ != NULL); |
101 } | 101 } |
102 | 102 |
103 // Test that we get notified about scripts if they're already in the test dir. | 103 // Test that we get notified about scripts if they're already in the test dir. |
104 TEST_F(GreasemonkeyMasterTest, ExistingScripts) { | 104 TEST_F(UserScriptMasterTest, ExistingScripts) { |
105 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); | 105 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); |
106 std::ofstream file; | 106 std::ofstream file; |
107 file.open(WideToUTF8(path.value()).c_str()); | 107 file.open(WideToUTF8(path.value()).c_str()); |
108 file << "some content"; | 108 file << "some content"; |
109 file.close(); | 109 file.close(); |
110 | 110 |
111 scoped_refptr<GreasemonkeyMaster> master( | 111 scoped_refptr<UserScriptMaster> master( |
112 new GreasemonkeyMaster(MessageLoop::current(), script_dir_)); | 112 new UserScriptMaster(MessageLoop::current(), script_dir_)); |
113 | 113 |
114 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); | 114 message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
115 message_loop_.Run(); | 115 message_loop_.Run(); |
116 | 116 |
117 ASSERT_TRUE(shared_memory_ != NULL); | 117 ASSERT_TRUE(shared_memory_ != NULL); |
118 } | 118 } |
OLD | NEW |