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/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 UserScriptMasterTest : public testing::Test, | 19 class UserScriptMasterTest : public testing::Test, |
20 public NotificationObserver { | 20 public NotificationObserver { |
21 public: | 21 public: |
22 UserScriptMasterTest() : 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 FilePath tmp_dir; | 26 FilePath tmp_dir; |
27 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir)); | 27 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir)); |
28 script_dir_ = tmp_dir.Append(FILE_PATH_LITERAL("UserScriptTest")); | 28 script_dir_ = tmp_dir.AppendASCII("UserScriptTest"); |
29 | 29 |
30 // Create a fresh, empty copy of this directory. | 30 // Create a fresh, empty copy of this directory. |
31 file_util::Delete(script_dir_, true); | 31 file_util::Delete(script_dir_, true); |
32 file_util::CreateDirectory(script_dir_); | 32 file_util::CreateDirectory(script_dir_); |
33 | 33 |
34 // Register for all user script notifications. | 34 // Register for all user script notifications. |
35 NotificationService::current()->AddObserver(this, | 35 NotificationService::current()->AddObserver(this, |
36 NOTIFY_USER_SCRIPTS_LOADED, | 36 NOTIFY_USER_SCRIPTS_LOADED, |
37 NotificationService::AllSources()); | 37 NotificationService::AllSources()); |
38 } | 38 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // There were no scripts in the script dir, so we shouldn't have gotten | 80 // There were no scripts in the script dir, so we shouldn't have gotten |
81 // a notification. | 81 // a notification. |
82 ASSERT_EQ(NULL, shared_memory_); | 82 ASSERT_EQ(NULL, shared_memory_); |
83 } | 83 } |
84 | 84 |
85 // Test that we get notified about new scripts after they're added. | 85 // Test that we get notified about new scripts after they're added. |
86 TEST_F(UserScriptMasterTest, NewScripts) { | 86 TEST_F(UserScriptMasterTest, NewScripts) { |
87 scoped_refptr<UserScriptMaster> master( | 87 scoped_refptr<UserScriptMaster> master( |
88 new UserScriptMaster(MessageLoop::current(), script_dir_)); | 88 new UserScriptMaster(MessageLoop::current(), script_dir_)); |
89 | 89 |
90 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); | 90 FilePath path = script_dir_.AppendASCII("script.user.js"); |
91 | 91 |
92 FILE* file = file_util::OpenFile(path, "w"); | 92 FILE* file = file_util::OpenFile(path, "w"); |
93 const char content[] = "some content"; | 93 const char content[] = "some content"; |
94 fwrite(content, 1, arraysize(content), file); | 94 fwrite(content, 1, arraysize(content), file); |
95 file_util::CloseFile(file); | 95 file_util::CloseFile(file); |
96 | 96 |
97 message_loop_.Run(); | 97 message_loop_.Run(); |
98 | 98 |
99 ASSERT_TRUE(shared_memory_ != NULL); | 99 ASSERT_TRUE(shared_memory_ != NULL); |
100 } | 100 } |
101 | 101 |
102 // Test that we get notified about scripts if they're already in the test dir. | 102 // Test that we get notified about scripts if they're already in the test dir. |
103 TEST_F(UserScriptMasterTest, ExistingScripts) { | 103 TEST_F(UserScriptMasterTest, ExistingScripts) { |
104 FilePath path = script_dir_.Append(FILE_PATH_LITERAL("script.user.js")); | 104 FilePath path = script_dir_.AppendASCII("script.user.js"); |
105 | 105 |
106 FILE* file = file_util::OpenFile(path, "w"); | 106 FILE* file = file_util::OpenFile(path, "w"); |
107 const char content[] = "some content"; | 107 const char content[] = "some content"; |
108 fwrite(content, 1, arraysize(content), file); | 108 fwrite(content, 1, arraysize(content), file); |
109 file_util::CloseFile(file); | 109 file_util::CloseFile(file); |
110 | 110 |
111 scoped_refptr<UserScriptMaster> master( | 111 scoped_refptr<UserScriptMaster> master( |
112 new UserScriptMaster(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); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 const std::string text( | 156 const std::string text( |
157 "// ==UserScript==\n" | 157 "// ==UserScript==\n" |
158 "// @include *foo*\n" | 158 "// @include *foo*\n" |
159 "// ==/UserScript=="); // no trailing newline | 159 "// ==/UserScript=="); // no trailing newline |
160 | 160 |
161 std::vector<std::string> includes; | 161 std::vector<std::string> includes; |
162 UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &includes); | 162 UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &includes); |
163 EXPECT_EQ(1U, includes.size()); | 163 EXPECT_EQ(1U, includes.size()); |
164 EXPECT_EQ("*foo*", includes[0]); | 164 EXPECT_EQ("*foo*", includes[0]); |
165 } | 165 } |
OLD | NEW |