Commit 65f31703 authored by Patrick Monette's avatar Patrick Monette Committed by Commit Bot

Create a common test ProtocolHandlerRegistry::Delegate

This avoids having tests defining their own delegate implementation
when they need one.

Bug: 1021600
Change-Id: I12d84c647e17c3c476ce2dc19f5298a524cf98dd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1900346
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714749}
parent 57071897
......@@ -37,6 +37,7 @@
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h"
#include "chrome/browser/domain_reliability/service_factory.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service_factory.h"
......@@ -486,41 +487,11 @@ class RemoveFaviconTester {
DISALLOW_COPY_AND_ASSIGN(RemoveFaviconTester);
};
// Custom ProtocolHandlerRegistry delegate that doesn't change any OS settings.
class FakeProtocolHandlerRegistryDelegate
: public ProtocolHandlerRegistry::Delegate {
public:
~FakeProtocolHandlerRegistryDelegate() override = default;
void RegisterExternalHandler(const std::string& protocol) override {
registered_protocols_.insert(protocol);
}
void DeregisterExternalHandler(const std::string& protocol) override {
registered_protocols_.erase(protocol);
}
bool IsExternalHandlerRegistered(const std::string& protocol) override {
return registered_protocols_.count(protocol);
}
void RegisterWithOSAsDefaultClient(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override {}
void CheckDefaultClientWithOS(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override {}
private:
std::set<std::string> registered_protocols_;
};
std::unique_ptr<KeyedService> BuildProtocolHandlerRegistry(
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return std::make_unique<ProtocolHandlerRegistry>(
profile, std::make_unique<FakeProtocolHandlerRegistryDelegate>());
profile, std::make_unique<TestProtocolHandlerRegistryDelegate>());
}
class ClearDomainReliabilityTester {
......
......@@ -15,6 +15,7 @@
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/test/base/testing_profile.h"
#include "components/browsing_data/core/browsing_data_utils.h"
......@@ -31,48 +32,6 @@
namespace {
class TestProtocolHandlerRegistryDelegate
: public ProtocolHandlerRegistry::Delegate {
public:
TestProtocolHandlerRegistryDelegate() = default;
~TestProtocolHandlerRegistryDelegate() override = default;
TestProtocolHandlerRegistryDelegate(
const TestProtocolHandlerRegistryDelegate& other) = delete;
TestProtocolHandlerRegistryDelegate& operator=(
const TestProtocolHandlerRegistryDelegate& other) = delete;
// ProtocolHandlerRegistry::Delegate:
void RegisterExternalHandler(const std::string& protocol) override {
bool inserted = registered_protocols_.insert(protocol).second;
DCHECK(inserted);
}
void DeregisterExternalHandler(const std::string& protocol) override {
size_t removed = registered_protocols_.erase(protocol);
DCHECK_EQ(removed, 1u);
}
bool IsExternalHandlerRegistered(const std::string& protocol) override {
return registered_protocols_.find(protocol) != registered_protocols_.end();
}
void RegisterWithOSAsDefaultClient(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), shell_integration::NOT_DEFAULT));
}
void CheckDefaultClientWithOS(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), shell_integration::NOT_DEFAULT));
}
private:
base::flat_set<std::string> registered_protocols_;
};
class SiteSettingsCounterTest : public testing::Test {
public:
void SetUp() override {
......
// Copyright 2019 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 "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h"
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/threading/thread_task_runner_handle.h"
TestProtocolHandlerRegistryDelegate::TestProtocolHandlerRegistryDelegate() =
default;
TestProtocolHandlerRegistryDelegate::~TestProtocolHandlerRegistryDelegate() =
default;
// ProtocolHandlerRegistry::Delegate:
void TestProtocolHandlerRegistryDelegate::RegisterExternalHandler(
const std::string& protocol) {
bool inserted = registered_protocols_.insert(protocol).second;
DCHECK(inserted);
}
void TestProtocolHandlerRegistryDelegate::DeregisterExternalHandler(
const std::string& protocol) {
size_t removed = registered_protocols_.erase(protocol);
DCHECK_EQ(removed, 1u);
}
bool TestProtocolHandlerRegistryDelegate::IsExternalHandlerRegistered(
const std::string& protocol) {
return registered_protocols_.find(protocol) != registered_protocols_.end();
}
void TestProtocolHandlerRegistryDelegate::RegisterWithOSAsDefaultClient(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) {
// Respond asynchronously to mimic the real behavior.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), shell_integration::IS_DEFAULT));
}
void TestProtocolHandlerRegistryDelegate::CheckDefaultClientWithOS(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) {
// Respond asynchronously to mimic the real behavior.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), shell_integration::IS_DEFAULT));
}
// Copyright 2019 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 CHROME_BROWSER_CUSTOM_HANDLERS_TEST_PROTOCOL_HANDLER_REGISTRY_DELEGATE_H_
#define CHROME_BROWSER_CUSTOM_HANDLERS_TEST_PROTOCOL_HANDLER_REGISTRY_DELEGATE_H_
#include <set>
#include <string>
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
// A test ProtocolHandlerRegistry::Delegate implementation that keeps track of
// registered protocols and doesn't change any OS settings.
class TestProtocolHandlerRegistryDelegate
: public ProtocolHandlerRegistry::Delegate {
public:
TestProtocolHandlerRegistryDelegate();
~TestProtocolHandlerRegistryDelegate() override;
TestProtocolHandlerRegistryDelegate(
const TestProtocolHandlerRegistryDelegate& other) = delete;
TestProtocolHandlerRegistryDelegate& operator=(
const TestProtocolHandlerRegistryDelegate& other) = delete;
// ProtocolHandlerRegistry::Delegate:
void RegisterExternalHandler(const std::string& protocol) override;
void DeregisterExternalHandler(const std::string& protocol) override;
bool IsExternalHandlerRegistered(const std::string& protocol) override;
void RegisterWithOSAsDefaultClient(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override;
void CheckDefaultClientWithOS(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override;
private:
// Holds registered protocols.
std::set<std::string> registered_protocols_;
};
#endif // CHROME_BROWSER_CUSTOM_HANDLERS_TEST_PROTOCOL_HANDLER_REGISTRY_DELEGATE_H_
......@@ -13,6 +13,7 @@
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
......@@ -853,23 +854,9 @@ TEST_F(ContentSettingBubbleModelTest, RegisterProtocolHandler) {
EXPECT_FALSE(bubble_content.manage_text.empty());
}
class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
public:
void RegisterExternalHandler(const std::string& protocol) override {
// Overrides in order to not register the handler with the
// ChildProcessSecurityPolicy. That has persistent and unalterable
// side effects on other tests.
}
void RegisterWithOSAsDefaultClient(
const std::string& protocol,
shell_integration::DefaultWebClientWorkerCallback callback) override {
VLOG(1) << "Register With OS";
}
};
TEST_F(ContentSettingBubbleModelTest, RPHAllow) {
ProtocolHandlerRegistry registry(profile(), std::make_unique<FakeDelegate>());
ProtocolHandlerRegistry registry(
profile(), std::make_unique<TestProtocolHandlerRegistryDelegate>());
registry.InitProtocolSettings();
const GURL page_url("http://toplevel.example/");
......@@ -934,7 +921,8 @@ TEST_F(ContentSettingBubbleModelTest, RPHAllow) {
}
TEST_F(ContentSettingBubbleModelTest, RPHDefaultDone) {
ProtocolHandlerRegistry registry(profile(), std::make_unique<FakeDelegate>());
ProtocolHandlerRegistry registry(
profile(), std::make_unique<TestProtocolHandlerRegistryDelegate>());
registry.InitProtocolSettings();
const GURL page_url("http://toplevel.example/");
......
......@@ -2980,6 +2980,8 @@ test("unit_tests") {
"../browser/content_settings/sound_content_setting_observer_unittest.cc",
"../browser/content_settings/tab_specific_content_settings_unittest.cc",
"../browser/custom_handlers/protocol_handler_registry_unittest.cc",
"../browser/custom_handlers/test_protocol_handler_registry_delegate.cc",
"../browser/custom_handlers/test_protocol_handler_registry_delegate.h",
"../browser/data_reduction_proxy/data_reduction_proxy_chrome_settings_unittest.cc",
"../browser/data_reduction_proxy/data_reduction_proxy_settings_unittest_android.cc",
"../browser/download/chrome_download_manager_delegate_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment