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