diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 16:52:23 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 16:52:23 +0000 |
commit | a42b559451a78a1fc1a8d1f2bfe129d51924a3c0 (patch) | |
tree | 846d078b08aa558e7c20f8f44e3dd46571a514cb | |
parent | 9aca7238d57d556e52532ab3491a11192bfad6b8 (diff) | |
download | chromium_src-a42b559451a78a1fc1a8d1f2bfe129d51924a3c0.zip chromium_src-a42b559451a78a1fc1a8d1f2bfe129d51924a3c0.tar.gz chromium_src-a42b559451a78a1fc1a8d1f2bfe129d51924a3c0.tar.bz2 |
GTK Themes: Add basic unit tests for gtk themes.
Review URL: http://codereview.chromium.org/186019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25317 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc | 2 | ||||
-rw-r--r-- | chrome/browser/gtk/gtk_theme_provider.h | 18 | ||||
-rw-r--r-- | chrome/browser/gtk/gtk_theme_provider_unittest.cc | 136 | ||||
-rw-r--r-- | chrome/chrome.gyp | 1 | ||||
-rw-r--r-- | chrome/test/testing_profile.cc | 25 | ||||
-rw-r--r-- | chrome/test/testing_profile.h | 11 |
6 files changed, 180 insertions, 13 deletions
diff --git a/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc b/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc index 561cd6d..e946047 100644 --- a/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc +++ b/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc @@ -36,7 +36,7 @@ class DOMUISourcesTest : public testing::Test { private: virtual void SetUp() { profile_.reset(new TestingProfile()); - profile_.get()->CreateThemeProvider(); + profile_->InitThemes(); theme_source_ = new MockThemeSource(profile_.get()); } diff --git a/chrome/browser/gtk/gtk_theme_provider.h b/chrome/browser/gtk/gtk_theme_provider.h index be160e5..e914d89 100644 --- a/chrome/browser/gtk/gtk_theme_provider.h +++ b/chrome/browser/gtk/gtk_theme_provider.h @@ -5,6 +5,7 @@ #ifndef CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ #define CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ +#include <string> #include <vector> #include "chrome/browser/browser_theme_provider.h" @@ -62,19 +63,24 @@ class GtkThemeProvider : public BrowserThemeProvider, // label. Used for borders between GTK stuff and the webcontent. GdkColor GetBorderColor(); - private: - // Load theme data from preferences, possibly picking colors from GTK. - virtual void LoadThemePrefs(); - - // Let all the browser views know that themes have changed. - virtual void NotifyThemeChanged(); + // Expose the inner widgets. Only used for testing. + GtkWidget* fake_window() { return fake_window_; } + GtkWidget* fake_label() { return fake_label_.get(); } + protected: // Possibly creates a theme specific version of theme_toolbar_default. // (minimally acceptable version right now, which is just a fill of the bg // color; this should instead invoke gtk_draw_box(...) for complex theme // engines.) virtual SkBitmap* LoadThemeBitmap(int id); + private: + // Load theme data from preferences, possibly picking colors from GTK. + virtual void LoadThemePrefs(); + + // Let all the browser views know that themes have changed. + virtual void NotifyThemeChanged(); + // If use_gtk_ is true, completely ignores this call. Otherwise passes it to // the superclass. virtual void SaveThemeBitmap(const std::string resource_name, int id); diff --git a/chrome/browser/gtk/gtk_theme_provider_unittest.cc b/chrome/browser/gtk/gtk_theme_provider_unittest.cc new file mode 100644 index 0000000..79869e8 --- /dev/null +++ b/chrome/browser/gtk/gtk_theme_provider_unittest.cc @@ -0,0 +1,136 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <gtk/gtk.h> + +#include "chrome/browser/gtk/gtk_theme_provider.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// Converts a GdkColor to a SkColor. +SkColor GdkToSkColor(GdkColor* color) { + return SkColorSetRGB(color->red >> 8, + color->green >> 8, + color->blue >> 8); +} + +} // namespace + +class GtkThemeProviderTest : public testing::Test { + public: + GtkThemeProviderTest() : provider_(NULL) {} + + void SetUseGtkTheme(bool use_gtk_theme) { + profile_.GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, use_gtk_theme); + } + + void BuildProvider() { + profile_.InitThemes(); + provider_ = GtkThemeProvider::GetFrom(&profile_); + } + + void UseThemeProvider(GtkThemeProvider* provider) { + profile_.UseThemeProvider(provider); + provider_ = GtkThemeProvider::GetFrom(&profile_); + } + + protected: + TestingProfile profile_; + + GtkThemeProvider* provider_; +}; + +TEST_F(GtkThemeProviderTest, DefaultValues) { + SetUseGtkTheme(false); + BuildProvider(); + + // Test that we get the default theme colors back when in normal mode. + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_FRAME), + BrowserThemeProvider::kDefaultColorFrame); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_FRAME_INACTIVE), + BrowserThemeProvider::kDefaultColorFrameInactive); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_FRAME_INCOGNITO), + BrowserThemeProvider::kDefaultColorFrameIncognito); + EXPECT_EQ(provider_->GetColor( + BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE), + BrowserThemeProvider::kDefaultColorFrameIncognitoInactive); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_TOOLBAR), + BrowserThemeProvider::kDefaultColorToolbar); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_TAB_TEXT), + BrowserThemeProvider::kDefaultColorTabText); + EXPECT_EQ(provider_->GetColor( + BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT), + BrowserThemeProvider::kDefaultColorBackgroundTabText); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT), + BrowserThemeProvider::kDefaultColorBookmarkText); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND), + BrowserThemeProvider::kDefaultColorNTPBackground); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_TEXT), + BrowserThemeProvider::kDefaultColorNTPText); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_LINK), + BrowserThemeProvider::kDefaultColorNTPLink); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_HEADER), + BrowserThemeProvider::kDefaultColorNTPHeader); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_SECTION), + BrowserThemeProvider::kDefaultColorNTPSection); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_SECTION_TEXT), + BrowserThemeProvider::kDefaultColorNTPSectionText); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_NTP_SECTION_LINK), + BrowserThemeProvider::kDefaultColorNTPSectionLink); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_CONTROL_BACKGROUND), + BrowserThemeProvider::kDefaultColorControlBackground); + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_BUTTON_BACKGROUND), + BrowserThemeProvider::kDefaultColorButtonBackground); +} + +TEST_F(GtkThemeProviderTest, UsingGtkValues) { + SetUseGtkTheme(true); + BuildProvider(); + + // This test only verifies that we're using GTK values. Because of Gtk's + // large, implied global state, it would take some IN_PROCESS_BROWSER_TESTS + // to write an equivalent of DefaultValues above in a way that wouldn't make + // other tests flaky. kColorTabText is the only simple path where there's no + // weird calculations for edge cases so use that as a simple test. + GtkWidget* fake_label = provider_->fake_label(); + GtkStyle* label_style = gtk_rc_get_style(fake_label); + GdkColor label_color = label_style->text[GTK_STATE_NORMAL]; + EXPECT_EQ(provider_->GetColor(BrowserThemeProvider::COLOR_TAB_TEXT), + GdkToSkColor(&label_color)); +} + +// Helper class to GtkThemeProviderTest.UsingGtkFrame. +class ImageVerifierGtkThemeProvider : public GtkThemeProvider { + public: + ImageVerifierGtkThemeProvider() : theme_toolbar_(NULL) { } + + virtual SkBitmap* LoadThemeBitmap(int id) { + if (id == IDR_THEME_TOOLBAR) { + theme_toolbar_ = GtkThemeProvider::LoadThemeBitmap(id); + return theme_toolbar_; + } else { + return GtkThemeProvider::LoadThemeBitmap(id); + } + } + + SkBitmap* theme_toolbar_; +}; + +TEST_F(GtkThemeProviderTest, InjectsToolbar) { + SetUseGtkTheme(true); + ImageVerifierGtkThemeProvider* verifier_provider = + new ImageVerifierGtkThemeProvider; + UseThemeProvider(verifier_provider); + + // Make sure the image we get from the public BrowserThemeProvider interface + // is the one we injected through GtkThemeProvider. + SkBitmap* image = provider_->GetBitmapNamed(IDR_THEME_TOOLBAR); + EXPECT_TRUE(verifier_provider->theme_toolbar_); + EXPECT_TRUE(image); + EXPECT_EQ(verifier_provider->theme_toolbar_, image); +} diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 774e7a4..807cc50 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -4040,6 +4040,7 @@ 'browser/google_url_tracker_unittest.cc', 'browser/google_update_settings_posix_unittest.cc', 'browser/gtk/bookmark_editor_gtk_unittest.cc', + 'browser/gtk/gtk_theme_provider_unittest.cc', 'browser/gtk/go_button_gtk_unittest.cc', 'browser/gtk/keyword_editor_view_unittest.cc', 'browser/gtk/options/cookies_view_unittest.cc', diff --git a/chrome/test/testing_profile.cc b/chrome/test/testing_profile.cc index cce795d1..bddfc7f 100644 --- a/chrome/test/testing_profile.cc +++ b/chrome/test/testing_profile.cc @@ -4,11 +4,16 @@ #include "chrome/test/testing_profile.h" +#include "build/build_config.h" #include "base/string_util.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/history/history_backend.h" #include "chrome/common/chrome_constants.h" +#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS) +#include "chrome/browser/gtk/gtk_theme_provider.h" +#endif + using base::Time; namespace { @@ -70,6 +75,7 @@ class BookmarkLoadObserver : public BookmarkModelObserver { TestingProfile::TestingProfile() : start_time_(Time::Now()), + created_theme_provider_(false), has_history_service_(false), off_the_record_(false), last_session_exited_cleanly_(true) { @@ -166,8 +172,23 @@ void TestingProfile::CreateTemplateURLModel() { template_url_model_.reset(new TemplateURLModel(this)); } -void TestingProfile::CreateThemeProvider() { - theme_provider_ = new BrowserThemeProvider(); +void TestingProfile::UseThemeProvider(BrowserThemeProvider* theme_provider) { + theme_provider->Init(this); + created_theme_provider_ = true; + theme_provider_ = theme_provider; +} + +void TestingProfile::InitThemes() { + if (!created_theme_provider_) { +#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS) + scoped_refptr<BrowserThemeProvider> themes(new GtkThemeProvider); +#else + scoped_refptr<BrowserThemeProvider> themes(new BrowserThemeProvider); +#endif + themes->Init(this); + created_theme_provider_ = true; + theme_provider_.swap(themes); + } } void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index d786017..bc06a3d 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -53,8 +53,9 @@ class TestingProfile : public Profile { // Creates a TemplateURLModel. If not invoked the TemplateURLModel is NULL. void CreateTemplateURLModel(); - // Creates a ThemeProvider. If not invoked the ThemeProvider is NULL. - void CreateThemeProvider(); + // Uses a specific theme provider for this profile. TestingProfile takes + // ownership of |theme_provider|. + void UseThemeProvider(BrowserThemeProvider* theme_provider); virtual FilePath GetPath() { return path_; @@ -142,7 +143,7 @@ class TestingProfile : public Profile { virtual bool HasCreatedDownloadManager() const { return false; } - virtual void InitThemes() { } + virtual void InitThemes(); virtual void SetTheme(Extension* extension) { } virtual void SetNativeTheme() { } virtual void ClearTheme() { } @@ -150,6 +151,7 @@ class TestingProfile : public Profile { return NULL; } virtual ThemeProvider* GetThemeProvider() { + InitThemes(); return theme_provider_.get(); } virtual URLRequestContext* GetRequestContext() { @@ -267,8 +269,9 @@ class TestingProfile : public Profile { // The SessionService. Defaults to NULL, but can be set using the setter. scoped_refptr<SessionService> session_service_; - // The theme provider. Only created if CreateThemeProvider is invoked. + // The theme provider. Created lazily by GetThemeProvider()/InitThemes(). scoped_refptr<BrowserThemeProvider> theme_provider_; + bool created_theme_provider_; // Do we have a history service? This defaults to the value of // history_service, but can be explicitly set. |