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. 4 5 #include "base/bind.h" 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" 15 16 using content::BrowserThread; 17 18 class WebUISourcesTest : public testing::Test { 19 public: 20 WebUISourcesTest() 21 : result_data_size_(0), 22 ui_thread_(BrowserThread::UI, base::MessageLoop::current()) {} 23 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_; } 27 28 void StartDataRequest(const std::string& source) { 29 theme_source()->StartDataRequest(source, -1, -1, callback_); 30 } 31 32 size_t result_data_size_; 33 34 private: 35 virtual void SetUp() { 36 profile_.reset(new TestingProfile()); 37 theme_source_.reset(new ThemeSource(profile_.get())); 38 callback_ = base::Bind(&WebUISourcesTest::SendResponse, 39 base::Unretained(this)); 40 } 41 42 virtual void TearDown() { 43 theme_source_.reset(); 44 profile_.reset(); 45 } 46 47 void SendResponse(base::RefCountedMemory* data) { 48 result_data_size_ = data ? data->size() : 0; 49 } 50 51 content::URLDataSource::GotDataCallback callback_; 52 53 base::MessageLoop loop_; 54 content::TestBrowserThread ui_thread_; 55 56 scoped_ptr<TestingProfile> profile_; 57 scoped_ptr<ThemeSource> theme_source_; 58 }; 59 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"), 63 "text/css"); 64 EXPECT_EQ(theme_source()->GetMimeType("WRONGURL"), "image/png"); 65 } 66 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"); 71 size_t min = 0; 72 EXPECT_GT(result_data_size_, min); 73 74 StartDataRequest("IDR_THEME_TOOLBAR"); 75 EXPECT_GT(result_data_size_, min); 76 } 77 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; 86 87 StartDataRequest("css/new_tab_theme.css"); 88 EXPECT_NE(result_data_size_, empty_size); 89 90 StartDataRequest("css/new_tab_theme.css?pie"); 91 EXPECT_NE(result_data_size_, empty_size); 92 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); 96 } 97