OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/user_style_sheet_watcher.h" | 5 #include "chrome/browser/user_style_sheet_watcher.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "base/base64.h" | 8 #include "base/base64.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/threading/thread.h" |
12 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 TEST(UserStyleSheetWatcherTest, StyleLoad) { | 17 TEST(UserStyleSheetWatcherTest, StyleLoad) { |
16 ScopedTempDir dir; | 18 ScopedTempDir dir; |
17 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 19 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
18 | 20 |
19 std::string css_file_contents = "a { color: green; }"; | 21 std::string css_file_contents = "a { color: green; }"; |
20 FilePath style_sheet_file = dir.path().AppendASCII("User StyleSheets") | 22 FilePath style_sheet_file = dir.path().AppendASCII("User StyleSheets") |
21 .AppendASCII("Custom.css"); | 23 .AppendASCII("Custom.css"); |
22 file_util::CreateDirectory(style_sheet_file.DirName()); | 24 file_util::CreateDirectory(style_sheet_file.DirName()); |
23 ASSERT_TRUE(file_util::WriteFile(style_sheet_file, | 25 ASSERT_TRUE(file_util::WriteFile(style_sheet_file, |
24 css_file_contents.data(), css_file_contents.length())); | 26 css_file_contents.data(), css_file_contents.length())); |
25 | 27 |
26 scoped_refptr<UserStyleSheetWatcher> style_sheet_watcher( | 28 scoped_refptr<UserStyleSheetWatcher> style_sheet_watcher( |
27 new UserStyleSheetWatcher(dir.path())); | 29 new UserStyleSheetWatcher(dir.path())); |
28 MessageLoop loop; | 30 |
29 BrowserThread ui_thread(BrowserThread::UI, &loop); | 31 MessageLoop loop(MessageLoop::TYPE_UI); |
30 BrowserThread file_thread(BrowserThread::FILE, &loop); | 32 base::Thread io_thread("UserStyleSheetWatcherTestIOThread"); |
| 33 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 34 io_thread.StartWithOptions(options); |
| 35 BrowserThread browser_ui_thread(BrowserThread::UI, &loop); |
| 36 BrowserThread browser_file_thread(BrowserThread::FILE, |
| 37 io_thread.message_loop()); |
31 style_sheet_watcher->Init(); | 38 style_sheet_watcher->Init(); |
32 | 39 |
| 40 io_thread.Stop(); |
33 loop.RunAllPending(); | 41 loop.RunAllPending(); |
34 | 42 |
35 GURL result_url = style_sheet_watcher->user_style_sheet(); | 43 GURL result_url = style_sheet_watcher->user_style_sheet(); |
36 std::string result = result_url.spec(); | 44 std::string result = result_url.spec(); |
37 std::string prefix = "data:text/css;charset=utf-8;base64,"; | 45 std::string prefix = "data:text/css;charset=utf-8;base64,"; |
38 EXPECT_TRUE(StartsWithASCII(result, prefix, true)); | 46 ASSERT_TRUE(StartsWithASCII(result, prefix, true)); |
39 result = result.substr(prefix.length()); | 47 result = result.substr(prefix.length()); |
40 std::string decoded; | 48 std::string decoded; |
41 EXPECT_TRUE(base::Base64Decode(result, &decoded)); | 49 ASSERT_TRUE(base::Base64Decode(result, &decoded)); |
42 EXPECT_EQ(css_file_contents, decoded); | 50 ASSERT_EQ(css_file_contents, decoded); |
43 style_sheet_watcher = NULL; | |
44 } | 51 } |
OLD | NEW |