OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/webui/theme_source.h" | 12 #include "chrome/browser/ui/webui/theme_source.h" |
13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
14 #include "chrome/grit/theme_resources.h" | 14 #include "chrome/grit/theme_resources.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
16 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using content::BrowserThread; | |
20 | |
21 class WebUISourcesTest : public testing::Test { | 19 class WebUISourcesTest : public testing::Test { |
22 public: | 20 public: |
23 WebUISourcesTest() | 21 WebUISourcesTest() : result_data_size_(0) {} |
24 : result_data_size_(0), | |
25 ui_thread_(BrowserThread::UI, base::MessageLoop::current()) {} | |
26 | 22 |
27 TestingProfile* profile() const { return profile_.get(); } | 23 TestingProfile* profile() const { return profile_.get(); } |
28 ThemeSource* theme_source() const { return theme_source_.get(); } | 24 ThemeSource* theme_source() const { return theme_source_.get(); } |
29 size_t result_data_size() const { return result_data_size_; } | 25 size_t result_data_size() const { return result_data_size_; } |
30 | 26 |
31 void StartDataRequest(const std::string& source) { | 27 void StartDataRequest(const std::string& source) { |
32 theme_source()->StartDataRequest( | 28 theme_source()->StartDataRequest( |
33 source, | 29 source, |
34 content::ResourceRequestInfo::WebContentsGetter(), | 30 content::ResourceRequestInfo::WebContentsGetter(), |
35 callback_); | 31 callback_); |
(...skipping 13 matching lines...) Expand all Loading... |
49 theme_source_.reset(); | 45 theme_source_.reset(); |
50 profile_.reset(); | 46 profile_.reset(); |
51 } | 47 } |
52 | 48 |
53 void SendResponse(scoped_refptr<base::RefCountedMemory> data) { | 49 void SendResponse(scoped_refptr<base::RefCountedMemory> data) { |
54 result_data_size_ = data ? data->size() : 0; | 50 result_data_size_ = data ? data->size() : 0; |
55 } | 51 } |
56 | 52 |
57 content::URLDataSource::GotDataCallback callback_; | 53 content::URLDataSource::GotDataCallback callback_; |
58 | 54 |
59 base::MessageLoop loop_; | 55 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
60 content::TestBrowserThread ui_thread_; | |
61 | 56 |
62 std::unique_ptr<TestingProfile> profile_; | 57 std::unique_ptr<TestingProfile> profile_; |
63 std::unique_ptr<ThemeSource> theme_source_; | 58 std::unique_ptr<ThemeSource> theme_source_; |
64 }; | 59 }; |
65 | 60 |
66 TEST_F(WebUISourcesTest, ThemeSourceMimeTypes) { | 61 TEST_F(WebUISourcesTest, ThemeSourceMimeTypes) { |
67 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css"), "text/css"); | 62 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css"), "text/css"); |
68 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css?foo"), | 63 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css?foo"), |
69 "text/css"); | 64 "text/css"); |
70 EXPECT_EQ(theme_source()->GetMimeType("WRONGURL"), "image/png"); | 65 EXPECT_EQ(theme_source()->GetMimeType("WRONGURL"), "image/png"); |
71 } | 66 } |
72 | 67 |
73 TEST_F(WebUISourcesTest, ThemeSourceImages) { | 68 TEST_F(WebUISourcesTest, ThemeSourceImages) { |
74 // We used to PNGEncode the images ourselves, but encoder differences | 69 // We used to PNGEncode the images ourselves, but encoder differences |
75 // invalidated that. We now just check that the image exists. | 70 // invalidated that. We now just check that the image exists. |
76 StartDataRequest("IDR_THEME_FRAME_INCOGNITO"); | 71 StartDataRequest("IDR_THEME_FRAME_INCOGNITO"); |
77 size_t min = 0; | 72 size_t min = 0; |
78 EXPECT_GT(result_data_size_, min); | 73 EXPECT_GT(result_data_size_, min); |
79 | 74 |
80 StartDataRequest("IDR_THEME_TOOLBAR"); | 75 StartDataRequest("IDR_THEME_TOOLBAR"); |
81 EXPECT_GT(result_data_size_, min); | 76 EXPECT_GT(result_data_size_, min); |
82 } | 77 } |
83 | 78 |
84 TEST_F(WebUISourcesTest, ThemeSourceCSS) { | 79 TEST_F(WebUISourcesTest, ThemeSourceCSS) { |
85 content::TestBrowserThread io_thread(BrowserThread::IO, | |
86 base::MessageLoop::current()); | |
87 // Generating the test data for the NTP CSS would just involve copying the | 80 // Generating the test data for the NTP CSS would just involve copying the |
88 // method, or being super brittle and hard-coding the result (requiring | 81 // method, or being super brittle and hard-coding the result (requiring |
89 // an update to the unittest every time the CSS template changes), so we | 82 // an update to the unittest every time the CSS template changes), so we |
90 // just check for a successful request and data that is non-null. | 83 // just check for a successful request and data that is non-null. |
91 size_t empty_size = 0; | 84 size_t empty_size = 0; |
92 | 85 |
93 StartDataRequest("css/new_tab_theme.css"); | 86 StartDataRequest("css/new_tab_theme.css"); |
94 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
95 EXPECT_NE(result_data_size_, empty_size); | 88 EXPECT_NE(result_data_size_, empty_size); |
96 | 89 |
97 StartDataRequest("css/new_tab_theme.css?pie"); | 90 StartDataRequest("css/new_tab_theme.css?pie"); |
98 base::RunLoop().RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
99 EXPECT_NE(result_data_size_, empty_size); | 92 EXPECT_NE(result_data_size_, empty_size); |
100 | 93 |
101 #if !DCHECK_IS_ON() | 94 #if !DCHECK_IS_ON() |
102 // Check that we send NULL back when we can't find what we're looking for. | 95 // Check that we send NULL back when we can't find what we're looking for. |
103 StartDataRequest("css/WRONGURL"); | 96 StartDataRequest("css/WRONGURL"); |
104 EXPECT_EQ(result_data_size_, empty_size); | 97 EXPECT_EQ(result_data_size_, empty_size); |
105 #endif | 98 #endif |
106 } | 99 } |
OLD | NEW |