Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uplift #2134 to 0.64.x (Make chrome.braveTheme.onBraveThemeTypeChanged emit by native theme change) #2169

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 3 additions & 33 deletions browser/extensions/api/brave_theme_api_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,24 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/extensions/api/brave_theme_api.h"
#include "brave/browser/extensions/brave_theme_event_router.h"
#include "brave/browser/themes/brave_theme_service.h"
#include "brave/browser/themes/theme_properties.h"
#include "brave/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"

using BraveThemeEventRouterBrowserTest = InProcessBrowserTest;
using BTS = BraveThemeService;
using extensions::api::BraveThemeGetBraveThemeTypeFunction;
using extension_function_test_utils::RunFunctionAndReturnSingleResult;

namespace {
class MockBraveThemeEventRouter : public extensions::BraveThemeEventRouter {
public:
MockBraveThemeEventRouter() {}
~MockBraveThemeEventRouter() override {}

MOCK_METHOD1(OnBraveThemeTypeChanged, void(Profile*));
};

void SetBraveThemeType(Profile* profile, BraveThemeType type) {
profile->GetPrefs()->SetInteger(kBraveThemeType, type);
}
} // namespace

IN_PROC_BROWSER_TEST_F(BraveThemeEventRouterBrowserTest,
BraveThemeEventRouterTest) {
Profile* profile = browser()->profile();
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_DARK);

MockBraveThemeEventRouter* mock_router_ = new MockBraveThemeEventRouter;
EXPECT_CALL(*mock_router_, OnBraveThemeTypeChanged(profile)).Times(1);

BraveThemeService* service = static_cast<BraveThemeService*>(
ThemeServiceFactory::GetForProfile(browser()->profile()));
service->SetBraveThemeEventRouterForTesting(mock_router_);
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT);
}

using BTS = BraveThemeService;
using extensions::api::BraveThemeGetBraveThemeTypeFunction;
using extension_function_test_utils::RunFunctionAndReturnSingleResult;

class BraveThemeAPIBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
Expand Down
60 changes: 42 additions & 18 deletions browser/extensions/brave_theme_event_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,69 @@

#include "brave/browser/extensions/brave_theme_event_router.h"

#include <utility>
#include <memory>
#include <string>
#include <utility>

#include "brave/browser/themes/brave_theme_service.h"
#include "brave/common/extensions/api/brave_theme.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "ui/native_theme/native_theme_dark_aura.h"

namespace extensions {

class BraveThemeEventRouterImpl : public BraveThemeEventRouter {
public:
BraveThemeEventRouterImpl() {}
~BraveThemeEventRouterImpl() override {}
BraveThemeEventRouter::BraveThemeEventRouter(Profile* profile)
: profile_(profile),
using_dark_(IsDarkModeEnabled()),
observer_(this) {
ResetThemeObserver();
}

BraveThemeEventRouter::~BraveThemeEventRouter() {}

void OnBraveThemeTypeChanged(Profile* profile) override;
void BraveThemeEventRouter::OnNativeThemeUpdated(
ui::NativeTheme* observed_theme) {
DCHECK(observer_.IsObserving(observed_theme));
ResetThemeObserver();

private:
DISALLOW_COPY_AND_ASSIGN(BraveThemeEventRouterImpl);
};
bool use_dark = IsDarkModeEnabled();
if (use_dark == using_dark_)
return;

void BraveThemeEventRouterImpl::OnBraveThemeTypeChanged(Profile* profile) {
EventRouter* event_router = EventRouter::Get(profile);
const std::string theme_type = BraveThemeService::GetStringFromBraveThemeType(
BraveThemeService::GetActiveBraveThemeType(profile));
using_dark_ = use_dark;
Notify();
}

void BraveThemeEventRouter::Notify() {
EventRouter* event_router = EventRouter::Get(profile_);
const std::string theme_type =
BraveThemeService::GetStringFromBraveThemeType(
BraveThemeService::GetActiveBraveThemeType(profile_));

auto event = std::make_unique<extensions::Event>(
extensions::events::BRAVE_ON_BRAVE_THEME_TYPE_CHANGED,
api::brave_theme::OnBraveThemeTypeChanged::kEventName,
api::brave_theme::OnBraveThemeTypeChanged::Create(theme_type),
profile);
profile_);

event_router->BroadcastEvent(std::move(event));
}

// static
std::unique_ptr<BraveThemeEventRouter> BraveThemeEventRouter::Create() {
return std::make_unique<BraveThemeEventRouterImpl>();
bool BraveThemeEventRouter::IsDarkModeEnabled() const {
return BraveThemeService::GetActiveBraveThemeType(profile_) ==
BRAVE_THEME_TYPE_DARK;
}

void BraveThemeEventRouter::ResetThemeObserver() {
auto* current_native_theme =
IsDarkModeEnabled() ? ui::NativeThemeDarkAura::instance()
: ui::NativeTheme::GetInstanceForNativeUi();
if (!observer_.IsObserving(current_native_theme)) {
observer_.RemoveAll();
observer_.Add(current_native_theme);
current_native_theme_for_testing_ = current_native_theme;
}
}

} // namespace extensions
31 changes: 25 additions & 6 deletions browser/extensions/brave_theme_event_router.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_

#include <memory>
#include "base/gtest_prod_util.h"
#include "base/scoped_observer.h"
#include "ui/native_theme/native_theme_observer.h"

class Profile;

namespace extensions {

class BraveThemeEventRouter {
class BraveThemeEventRouter : public ui::NativeThemeObserver {
public:
static std::unique_ptr<BraveThemeEventRouter> Create();
explicit BraveThemeEventRouter(Profile* profile);
~BraveThemeEventRouter() override;

virtual ~BraveThemeEventRouter() {}
private:
friend class MockBraveThemeEventRouter;

virtual void OnBraveThemeTypeChanged(Profile* profile) = 0;
// ui::NativeThemeObserver overrides:
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;

bool IsDarkModeEnabled() const;
void ResetThemeObserver();

// Make virtual for testing.
virtual void Notify();

ui::NativeTheme* current_native_theme_for_testing_ = nullptr;
Profile* profile_;
bool using_dark_;
ScopedObserver<ui::NativeTheme, BraveThemeEventRouter> observer_;

DISALLOW_COPY_AND_ASSIGN(BraveThemeEventRouter);
};

} // namespace extensions
Expand Down
69 changes: 69 additions & 0 deletions browser/extensions/brave_theme_event_router_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/extensions/brave_theme_event_router.h"
#include "brave/browser/themes/brave_theme_service.h"
#include "brave/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/native_theme/native_theme_dark_aura.h"

using BraveThemeEventRouterBrowserTest = InProcessBrowserTest;

namespace {

void SetBraveThemeType(Profile* profile, BraveThemeType type) {
profile->GetPrefs()->SetInteger(kBraveThemeType, type);
}

} // namespace

namespace extensions {

class MockBraveThemeEventRouter : public BraveThemeEventRouter {
public:
using BraveThemeEventRouter::BraveThemeEventRouter;
~MockBraveThemeEventRouter() override {}

ui::NativeTheme* current_native_theme_for_testing() const {
return current_native_theme_for_testing_;
}

MOCK_METHOD0(Notify, void());
};

} // namespace extensions


IN_PROC_BROWSER_TEST_F(BraveThemeEventRouterBrowserTest,
ThemeChangeTest) {
Profile* profile = browser()->profile();
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_DARK);

extensions::MockBraveThemeEventRouter* mock_router =
new extensions::MockBraveThemeEventRouter(profile);
BraveThemeService* service = static_cast<BraveThemeService*>(
ThemeServiceFactory::GetForProfile(browser()->profile()));
service->SetBraveThemeEventRouterForTesting(mock_router);

EXPECT_CALL(*mock_router, Notify()).Times(1);
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT);
EXPECT_EQ(
ui::NativeTheme::GetInstanceForNativeUi(),
mock_router->current_native_theme_for_testing());

EXPECT_CALL(*mock_router, Notify()).Times(1);
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_DARK);
EXPECT_EQ(
ui::NativeThemeDarkAura::instance(),
mock_router->current_native_theme_for_testing());

