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