summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/gtk_theme_provider_unittest.cc
blob: 79869e8e85d8752f760520b50535eb87f78cab4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
}