EXPECT_CALL(*mock_router, Notify()).Times(0);
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_DARK);
}
8 changes: 3 additions & 5 deletions browser/themes/brave_theme_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ void BraveThemeService::Init(Profile* profile) {
profile->GetPrefs(),
base::Bind(&BraveThemeService::OnPreferenceChanged,
base::Unretained(this)));

brave_theme_event_router_.reset(
new extensions::BraveThemeEventRouter(profile));
}

ThemeService::Init(profile);
Expand Down Expand Up @@ -209,11 +212,6 @@ void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) {
? ui::NativeThemeDarkAura::instance()->NotifyObservers()
: ui::NativeTheme::GetInstanceForNativeUi()->NotifyObservers();
}

if (!brave_theme_event_router_)
brave_theme_event_router_ = extensions::BraveThemeEventRouter::Create();

brave_theme_event_router_->OnBraveThemeTypeChanged(profile());
}

void BraveThemeService::RecoverPrefStates(Profile* profile) {
Expand Down
7 changes: 6 additions & 1 deletion browser/themes/brave_theme_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BraveThemeService : public ThemeService {
private:
friend class BraveThemeServiceTestWithoutSystemTheme;
FRIEND_TEST_ALL_PREFIXES(BraveThemeEventRouterBrowserTest,
BraveThemeEventRouterTest);
ThemeChangeTest);
FRIEND_TEST_ALL_PREFIXES(BraveThemeServiceTest, GetBraveThemeListTest);
FRIEND_TEST_ALL_PREFIXES(BraveThemeServiceTest, SystemThemeChangeTest);

Expand All @@ -72,6 +72,11 @@ class BraveThemeService : public ThemeService {

IntegerPrefMember brave_theme_type_pref_;

// Make BraveThemeService own BraveThemeEventRouter.
// BraveThemeEventRouter does its job independently with BraveThemeService.
// However, both are related with brave theme and have similar life cycle.
// So, Owning BraveThemeEventRouter by BraveThemeService seems fine.
// Use smart ptr for testing by SetBraveThemeEventRouterForTesting.
std::unique_ptr<extensions::BraveThemeEventRouter> brave_theme_event_router_;

DISALLOW_COPY_AND_ASSIGN(BraveThemeService);
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ test("brave_browser_tests") {
"//brave/browser/extensions/brave_extension_functional_test.h",
"//brave/browser/extensions/api/brave_shields_api_browsertest.cc",
"//brave/browser/extensions/api/brave_theme_api_browsertest.cc",
"//brave/browser/extensions/brave_theme_event_router_browsertest.cc",
"//brave/browser/net/brave_network_delegate_browsertest.cc",
"//brave/browser/net/brave_network_delegate_hsts_fingerprinting_browsertest.cc",
"//brave/browser/renderer_context_menu/brave_mock_render_view_context_menu.cc",
Expand Down