1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/user_style_sheet_watcher.h"
7 #include "base/base64.h"
8 #include "base/basictypes.h"
9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string_util.h"
13 #include "base/threading/thread.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using content::BrowserThread
;
20 TEST(UserStyleSheetWatcherTest
, StyleLoad
) {
21 base::ScopedTempDir dir
;
22 ASSERT_TRUE(dir
.CreateUniqueTempDir());
24 std::string css_file_contents
= "a { color: green; }";
25 base::FilePath style_sheet_file
= dir
.path().AppendASCII("User StyleSheets")
26 .AppendASCII("Custom.css");
27 file_util::CreateDirectory(style_sheet_file
.DirName());
28 ASSERT_TRUE(file_util::WriteFile(style_sheet_file
,
29 css_file_contents
.data(), css_file_contents
.length()));
31 base::MessageLoop
loop(base::MessageLoop::TYPE_UI
);
32 base::Thread
io_thread("UserStyleSheetWatcherTestIOThread");
33 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
34 ASSERT_TRUE(io_thread
.StartWithOptions(options
));
35 content::TestBrowserThread
browser_ui_thread(BrowserThread::UI
, &loop
);
36 content::TestBrowserThread
browser_file_thread(BrowserThread::FILE,
37 io_thread
.message_loop());
39 // It is important that the creation of |style_sheet_watcher| occur after the
40 // creation of |browser_ui_thread| because UserStyleSheetWatchers are
41 // restricted to being deleted only on UI browser threads.
42 scoped_refptr
<UserStyleSheetWatcher
> style_sheet_watcher(
43 new UserStyleSheetWatcher(NULL
, dir
.path()));
44 style_sheet_watcher
->Init();
49 GURL result_url
= style_sheet_watcher
->user_style_sheet();
50 std::string result
= result_url
.spec();
51 std::string prefix
= "data:text/css;charset=utf-8;base64,";
52 ASSERT_TRUE(StartsWithASCII(result
, prefix
, true));
53 result
= result
.substr(prefix
.length());
55 ASSERT_TRUE(base::Base64Decode(result
, &decoded
));
56 ASSERT_EQ(css_file_contents
, decoded
);