1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/user_script_master.h"
#include <fstream>
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/common/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"
// Test bringing up a master on a specific directory, putting a script in there, etc.
class UserScriptMasterTest : public testing::Test,
public NotificationObserver {
public:
UserScriptMasterTest() : shared_memory_(NULL) {}
virtual void SetUp() {
// Name a subdirectory of the temp directory.
FilePath tmp_dir;
ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &tmp_dir));
script_dir_ = tmp_dir.AppendASCII("UserScriptTest");
// Create a fresh, empty copy of this directory.
file_util::Delete(script_dir_, true);
file_util::CreateDirectory(script_dir_);
// Register for all user script notifications.
NotificationService::current()->AddObserver(
this,
NotificationType::USER_SCRIPTS_LOADED,
NotificationService::AllSources());
}
virtual void TearDown() {
NotificationService::current()->RemoveObserver(
this,
NotificationType::USER_SCRIPTS_LOADED,
NotificationService::AllSources());
// Clean up test directory.
ASSERT_TRUE(file_util::Delete(script_dir_, true));
ASSERT_FALSE(file_util::PathExists(script_dir_));
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::USER_SCRIPTS_LOADED);
shared_memory_ = Details<base::SharedMemory>(details).ptr();
if (MessageLoop::current() == &message_loop_)
MessageLoop::current()->Quit();
}
// MessageLoop used in tests.
MessageLoop message_loop_;
// Directory containing user scripts.
FilePath script_dir_;
// Updated to the script shared memory when we get notified.
base::SharedMemory* shared_memory_;
};
// Test that we *don't* get spurious notifications.
TEST_F(UserScriptMasterTest, NoScripts) {
// Set shared_memory_ to something non-NULL, so we can check it became NULL.
shared_memory_ = reinterpret_cast<base::SharedMemory*>(1);
scoped_refptr<UserScriptMaster> master(
new UserScriptMaster(MessageLoop::current(), script_dir_));
master->StartScan();
message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
message_loop_.Run();
// There were no scripts in the script dir, so we shouldn't have gotten
// a notification.
ASSERT_EQ(NULL, shared_memory_);
}
// Test that we get notified about new scripts after they're added.
TEST_F(UserScriptMasterTest, NewScripts) {
scoped_refptr<UserScriptMaster> master(
new UserScriptMaster(MessageLoop::current(), script_dir_));
FilePath path = script_dir_.AppendASCII("script.user.js");
FILE* file = file_util::OpenFile(path, "w");
const char content[] = "some content";
fwrite(content, 1, arraysize(content), file);
file_util::CloseFile(file);
message_loop_.Run();
ASSERT_TRUE(shared_memory_ != NULL);
}
// Test that we get notified about scripts if they're already in the test dir.
TEST_F(UserScriptMasterTest, ExistingScripts) {
FilePath path = script_dir_.AppendASCII("script.user.js");
FILE* file = file_util::OpenFile(path, "w");
const char content[] = "some content";
fwrite(content, 1, arraysize(content), file);
file_util::CloseFile(file);
scoped_refptr<UserScriptMaster> master(
new UserScriptMaster(MessageLoop::current(), script_dir_));
master->StartScan();
message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
message_loop_.Run();
ASSERT_TRUE(shared_memory_ != NULL);
}
TEST_F(UserScriptMasterTest, Parse1) {
const std::string text(
"// This is my awesome script\n"
"// It does stuff.\n"
"// ==UserScript== trailing garbage\n"
"// @name foobar script\n"
"// @namespace http://www.google.com/\n"
"// @include *mail.google.com*\n"
"// \n"
"// @othergarbage\n"
"// @include *mail.yahoo.com*\r\n"
"// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK
"//@include not-recognized\n" // must have one space after "//"
"// ==/UserScript== trailing garbage\n"
"\n"
"\n"
"alert('hoo!');\n");
UserScript script;
UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script);
EXPECT_EQ(3U, script.globs().size());
EXPECT_EQ("*mail.google.com*", script.globs()[0]);
EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]);
EXPECT_EQ("*mail.msn.com*", script.globs()[2]);
}
TEST_F(UserScriptMasterTest, Parse2) {
const std::string text("default to @include *");
UserScript script;
UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script);
EXPECT_EQ(1U, script.globs().size());
EXPECT_EQ("*", script.globs()[0]);
}
TEST_F(UserScriptMasterTest, Parse3) {
const std::string text(
"// ==UserScript==\n"
"// @include *foo*\n"
"// ==/UserScript=="); // no trailing newline
UserScript script;
UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script);
EXPECT_EQ(1U, script.globs().size());
EXPECT_EQ("*foo*", script.globs()[0]);
}
|