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
3 // found in the LICENSE file.
6 #include "base/memory/ref_counted_memory.h"
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/webui/theme_source.h"
10 #include "chrome/common/url_constants.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "grit/theme_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using content::BrowserThread
;
18 class WebUISourcesTest
: public testing::Test
{
21 : result_data_size_(0),
22 ui_thread_(BrowserThread::UI
, base::MessageLoop::current()) {}
24 TestingProfile
* profile() const { return profile_
.get(); }
25 ThemeSource
* theme_source() const { return theme_source_
.get(); }
26 size_t result_data_size() const { return result_data_size_
; }
28 void StartDataRequest(const std::string
& source
) {
29 theme_source()->StartDataRequest(source
, -1, -1, callback_
);
32 size_t result_data_size_
;
35 void SetUp() override
{
36 profile_
.reset(new TestingProfile());
37 theme_source_
.reset(new ThemeSource(profile_
.get()));
38 callback_
= base::Bind(&WebUISourcesTest::SendResponse
,
39 base::Unretained(this));
42 void TearDown() override
{
43 theme_source_
.reset();
47 void SendResponse(base::RefCountedMemory
* data
) {
48 result_data_size_
= data
? data
->size() : 0;
51 content::URLDataSource::GotDataCallback callback_
;
53 base::MessageLoop loop_
;
54 content::TestBrowserThread ui_thread_
;
56 scoped_ptr
<TestingProfile
> profile_
;
57 scoped_ptr
<ThemeSource
> theme_source_
;
60 TEST_F(WebUISourcesTest
, ThemeSourceMimeTypes
) {
61 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css"), "text/css");
62 EXPECT_EQ(theme_source()->GetMimeType("css/new_tab_theme.css?foo"),
64 EXPECT_EQ(theme_source()->GetMimeType("WRONGURL"), "image/png");
67 TEST_F(WebUISourcesTest
, ThemeSourceImages
) {
68 // We used to PNGEncode the images ourselves, but encoder differences
69 // invalidated that. We now just check that the image exists.
70 StartDataRequest("IDR_THEME_FRAME_INCOGNITO");
72 EXPECT_GT(result_data_size_
, min
);
74 StartDataRequest("IDR_THEME_TOOLBAR");
75 EXPECT_GT(result_data_size_
, min
);
78 TEST_F(WebUISourcesTest
, ThemeSourceCSS
) {
79 content::TestBrowserThread
io_thread(BrowserThread::IO
,
80 base::MessageLoop::current());
81 // Generating the test data for the NTP CSS would just involve copying the
82 // method, or being super brittle and hard-coding the result (requiring
83 // an update to the unittest every time the CSS template changes), so we
84 // just check for a successful request and data that is non-null.
85 size_t empty_size
= 0;
87 StartDataRequest("css/new_tab_theme.css");
88 EXPECT_NE(result_data_size_
, empty_size
);
90 StartDataRequest("css/new_tab_theme.css?pie");
91 EXPECT_NE(result_data_size_
, empty_size
);
93 // Check that we send NULL back when we can't find what we're looking for.
94 StartDataRequest("css/WRONGURL");
95 EXPECT_EQ(result_data_size_
, empty_size
);