Commit a7947310 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Create TimerFactory and associated test doubles.

This is necessary for a follow-up CL which much inject a
FakeTimerFactory.

Bug: 824568, 752273
Change-Id: I3575778517c5768074e970604f95729c1fb761ae
Reviewed-on: https://chromium-review.googlesource.com/1091657
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#565474}
parent f3ae11fc
...@@ -82,6 +82,9 @@ static_library("secure_channel") { ...@@ -82,6 +82,9 @@ static_library("secure_channel") {
"single_client_message_proxy.h", "single_client_message_proxy.h",
"single_client_message_proxy_impl.cc", "single_client_message_proxy_impl.cc",
"single_client_message_proxy_impl.h", "single_client_message_proxy_impl.h",
"timer_factory.h",
"timer_factory_impl.cc",
"timer_factory_impl.h",
] ]
deps = [ deps = [
...@@ -133,6 +136,8 @@ static_library("test_support") { ...@@ -133,6 +136,8 @@ static_library("test_support") {
"fake_message_receiver.h", "fake_message_receiver.h",
"fake_multiplexed_channel.cc", "fake_multiplexed_channel.cc",
"fake_multiplexed_channel.h", "fake_multiplexed_channel.h",
"fake_one_shot_timer.cc",
"fake_one_shot_timer.h",
"fake_pending_connection_manager.cc", "fake_pending_connection_manager.cc",
"fake_pending_connection_manager.h", "fake_pending_connection_manager.h",
"fake_pending_connection_request.cc", "fake_pending_connection_request.cc",
...@@ -141,6 +146,8 @@ static_library("test_support") { ...@@ -141,6 +146,8 @@ static_library("test_support") {
"fake_pending_connection_request_delegate.h", "fake_pending_connection_request_delegate.h",
"fake_single_client_message_proxy.cc", "fake_single_client_message_proxy.cc",
"fake_single_client_message_proxy.h", "fake_single_client_message_proxy.h",
"fake_timer_factory.cc",
"fake_timer_factory.h",
] ]
deps = [ deps = [
......
// 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 "chromeos/services/secure_channel/ble_advertiser_impl.h"
#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace secure_channel {
namespace {} // namespace
class SecureChannelBleAdvertiserImplTest : public testing::Test {
protected:
SecureChannelBleAdvertiserImplTest() {}
~SecureChannelBleAdvertiserImplTest() override = default;
// testing::Test:
void SetUp() override {}
private:
private:
DISALLOW_COPY_AND_ASSIGN(SecureChannelBleAdvertiserImplTest);
};
TEST_F(SecureChannelBleAdvertiserImplTest, UrelatedScanResults) {}
} // namespace secure_channel
} // namespace chromeos
// 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 "chromeos/services/secure_channel/fake_one_shot_timer.h"
#include "base/callback.h"
namespace chromeos {
namespace secure_channel {
FakeOneShotTimer::FakeOneShotTimer(
base::OnceCallback<void(const base::UnguessableToken&)> destructor_callback)
: base::MockTimer(true /* retain_user_task */, false /* is_repeating */),
destructor_callback_(std::move(destructor_callback)),
id_(base::UnguessableToken::Create()) {}
FakeOneShotTimer::~FakeOneShotTimer() {
std::move(destructor_callback_).Run(id_);
}
} // namespace secure_channel
} // namespace chromeos
// 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 CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_ONE_SHOT_TIMER_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_ONE_SHOT_TIMER_H_
#include <memory>
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/timer/mock_timer.h"
#include "base/unguessable_token.h"
namespace chromeos {
namespace secure_channel {
// Fake base::Timer implementation, which extends MockTimer and provides a
// mechanism for alerting its creator when it is destroyed.
class FakeOneShotTimer : public base::MockTimer {
public:
FakeOneShotTimer(base::OnceCallback<void(const base::UnguessableToken&)>
destructor_callback);
~FakeOneShotTimer() override;
const base::UnguessableToken& id() const { return id_; }
private:
base::OnceCallback<void(const base::UnguessableToken&)> destructor_callback_;
base::UnguessableToken id_;
DISALLOW_COPY_AND_ASSIGN(FakeOneShotTimer);
};
} // namespace secure_channel
} // namespace chromeos
#endif // CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_ONE_SHOT_TIMER_H_
// 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 "chromeos/services/secure_channel/fake_timer_factory.h"
#include "base/bind.h"
#include "base/callback.h"
#include "chromeos/services/secure_channel/fake_one_shot_timer.h"
namespace chromeos {
namespace secure_channel {
FakeTimerFactory::FakeTimerFactory() : weak_ptr_factory_(this) {}
FakeTimerFactory::~FakeTimerFactory() = default;
std::unique_ptr<base::Timer> FakeTimerFactory::CreateOneShotTimer() {
auto fake_one_shot_timer = std::make_unique<FakeOneShotTimer>(
base::BindOnce(&FakeTimerFactory::OnOneShotTimerDeleted,
weak_ptr_factory_.GetWeakPtr()));
id_for_last_created_one_shot_timer_ = fake_one_shot_timer->id();
id_to_active_one_shot_timer_map_[fake_one_shot_timer->id()] =
fake_one_shot_timer.get();
return fake_one_shot_timer;
}
void FakeTimerFactory::OnOneShotTimerDeleted(
const base::UnguessableToken& deleted_timer_id) {
size_t num_deleted = id_to_active_one_shot_timer_map_.erase(deleted_timer_id);
DCHECK_EQ(1u, num_deleted);
}
} // namespace secure_channel
} // namespace chromeos
// 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 CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_TIMER_FACTORY_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_TIMER_FACTORY_H_
#include <memory>
#include "base/containers/flat_map.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/unguessable_token.h"
#include "chromeos/services/secure_channel/timer_factory.h"
namespace chromeos {
namespace secure_channel {
class FakeOneShotTimer;
// Test TimerFactory implementation, which returns FakeOneShotTimer objects.
class FakeTimerFactory : public TimerFactory {
public:
FakeTimerFactory();
~FakeTimerFactory() override;
const base::UnguessableToken& id_for_last_created_one_shot_timer() {
return id_for_last_created_one_shot_timer_;
}
base::flat_map<base::UnguessableToken, FakeOneShotTimer*>&
id_to_active_one_shot_timer_map() {
return id_to_active_one_shot_timer_map_;
}
private:
// TimerFactory:
std::unique_ptr<base::Timer> CreateOneShotTimer() override;
void OnOneShotTimerDeleted(const base::UnguessableToken& deleted_timer_id);
base::UnguessableToken id_for_last_created_one_shot_timer_;
base::flat_map<base::UnguessableToken, FakeOneShotTimer*>
id_to_active_one_shot_timer_map_;
base::WeakPtrFactory<FakeTimerFactory> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeTimerFactory);
};
} // namespace secure_channel
} // namespace chromeos
#endif // CHROMEOS_SERVICES_SECURE_CHANNEL_FAKE_TIMER_FACTORY_H_
// 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 CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_H_
#include <memory>
#include "base/macros.h"
namespace base {
class Timer;
} // namespace base
namespace chromeos {
namespace secure_channel {
// Creates timers. This class is needed so that tests can inject test doubles
// for timers.
class TimerFactory {
public:
virtual ~TimerFactory() = default;
virtual std::unique_ptr<base::Timer> CreateOneShotTimer() = 0;
protected:
TimerFactory() = default;
DISALLOW_COPY_AND_ASSIGN(TimerFactory);
};
} // namespace secure_channel
} // namespace chromeos
#endif // CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_H_
// 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 "chromeos/services/secure_channel/timer_factory_impl.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/timer/timer.h"
namespace chromeos {
namespace secure_channel {
// static
TimerFactoryImpl::Factory* TimerFactoryImpl::Factory::test_factory_ = nullptr;
// static
TimerFactoryImpl::Factory* TimerFactoryImpl::Factory::Get() {
if (test_factory_)
return test_factory_;
static base::NoDestructor<Factory> factory;
return factory.get();
}
// static
void TimerFactoryImpl::Factory::SetFactoryForTesting(Factory* test_factory) {
test_factory_ = test_factory;
}
TimerFactoryImpl::Factory::~Factory() = default;
std::unique_ptr<TimerFactory> TimerFactoryImpl::Factory::BuildInstance() {
return base::WrapUnique(new TimerFactoryImpl());
}
TimerFactoryImpl::TimerFactoryImpl() = default;
;
TimerFactoryImpl::~TimerFactoryImpl() = default;
std::unique_ptr<base::Timer> TimerFactoryImpl::CreateOneShotTimer() {
return std::make_unique<base::OneShotTimer>();
}
} // namespace secure_channel
} // namespace chromeos
// 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 CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_IMPL_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_IMPL_H_
#include <memory>
#include "base/macros.h"
#include "chromeos/services/secure_channel/timer_factory.h"
namespace base {
class Timer;
} // namespace base
namespace chromeos {
namespace secure_channel {
// Concrete TimerFactory implementation, which returns base::OneShotTimer
// objects.
class TimerFactoryImpl : public TimerFactory {
public:
class Factory {
public:
static Factory* Get();
static void SetFactoryForTesting(Factory* test_factory);
virtual ~Factory();
virtual std::unique_ptr<TimerFactory> BuildInstance();
private:
static Factory* test_factory_;
};
~TimerFactoryImpl() override;
private:
TimerFactoryImpl();
// TimerFactory:
std::unique_ptr<base::Timer> CreateOneShotTimer() override;
DISALLOW_COPY_AND_ASSIGN(TimerFactoryImpl);
};
} // namespace secure_channel
} // namespace chromeos
#endif // CHROMEOS_SERVICES_SECURE_CHANNEL_TIMER_FACTORY_IMPL_H_
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