1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/extensions/user_script_master.h" 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string> 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_path.h" 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_util.h" 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/message_loop.h" 12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/path_service.h" 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/string_util.h" 14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_temp_dir.h" 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/test/testing_profile.h" 16dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/browser_thread.h" 17ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_registrar.h" 18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_service.h" 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "testing/gtest/include/gtest/gtest.h" 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Test bringing up a master on a specific directory, putting a script 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// in there, etc. 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass UserScriptMasterTest : public testing::Test, 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public NotificationObserver { 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptMasterTest() 28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch : message_loop_(MessageLoop::TYPE_UI), 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch shared_memory_(NULL) { 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void SetUp() { 33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Register for all user script notifications. 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch NotificationService::AllSources()); 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // UserScriptMaster posts tasks to the file thread so make the current 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // thread look like one. 41731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick file_thread_.reset(new BrowserThread( 42731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick BrowserThread::FILE, MessageLoop::current())); 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void TearDown() { 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch file_thread_.reset(); 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void Observe(NotificationType type, 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const NotificationSource& source, 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const NotificationDetails& details) { 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DCHECK(type == NotificationType::USER_SCRIPTS_UPDATED); 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch shared_memory_ = Details<base::SharedMemory>(details).ptr(); 55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (MessageLoop::current() == &message_loop_) 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch MessageLoop::current()->Quit(); 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // Directory containing user scripts. 60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ScopedTempDir temp_dir_; 61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch NotificationRegistrar registrar_; 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // MessageLoop used in tests. 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch MessageLoop message_loop_; 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 67731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick scoped_ptr<BrowserThread> file_thread_; 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Updated to the script shared memory when we get notified. 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::SharedMemory* shared_memory_; 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Test that we get notified even when there are no scripts. 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, NoScripts) { 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch TestingProfile profile; 76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen scoped_refptr<UserScriptMaster> master(new UserScriptMaster(temp_dir_.path(), 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch &profile)); 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch master->StartScan(); 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch message_loop_.Run(); 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(shared_memory_ != NULL); 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Test that we get notified about scripts if they're already in the test dir. 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, ExistingScripts) { 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch TestingProfile profile; 88ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen FilePath path = temp_dir_.path().AppendASCII("script.user.js"); 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const char content[] = "some content"; 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t written = file_util::WriteFile(path, content, sizeof(content)); 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(written, sizeof(content)); 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 94ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen scoped_refptr<UserScriptMaster> master(new UserScriptMaster(temp_dir_.path(), 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch &profile)); 96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch master->StartScan(); 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch message_loop_.Run(); 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(shared_memory_ != NULL); 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse1) { 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// This is my awesome script\n" 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// It does stuff.\n" 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript== trailing garbage\n" 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @name foobar script\n" 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @namespace http://www.google.com/\n" 111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @include *mail.google.com*\n" 112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// \n" 113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @othergarbage\n" 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @include *mail.yahoo.com*\r\n" 115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK 116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "//@include not-recognized\n" // must have one space after "//" 117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript== trailing garbage\n" 118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "\n" 119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "\n" 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "alert('hoo!');\n"); 121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(3U, script.globs().size()); 126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("*mail.google.com*", script.globs()[0]); 127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); 128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("*mail.msn.com*", script.globs()[2]); 129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 131c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse2) { 132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text("default to @include *"); 133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(1U, script.globs().size()); 138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("*", script.globs()[0]); 139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 141c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse3) { 142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @include *foo*\n" 145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript=="); // no trailing newline 146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script); 149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(1U, script.globs().size()); 150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("*foo*", script.globs()[0]); 151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 153c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse4) { 154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match http://*.mail.google.com/*\n" 157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match \t http://mail.yahoo.com/*\n" 158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n"); 159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(0U, script.globs().size()); 164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(2U, script.url_patterns().size()); 165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("http://*.mail.google.com/*", 166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script.url_patterns()[0].GetAsString()); 167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("http://mail.yahoo.com/*", 168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script.url_patterns()[1].GetAsString()); 169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 171c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse5) { 172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match http://*mail.google.com/*\n" 175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n"); 176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Invalid @match value. 178c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 179c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_FALSE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 181c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 182c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 183c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse6) { 184c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 185c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 186c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @include http://*.mail.google.com/*\n" 187c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match \t http://mail.yahoo.com/*\n" 188c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n"); 189c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 190c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Allowed to match @include and @match. 191c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 192c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 193c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 194c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 195c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 196c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, Parse7) { 197c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Greasemonkey allows there to be any leading text before the comment marker. 198c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string text( 199c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 200c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "adsasdfasf// @name hello\n" 201c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch " // @description\twiggity woo\n" 202c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "\t// @match \t http://mail.yahoo.com/*\n" 203c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n"); 204c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 205c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScript script; 206c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( 207c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch text, &script)); 208c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ("hello", script.name()); 209c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ("wiggity woo", script.description()); 210c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(1U, script.url_patterns().size()); 211c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("http://mail.yahoo.com/*", 212c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script.url_patterns()[0].GetAsString()); 213c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 214c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 215c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { 216ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen FilePath path = temp_dir_.path().AppendASCII("script.user.js"); 217c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 218c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string content( 219c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "\xEF\xBB\xBF// ==UserScript==\n" 220c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match http://*.mail.google.com/*\n" 221c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n"); 222c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t written = file_util::WriteFile(path, content.c_str(), content.size()); 223c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(written, content.size()); 224c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 225c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptList script_list; 226c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( 227ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen temp_dir_.path(), &script_list); 228c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(1U, script_list.size()); 229c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 230c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(content.substr(3), 231c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script_list[0].js_scripts()[0].GetContent().as_string()); 232c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("http://*.mail.google.com/*", 233c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script_list[0].url_patterns()[0].GetAsString()); 234c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 235c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 236c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(UserScriptMasterTest, LeaveBOMNotAtTheBeginning) { 237ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen FilePath path = temp_dir_.path().AppendASCII("script.user.js"); 238c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 239c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string content( 240c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==UserScript==\n" 241c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @match http://*.mail.google.com/*\n" 242c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// ==/UserScript==\n" 243c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch "// @bom \xEF\xBB\xBF"); 244c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t written = file_util::WriteFile(path, content.c_str(), content.size()); 245c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(written, content.size()); 246c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 247c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptList script_list; 248c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( 249ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen temp_dir_.path(), &script_list); 250c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(1U, script_list.size()); 251c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 252c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(content, script_list[0].js_scripts()[0].GetContent().as_string()); 253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ("http://*.mail.google.com/*", 254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch script_list[0].url_patterns()[0].GetAsString()); 255c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 256