Skip to content

Commit

Permalink
Dark Mode: split out TestNativeTheme as a separate class/target
Browse files Browse the repository at this point in the history
There was a handy dandy testing version of NativeTheme in
view_unittest.cc that I'd like to reuse in upcoming unit tests for WebUI
Dark Mode support (https://crrev.com/c/1354685).

BUG=883049
R=sky@chromium.org

Change-Id: Ie39f6c42f5069450ec9933b1b83d4cb615ab713d
Reviewed-on: https://chromium-review.googlesource.com/c/1357607
Commit-Queue: Dan Beam <dbeam@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613233}
  • Loading branch information
danbeam authored and Commit Bot committed Dec 3, 2018
1 parent daf8790 commit 7fc92be
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 37 deletions.
19 changes: 18 additions & 1 deletion ui/native_theme/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ jumbo_component("native_theme") {

defines = [ "NATIVE_THEME_IMPLEMENTATION" ]

public_deps = [
"//skia",
]

deps = [
"//base",
"//base/third_party/dynamic_annotations",
"//cc/paint",
"//skia",
"//ui/base",
"//ui/display",
"//ui/gfx",
Expand Down Expand Up @@ -81,6 +84,20 @@ if (is_win) {
}
}

jumbo_source_set("test_support") {
testonly = true

deps = [
":native_theme",
"//base",
]

sources = [
"test_native_theme.cc",
"test_native_theme.h",
]
}

test("native_theme_unittests") {
sources = []

Expand Down
48 changes: 48 additions & 0 deletions ui/native_theme/test_native_theme.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018 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 "ui/native_theme/test_native_theme.h"

namespace ui {

TestNativeTheme::TestNativeTheme() {}
TestNativeTheme::~TestNativeTheme() {}

SkColor TestNativeTheme::GetSystemColor(ColorId color_id) const {
return SK_ColorRED;
}

gfx::Size TestNativeTheme::GetPartSize(Part part,
State state,
const ExtraParams& extra) const {
return gfx::Size();
}

void TestNativeTheme::Paint(cc::PaintCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const {}

bool TestNativeTheme::SupportsNinePatch(Part part) const {
return false;
}

gfx::Size TestNativeTheme::GetNinePatchCanvasSize(Part part) const {
return gfx::Size();
}

gfx::Rect TestNativeTheme::GetNinePatchAperture(Part part) const {
return gfx::Rect();
}

bool TestNativeTheme::UsesHighContrastColors() const {
return false;
}

bool TestNativeTheme::SystemDarkModeEnabled() const {
return dark_mode_;
}

} // namespace ui
44 changes: 44 additions & 0 deletions ui/native_theme/test_native_theme.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2018 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.

#ifndef UI_NATIVE_THEME_TEST_NATIVE_THEME_H_
#define UI_NATIVE_THEME_TEST_NATIVE_THEME_H_

#include "base/macros.h"
#include "ui/native_theme/native_theme.h"

namespace ui {

class TestNativeTheme : public NativeTheme {
public:
TestNativeTheme();
~TestNativeTheme() override;

// NativeTheme:
SkColor GetSystemColor(ColorId color_id) const override;
gfx::Size GetPartSize(Part part,
State state,
const ExtraParams& extra) const override;
void Paint(cc::PaintCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const override;
bool SupportsNinePatch(Part part) const override;
gfx::Size GetNinePatchCanvasSize(Part part) const override;
gfx::Rect GetNinePatchAperture(Part part) const override;
bool UsesHighContrastColors() const override;
bool SystemDarkModeEnabled() const override;

void SetDarkMode(bool dark_mode) { dark_mode_ = dark_mode; }

private:
bool dark_mode_ = false;

DISALLOW_COPY_AND_ASSIGN(TestNativeTheme);
};

} // namespace ui

#endif // UI_NATIVE_THEME_TEST_NATIVE_THEME_H_
1 change: 1 addition & 0 deletions ui/views/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ source_set("views_unittests_sources") {
"//ui/gfx/geometry",
"//ui/gl:test_support",
"//ui/native_theme",
"//ui/native_theme:test_support",
"//ui/resources",
"//ui/resources:ui_test_pak",
"//ui/strings",
Expand Down
38 changes: 2 additions & 36 deletions ui/views/view_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "ui/gfx/path.h"
#include "ui/gfx/transform.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/test_native_theme.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/background.h"
#include "ui/views/controls/native/native_view_host.h"
Expand Down Expand Up @@ -4787,41 +4788,6 @@ class ViewThatAddsViewInOnNativeThemeChanged : public View {
DISALLOW_COPY_AND_ASSIGN(ViewThatAddsViewInOnNativeThemeChanged);
};

// See comment above test for details.
class TestNativeTheme : public ui::NativeTheme {
public:
TestNativeTheme() {}
~TestNativeTheme() override {}

// ui::NativeTheme:
SkColor GetSystemColor(ColorId color_id) const override {
return SK_ColorRED;
}
gfx::Size GetPartSize(Part part,
State state,
const ExtraParams& extra) const override {
return gfx::Size();
}
void Paint(cc::PaintCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const override {}

bool SupportsNinePatch(Part part) const override { return false; }
gfx::Size GetNinePatchCanvasSize(Part part) const override {
return gfx::Size();
}
gfx::Rect GetNinePatchAperture(Part part) const override {
return gfx::Rect();
}
bool UsesHighContrastColors() const override { return false; }
bool SystemDarkModeEnabled() const override { return false; }

private:
DISALLOW_COPY_AND_ASSIGN(TestNativeTheme);
};

// Creates and adds a new child view to |parent| that has a layer.
void AddViewWithChildLayer(View* parent) {
View* child = new View;
Expand All @@ -4838,7 +4804,7 @@ void AddViewWithChildLayer(View* parent) {
// before the layer hierarchy was updated. OnNativeThemeChanged() should be
// called after the layer hierarchy matches the view hierarchy.
TEST_F(ViewTest, CrashOnAddFromFromOnNativeThemeChanged) {
TestNativeTheme theme;
ui::TestNativeTheme theme;
WidgetWithCustomTheme widget(&theme);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
Expand Down

0 comments on commit 7fc92be

Please sign in to comment.