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.
4
5 #include "chrome/browser/user_style_sheet_watcher.h"
6
7 #include "base/basictypes.h"
8 #include "base/base64.h"
9 #include "base/file_util.h"
10 #include "base/memory/scoped_temp_dir.h"
11 #include "base/message_loop.h"
12 #include "base/string_util.h"
13 #include "base/threading/thread.h"
14 #include "content/browser/browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
TEST(UserStyleSheetWatcherTest,StyleLoad)17 TEST(UserStyleSheetWatcherTest, StyleLoad) {
18 ScopedTempDir dir;
19 ASSERT_TRUE(dir.CreateUniqueTempDir());
20
21 std::string css_file_contents = "a { color: green; }";
22 FilePath style_sheet_file = dir.path().AppendASCII("User StyleSheets")
23 .AppendASCII("Custom.css");
24 file_util::CreateDirectory(style_sheet_file.DirName());
25 ASSERT_TRUE(file_util::WriteFile(style_sheet_file,
26 css_file_contents.data(), css_file_contents.length()));
27
28 MessageLoop loop(MessageLoop::TYPE_UI);
29 base::Thread io_thread("UserStyleSheetWatcherTestIOThread");
30 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
31 ASSERT_TRUE(io_thread.StartWithOptions(options));
32 BrowserThread browser_ui_thread(BrowserThread::UI, &loop);
33 BrowserThread browser_file_thread(BrowserThread::FILE,
34 io_thread.message_loop());
35
36 // It is important that the creation of |style_sheet_watcher| occur after the
37 // creation of |browser_ui_thread| because UserStyleSheetWatchers are
38 // restricted to being deleted only on UI browser threads.
39 scoped_refptr<UserStyleSheetWatcher> style_sheet_watcher(
40 new UserStyleSheetWatcher(dir.path()));
41 style_sheet_watcher->Init();
42
43 io_thread.Stop();
44 loop.RunAllPending();
45
46 GURL result_url = style_sheet_watcher->user_style_sheet();
47 std::string result = result_url.spec();
48 std::string prefix = "data:text/css;charset=utf-8;base64,";
49 ASSERT_TRUE(StartsWithASCII(result, prefix, true));
50 result = result.substr(prefix.length());
51 std::string decoded;
52 ASSERT_TRUE(base::Base64Decode(result, &decoded));
53 ASSERT_EQ(css_file_contents, decoded);
54 }
55