Commit d3451304 authored by Olga Sharonova's avatar Olga Sharonova Committed by Commit Bot

Test support for mocking audio::SystemInfo

This is another spin-off from WIP
https://chromium-review.googlesource.com/c/chromium/src/+/875927

See chrome/browser/chromeos/login/kiosk_browsertest.cc there for a use case.

Another (potential) use case is Speech Recognition browser tests here
https://cs.chromium.org/chromium/src/content/browser/speech/speech_recognition_browsertest.cc?type=cs&sq=package:chromium&l=132

Bug: 792441
Change-Id: I7c8b89bb1fc920ccade625dfc259e8c91edc8e94
Reviewed-on: https://chromium-review.googlesource.com/891184
Commit-Queue: Olga Sharonova <olka@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Reviewed-by: default avatarMax Morin <maxmorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533621}
parent bfb91472
......@@ -46,6 +46,7 @@ source_set("tests") {
"//base/test:test_support",
"//media:test_support",
"//services/audio/public/cpp",
"//services/audio/public/cpp:test_support",
"//services/audio/public/interfaces",
"//services/service_manager/public/cpp",
"//services/service_manager/public/cpp:service_test_support",
......
......@@ -2,4 +2,5 @@ include_rules = [
"+media/audio",
"+media/base",
"+media/mojo",
"+services/audio/public",
]
......@@ -15,3 +15,19 @@ source_set("cpp") {
"//services/service_manager/public/cpp",
]
}
source_set("test_support") {
testonly = true
sources = [
"fake_system_info.cc",
"fake_system_info.h",
]
public_deps = [
"//base",
"//media",
"//services/audio/public/interfaces",
"//services/service_manager/public/cpp",
]
}
// 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 "services/audio/public/cpp/fake_system_info.h"
#include "services/audio/public/interfaces/constants.mojom.h"
#include "services/service_manager/public/cpp/bind_source_info.h"
#include "services/service_manager/public/cpp/service_context.h"
namespace audio {
FakeSystemInfo::FakeSystemInfo() {}
FakeSystemInfo::~FakeSystemInfo() {}
// static
void FakeSystemInfo::OverrideGlobalBinderForAudioService(
FakeSystemInfo* fake_system_info) {
service_manager::ServiceContext::SetGlobalBinderForTesting(
mojom::kServiceName, mojom::SystemInfo::Name_,
base::BindRepeating(&FakeSystemInfo::Bind,
base::Unretained(fake_system_info)));
}
void FakeSystemInfo::GetInputStreamParameters(
const std::string& device_id,
GetInputStreamParametersCallback callback) {
std::move(callback).Run(base::nullopt);
}
void FakeSystemInfo::GetOutputStreamParameters(
const std::string& device_id,
GetOutputStreamParametersCallback callback) {
std::move(callback).Run(base::nullopt);
}
void FakeSystemInfo::HasInputDevices(HasInputDevicesCallback callback) {
std::move(callback).Run(false);
}
void FakeSystemInfo::HasOutputDevices(HasOutputDevicesCallback callback) {
std::move(callback).Run(false);
}
void FakeSystemInfo::GetInputDeviceDescriptions(
GetInputDeviceDescriptionsCallback callback) {
std::move(callback).Run(media::AudioDeviceDescriptions());
}
void FakeSystemInfo::GetOutputDeviceDescriptions(
GetOutputDeviceDescriptionsCallback callback) {
std::move(callback).Run(media::AudioDeviceDescriptions());
}
void FakeSystemInfo::GetAssociatedOutputDeviceID(
const std::string& input_device_id,
GetAssociatedOutputDeviceIDCallback callback) {
std::move(callback).Run(base::nullopt);
}
void FakeSystemInfo::GetInputDeviceInfo(const std::string& input_device_id,
GetInputDeviceInfoCallback callback) {
std::move(callback).Run(base::nullopt, base::nullopt);
}
void FakeSystemInfo::Bind(const std::string& interface_name,
mojo::ScopedMessagePipeHandle handle,
const service_manager::BindSourceInfo& source_info) {
DCHECK(interface_name == mojom::SystemInfo::Name_);
bindings_.AddBinding(this, mojom::SystemInfoRequest(std::move(handle)));
}
} // namespace audio
// 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 SERVICES_AUDIO_PUBLIC_CPP_FAKE_SYSTEM_INFO_H_
#define SERVICES_AUDIO_PUBLIC_CPP_FAKE_SYSTEM_INFO_H_
#include <string>
#include "base/macros.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/audio/public/interfaces/system_info.mojom.h"
namespace service_manager {
struct BindSourceInfo;
}
namespace audio {
// An instance of this class can be used to override the global binding for
// audio::SystemInfo. By default it behaves as if the system has no audio
// devices. Inherit from it to override the behavior.
class FakeSystemInfo : public mojom::SystemInfo {
public:
FakeSystemInfo();
~FakeSystemInfo() override;
// See ServiceContext::ClearGlobalBindersForTesting() to clear it if needed.
static void OverrideGlobalBinderForAudioService(
FakeSystemInfo* fake_system_info);
protected:
// audio::mojom::SystemInfo implementation.
void GetInputStreamParameters(
const std::string& device_id,
GetInputStreamParametersCallback callback) override;
void GetOutputStreamParameters(
const std::string& device_id,
GetOutputStreamParametersCallback callback) override;
void HasInputDevices(HasInputDevicesCallback callback) override;
void HasOutputDevices(HasOutputDevicesCallback callback) override;
void GetInputDeviceDescriptions(
GetInputDeviceDescriptionsCallback callback) override;
void GetOutputDeviceDescriptions(
GetOutputDeviceDescriptionsCallback callback) override;
void GetAssociatedOutputDeviceID(
const std::string& input_device_id,
GetAssociatedOutputDeviceIDCallback callback) override;
void GetInputDeviceInfo(const std::string& input_device_id,
GetInputDeviceInfoCallback callback) override;
private:
void Bind(const std::string& interface_name,
mojo::ScopedMessagePipeHandle handle,
const service_manager::BindSourceInfo& source_info);
mojo::BindingSet<mojom::SystemInfo> bindings_;
DISALLOW_COPY_AND_ASSIGN(FakeSystemInfo);
};
} // namespace audio
#endif // SERVICES_AUDIO_PUBLIC_CPP_FAKE_SYSTEM_INFO_H_
......@@ -6,6 +6,7 @@
#include "services/audio/in_process_audio_manager_accessor.h"
#include "services/audio/service.h"
#include "services/service_manager/public/cpp/service.h"
namespace audio {
......
......@@ -7,8 +7,9 @@
#include <memory>
#include "base/macros.h"
#include "services/service_manager/public/cpp/service.h"
namespace service_manager {
class Service;
}
namespace media {
class AudioManager;
......
......@@ -7,6 +7,7 @@
#include "media/audio/test_audio_thread.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/audio/public/cpp/audio_system_to_service_adapter.h"
#include "services/audio/public/cpp/fake_system_info.h"
#include "services/audio/public/interfaces/constants.mojom.h"
#include "services/audio/service_factory.h"
#include "services/service_manager/public/cpp/service_context.h"
......@@ -155,6 +156,36 @@ class InProcessServiceTest : public service_manager::test::ServiceTest {
DISALLOW_COPY_AND_ASSIGN(InProcessServiceTest);
};
// Tests for FakeSystemInfo overriding the global binder.
class FakeSystemInfoTest : public InProcessServiceTest<false>,
public FakeSystemInfo {
public:
FakeSystemInfoTest() {}
~FakeSystemInfoTest() override {}
protected:
MOCK_METHOD0(MethodCalled, void());
private:
void HasInputDevices(HasInputDevicesCallback callback) override {
std::move(callback).Run(true);
MethodCalled();
}
DISALLOW_COPY_AND_ASSIGN(FakeSystemInfoTest);
};
TEST_F(FakeSystemInfoTest, HasInputDevicesCalledOnGlobalBinderOverride) {
FakeSystemInfo::OverrideGlobalBinderForAudioService(this);
base::RunLoop wait_loop;
EXPECT_CALL(*this, MethodCalled())
.WillOnce(testing::Invoke(&wait_loop, &base::RunLoop::Quit));
audio_system()->HasInputDevices(base::BindOnce([](bool) {}));
wait_loop.Run();
service_manager::ServiceContext::ClearGlobalBindersForTesting(
mojom::kServiceName);
}
} // namespace audio
// AudioSystem interface conformance tests.
......
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