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 "base/memory/ref_counted_memory.h" 6 #include "chrome/browser/profiles/profile.h" 7 #include "chrome/browser/ui/webui/theme_source.h" 8 #include "chrome/common/url_constants.h" 9 #include "chrome/test/testing_profile.h" 10 #include "content/browser/browser_thread.h" 11 #include "grit/theme_resources.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 // A mock ThemeSource (so we can override SendResponse to get at its data). 15 class MockThemeSource : public ThemeSource { 16 public: 17 explicit MockThemeSource(Profile* profile) 18 : ThemeSource(profile), 19 result_request_id_(-1), 20 result_data_size_(0) { 21 } 22 23 virtual void SendResponse(int request_id, RefCountedMemory* data) { 24 result_data_size_ = data ? data->size() : 0; 25 result_request_id_ = request_id; 26 } 27 28 int result_request_id_; 29 size_t result_data_size_; 30 31 private: 32 ~MockThemeSource() {} 33 }; 34 35 class WebUISourcesTest : public testing::Test { 36 public: 37 WebUISourcesTest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} 38 39 TestingProfile* profile() const { return profile_.get(); } 40 MockThemeSource* theme_source() const { return theme_source_.get(); } 41 private: 42 virtual void SetUp() { 43 profile_.reset(new TestingProfile()); 44 theme_source_ = new MockThemeSource(profile_.get()); 45 } 46 47 virtual void TearDown() { 48 theme_source_ = NULL; 49 profile_.reset(NULL); 50 } 51 52 MessageLoop loop_; 53 BrowserThread ui_thread_; 54 55 scoped_ptr<TestingProfile> profile_; 56 scoped_refptr<MockThemeSource> theme_source_; 57 }; 58 59 TEST_F(WebUISourcesTest, ThemeSourceMimeTypes) { 60 EXPECT_EQ(theme_source()->GetMimeType("css/newtab.css"), "text/css"); 61 EXPECT_EQ(theme_source()->GetMimeType("css/newtab.css?foo"), "text/css"); 62 EXPECT_EQ(theme_source()->GetMimeType("WRONGURL"), "image/png"); 63 } 64 65 TEST_F(WebUISourcesTest, ThemeSourceImages) { 66 // We used to PNGEncode the images ourselves, but encoder differences 67 // invalidated that. We now just check that the image exists. 68 theme_source()->StartDataRequest("IDR_THEME_FRAME_INCOGNITO", true, 1); 69 size_t min = 0; 70 EXPECT_EQ(theme_source()->result_request_id_, 1); 71 EXPECT_GT(theme_source()->result_data_size_, min); 72 73 theme_source()->StartDataRequest("IDR_THEME_TOOLBAR", true, 2); 74 EXPECT_EQ(theme_source()->result_request_id_, 2); 75 EXPECT_GT(theme_source()->result_data_size_, min); 76 } 77 78 TEST_F(WebUISourcesTest, ThemeSourceCSS) { 79 BrowserThread io_thread(BrowserThread::IO, MessageLoop::current()); 80 // Generating the test data for the NTP CSS would just involve copying the 81 // method, or being super brittle and hard-coding the result (requiring 82 // an update to the unittest every time the CSS template changes), so we 83 // just check for a successful request and data that is non-null. 84 size_t empty_size = 0; 85 86 theme_source()->StartDataRequest("css/newtab.css", false, 1); 87 EXPECT_EQ(theme_source()->result_request_id_, 1); 88 EXPECT_NE(theme_source()->result_data_size_, empty_size); 89 90 theme_source()->StartDataRequest("css/newtab.css?pie", false, 3); 91 EXPECT_EQ(theme_source()->result_request_id_, 3); 92 EXPECT_NE(theme_source()->result_data_size_, empty_size); 93 94 // Check that we send NULL back when we can't find what we're looking for. 95 theme_source()->StartDataRequest("css/WRONGURL", false, 7); 96 EXPECT_EQ(theme_source()->result_request_id_, 7); 97 EXPECT_EQ(theme_source()->result_data_size_, empty_size); 98 } 99