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