Commit aea83152 authored by Jia's avatar Jia Committed by Commit Bot

[On-device adaptive brightness] Create an interface for ALS reader.

This CL splits the impl of ALS reader into an abstract interface (AlsReader) and
its actual implementation (AlsReaderImpl) and a fake version (FakeAlsReader).
Later CLs will use FakeAlsReader for testing.

Bug: 881215
Change-Id: I0d877c5ba8253b883349c85976729f225fc2f7b6
Reviewed-on: https://chromium-review.googlesource.com/1220847
Commit-Queue: Jia Meng <jiameng@chromium.org>
Reviewed-by: default avatarMichael Martis <martis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#591275}
parent c2d2edae
...@@ -1581,8 +1581,11 @@ source_set("chromeos") { ...@@ -1581,8 +1581,11 @@ source_set("chromeos") {
"policy/weekly_time/weekly_time_interval.h", "policy/weekly_time/weekly_time_interval.h",
"policy/wildcard_login_checker.cc", "policy/wildcard_login_checker.cc",
"policy/wildcard_login_checker.h", "policy/wildcard_login_checker.h",
"power/auto_screen_brightness/als_reader.cc",
"power/auto_screen_brightness/als_reader.h", "power/auto_screen_brightness/als_reader.h",
"power/auto_screen_brightness/als_reader_impl.cc",
"power/auto_screen_brightness/als_reader_impl.h",
"power/auto_screen_brightness/fake_als_reader.cc",
"power/auto_screen_brightness/fake_als_reader.h",
"power/cpu_data_collector.cc", "power/cpu_data_collector.cc",
"power/cpu_data_collector.h", "power/cpu_data_collector.h",
"power/extension_event_observer.cc", "power/extension_event_observer.cc",
...@@ -2256,7 +2259,8 @@ source_set("unit_tests") { ...@@ -2256,7 +2259,8 @@ source_set("unit_tests") {
"policy/weekly_time/time_utils_unittest.cc", "policy/weekly_time/time_utils_unittest.cc",
"policy/weekly_time/weekly_time_interval_unittest.cc", "policy/weekly_time/weekly_time_interval_unittest.cc",
"policy/weekly_time/weekly_time_unittest.cc", "policy/weekly_time/weekly_time_unittest.cc",
"power/auto_screen_brightness/als_reader_unittest.cc", "power/auto_screen_brightness/als_reader_impl_unittest.cc",
"power/auto_screen_brightness/fake_als_reader_unittest.cc",
"power/cpu_data_collector_unittest.cc", "power/cpu_data_collector_unittest.cc",
"power/extension_event_observer_unittest.cc", "power/extension_event_observer_unittest.cc",
"power/ml/adaptive_screen_brightness_manager_unittest.cc", "power/ml/adaptive_screen_brightness_manager_unittest.cc",
......
...@@ -5,34 +5,15 @@ ...@@ -5,34 +5,15 @@
#ifndef CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_H_ #ifndef CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_H_
#define CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_H_ #define CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_H_
#include "base/files/file_path.h" #include "base/observer_list_types.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "base/timer/timer.h"
namespace chromeos { namespace chromeos {
namespace power { namespace power {
namespace auto_screen_brightness { namespace auto_screen_brightness {
// AlsReader periodically reads lux values from the ambient light sensor (ALS) // Interface to ambient light reader.
// if powerd has been configured to use it.
class AlsReader { class AlsReader {
public: public:
// ALS file location may not be ready immediately, so we retry every
// |kAlsFileCheckingInterval| until |kMaxInitialAttempts| is reached, then
// we give up.
static constexpr base::TimeDelta kAlsFileCheckingInterval =
base::TimeDelta::FromSeconds(1);
static constexpr int kMaxInitialAttempts = 20;
// Interval for polling ambient light values.
static constexpr base::TimeDelta kAlsPollInterval =
base::TimeDelta::FromSeconds(1);
// Status of AlsReader initialization. // Status of AlsReader initialization.
enum class AlsInitStatus { enum class AlsInitStatus {
kSuccess = 0, kSuccess = 0,
...@@ -43,8 +24,8 @@ class AlsReader { ...@@ -43,8 +24,8 @@ class AlsReader {
kMaxValue = kMissingPath kMaxValue = kMissingPath
}; };
// Observers should take WeakPtr of AlsReader and remove themselves in // Observers should take WeakPtr of AlsReader and remove themselves
// observers' destructors if AlsReader hasn't be destructed. // in observers' destructors if AlsReader hasn't be destructed.
class Observer : public base::CheckedObserver { class Observer : public base::CheckedObserver {
public: public:
Observer() = default; Observer() = default;
...@@ -56,72 +37,15 @@ class AlsReader { ...@@ -56,72 +37,15 @@ class AlsReader {
DISALLOW_COPY_AND_ASSIGN(Observer); DISALLOW_COPY_AND_ASSIGN(Observer);
}; };
AlsReader(); virtual ~AlsReader() = default;
~AlsReader();
// Adds or removes an observer. // Adds or removes an observer.
void AddObserver(Observer* observer); virtual void AddObserver(Observer* observer) = 0;
void RemoveObserver(Observer* observer); virtual void RemoveObserver(Observer* observer) = 0;
// An observer can call this method to check if ALS has been properly // An observer can call this method to check if ALS has been properly
// initialized and ready to use. // initialized and ready to use.
AlsInitStatus GetInitStatus() const; virtual AlsInitStatus GetInitStatus() const = 0;
// Checks if an ALS is enabled, and if the config is valid . Also
// reads ambient light file path.
void Init();
// Sets the task runner for testing purpose.
void SetTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner);
// Sets ambient light path for testing purpose and initialize. This will cause
// all the checks to be skipped, i.e. whether ALS is enabled and if config is
// valid.
void InitForTesting(const base::FilePath& ambient_light_path);
base::WeakPtr<AlsReader> AsWeakPtr();
private:
friend class AlsReaderTest;
// Called when we've checked whether ALS is enabled.
void OnAlsEnableCheckDone(bool is_enabled);
// Called when we've checked whether ALS config is valid.
void OnAlsConfigCheckDone(bool is_config_valid);
// Called when we've tried to read ALS path. If |path| is empty, it would
// reschedule another attempt up to |kMaxInitialAttempts|.
void OnAlsPathReadAttempted(const std::string& path);
// Tries to read ALS path.
void RetryAlsPath();
// Notifies all observers with |status_| after AlsReader is initialized.
void OnInitializationComplete();
// Polls ambient light periodically and notifies all observers if a sample is
// read.
void ReadAlsPeriodically();
// This is called after ambient light (represented as |data|) is sampled. It
// parses |data| to int, notifies its observers and starts |als_timer_| for
// next sample.
void OnAlsRead(const std::string& data);
AlsInitStatus status_ = AlsInitStatus::kInProgress;
base::FilePath ambient_light_path_;
int num_failed_initialization_ = 0;
// Timer used to retry initialization and also for periodic ambient light
// sampling.
base::OneShotTimer als_timer_;
scoped_refptr<base::SequencedTaskRunner> als_task_runner_;
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<AlsReader> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AlsReader);
}; };
} // namespace auto_screen_brightness } // namespace auto_screen_brightness
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader.h" #include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader_impl.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
...@@ -101,45 +101,49 @@ std::string ReadAlsFromFile(const base::FilePath& ambient_light_path) { ...@@ -101,45 +101,49 @@ std::string ReadAlsFromFile(const base::FilePath& ambient_light_path) {
} }
} // namespace } // namespace
constexpr base::TimeDelta AlsReader::kAlsFileCheckingInterval; constexpr base::TimeDelta AlsReaderImpl::kAlsFileCheckingInterval;
constexpr int AlsReader::kMaxInitialAttempts; constexpr int AlsReaderImpl::kMaxInitialAttempts;
constexpr base::TimeDelta AlsReader::kAlsPollInterval; constexpr base::TimeDelta AlsReaderImpl::kAlsPollInterval;
AlsReader::AlsReader() AlsReaderImpl::AlsReaderImpl()
: als_task_runner_(base::CreateSequencedTaskRunnerWithTraits( : als_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
{base::TaskPriority::BEST_EFFORT, base::MayBlock(), {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
AlsReader::~AlsReader() = default; AlsReaderImpl::~AlsReaderImpl() = default;
void AlsReader::AddObserver(Observer* const observer) { void AlsReaderImpl::AddObserver(Observer* const observer) {
DCHECK(observer); DCHECK(observer);
observers_.AddObserver(observer); observers_.AddObserver(observer);
} }
void AlsReader::RemoveObserver(Observer* const observer) { void AlsReaderImpl::RemoveObserver(Observer* const observer) {
DCHECK(observer); DCHECK(observer);
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
AlsReader::AlsInitStatus AlsReader::GetInitStatus() const { AlsReader::AlsInitStatus AlsReaderImpl::GetInitStatus() const {
return status_; return status_;
} }
void AlsReader::Init() { void AlsReaderImpl::Init() {
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
als_task_runner_.get(), FROM_HERE, base::BindOnce(&IsAlsEnabled), als_task_runner_.get(), FROM_HERE, base::BindOnce(&IsAlsEnabled),
base::BindOnce(&AlsReader::OnAlsEnableCheckDone, AsWeakPtr())); base::BindOnce(&AlsReaderImpl::OnAlsEnableCheckDone, AsWeakPtr()));
} }
void AlsReader::SetTaskRunnerForTesting( base::WeakPtr<AlsReaderImpl> AlsReaderImpl::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void AlsReaderImpl::SetTaskRunnerForTesting(
const scoped_refptr<base::SequencedTaskRunner> task_runner) { const scoped_refptr<base::SequencedTaskRunner> task_runner) {
als_task_runner_ = task_runner; als_task_runner_ = task_runner;
als_timer_.SetTaskRunner(task_runner); als_timer_.SetTaskRunner(task_runner);
} }
void AlsReader::InitForTesting(const base::FilePath& ambient_light_path) { void AlsReaderImpl::InitForTesting(const base::FilePath& ambient_light_path) {
DCHECK(!ambient_light_path.empty()); DCHECK(!ambient_light_path.empty());
ambient_light_path_ = ambient_light_path; ambient_light_path_ = ambient_light_path;
status_ = AlsInitStatus::kSuccess; status_ = AlsInitStatus::kSuccess;
...@@ -147,11 +151,7 @@ void AlsReader::InitForTesting(const base::FilePath& ambient_light_path) { ...@@ -147,11 +151,7 @@ void AlsReader::InitForTesting(const base::FilePath& ambient_light_path) {
ReadAlsPeriodically(); ReadAlsPeriodically();
} }
base::WeakPtr<AlsReader> AlsReader::AsWeakPtr() { void AlsReaderImpl::OnAlsEnableCheckDone(const bool is_enabled) {
return weak_ptr_factory_.GetWeakPtr();
}
void AlsReader::OnAlsEnableCheckDone(const bool is_enabled) {
if (!is_enabled) { if (!is_enabled) {
status_ = AlsInitStatus::kDisabled; status_ = AlsInitStatus::kDisabled;
OnInitializationComplete(); OnInitializationComplete();
...@@ -160,10 +160,10 @@ void AlsReader::OnAlsEnableCheckDone(const bool is_enabled) { ...@@ -160,10 +160,10 @@ void AlsReader::OnAlsEnableCheckDone(const bool is_enabled) {
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
als_task_runner_.get(), FROM_HERE, base::BindOnce(&VerifyAlsConfig), als_task_runner_.get(), FROM_HERE, base::BindOnce(&VerifyAlsConfig),
base::BindOnce(&AlsReader::OnAlsConfigCheckDone, AsWeakPtr())); base::BindOnce(&AlsReaderImpl::OnAlsConfigCheckDone, AsWeakPtr()));
} }
void AlsReader::OnAlsConfigCheckDone(const bool is_config_valid) { void AlsReaderImpl::OnAlsConfigCheckDone(const bool is_config_valid) {
if (!is_config_valid) { if (!is_config_valid) {
status_ = AlsInitStatus::kIncorrectConfig; status_ = AlsInitStatus::kIncorrectConfig;
OnInitializationComplete(); OnInitializationComplete();
...@@ -173,7 +173,7 @@ void AlsReader::OnAlsConfigCheckDone(const bool is_config_valid) { ...@@ -173,7 +173,7 @@ void AlsReader::OnAlsConfigCheckDone(const bool is_config_valid) {
RetryAlsPath(); RetryAlsPath();
} }
void AlsReader::OnAlsPathReadAttempted(const std::string& path) { void AlsReaderImpl::OnAlsPathReadAttempted(const std::string& path) {
if (!path.empty()) { if (!path.empty()) {
ambient_light_path_ = base::FilePath(path); ambient_light_path_ = base::FilePath(path);
status_ = AlsInitStatus::kSuccess; status_ = AlsInitStatus::kSuccess;
...@@ -191,29 +191,29 @@ void AlsReader::OnAlsPathReadAttempted(const std::string& path) { ...@@ -191,29 +191,29 @@ void AlsReader::OnAlsPathReadAttempted(const std::string& path) {
} }
als_timer_.Start(FROM_HERE, kAlsFileCheckingInterval, this, als_timer_.Start(FROM_HERE, kAlsFileCheckingInterval, this,
&AlsReader::RetryAlsPath); &AlsReaderImpl::RetryAlsPath);
} }
void AlsReader::RetryAlsPath() { void AlsReaderImpl::RetryAlsPath() {
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
als_task_runner_.get(), FROM_HERE, base::BindOnce(&GetAlsPath), als_task_runner_.get(), FROM_HERE, base::BindOnce(&GetAlsPath),
base::BindOnce(&AlsReader::OnAlsPathReadAttempted, AsWeakPtr())); base::BindOnce(&AlsReaderImpl::OnAlsPathReadAttempted, AsWeakPtr()));
} }
void AlsReader::OnInitializationComplete() { void AlsReaderImpl::OnInitializationComplete() {
DCHECK_NE(status_, AlsInitStatus::kInProgress); DCHECK_NE(status_, AlsInitStatus::kInProgress);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.OnAlsReaderInitialized(status_); observer.OnAlsReaderInitialized(status_);
} }
void AlsReader::ReadAlsPeriodically() { void AlsReaderImpl::ReadAlsPeriodically() {
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
als_task_runner_.get(), FROM_HERE, als_task_runner_.get(), FROM_HERE,
base::BindOnce(&ReadAlsFromFile, ambient_light_path_), base::BindOnce(&ReadAlsFromFile, ambient_light_path_),
base::BindOnce(&AlsReader::OnAlsRead, AsWeakPtr())); base::BindOnce(&AlsReaderImpl::OnAlsRead, AsWeakPtr()));
} }
void AlsReader::OnAlsRead(const std::string& data) { void AlsReaderImpl::OnAlsRead(const std::string& data) {
std::string trimmed_data; std::string trimmed_data;
base::TrimWhitespaceASCII(data, base::TRIM_ALL, &trimmed_data); base::TrimWhitespaceASCII(data, base::TRIM_ALL, &trimmed_data);
int value = 0; int value = 0;
...@@ -222,7 +222,7 @@ void AlsReader::OnAlsRead(const std::string& data) { ...@@ -222,7 +222,7 @@ void AlsReader::OnAlsRead(const std::string& data) {
observer.OnAmbientLightUpdated(value); observer.OnAmbientLightUpdated(value);
} }
als_timer_.Start(FROM_HERE, kAlsPollInterval, this, als_timer_.Start(FROM_HERE, kAlsPollInterval, this,
&AlsReader::ReadAlsPeriodically); &AlsReaderImpl::ReadAlsPeriodically);
} }
} // namespace auto_screen_brightness } // namespace auto_screen_brightness
......
// 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 CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_IMPL_H_
#define CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_IMPL_H_
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "base/timer/timer.h"
#include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader.h"
namespace chromeos {
namespace power {
namespace auto_screen_brightness {
// Real implementation of AlsReader.
// It periodically reads lux values from the ambient light sensor (ALS)
// if powerd has been configured to use it.
class AlsReaderImpl : public AlsReader {
public:
// ALS file location may not be ready immediately, so we retry every
// |kAlsFileCheckingInterval| until |kMaxInitialAttempts| is reached, then
// we give up.
static constexpr base::TimeDelta kAlsFileCheckingInterval =
base::TimeDelta::FromSeconds(1);
static constexpr int kMaxInitialAttempts = 20;
// Interval for polling ambient light values.
// TODO(jiameng): revise frequency.
static constexpr base::TimeDelta kAlsPollInterval =
base::TimeDelta::FromSeconds(1);
AlsReaderImpl();
~AlsReaderImpl() override;
// AlsReader overrides:
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
AlsInitStatus GetInitStatus() const override;
// Checks if an ALS is enabled, and if the config is valid . Also
// reads ambient light file path.
void Init();
base::WeakPtr<AlsReaderImpl> AsWeakPtr();
// Sets the task runner for testing purpose.
void SetTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner);
// Sets ambient light path for testing purpose and initialize. This will cause
// all the checks to be skipped, i.e. whether ALS is enabled and if config is
// valid.
void InitForTesting(const base::FilePath& ambient_light_path);
private:
friend class AlsReaderImplTest;
// Called when we've checked whether ALS is enabled.
void OnAlsEnableCheckDone(bool is_enabled);
// Called when we've checked whether ALS config is valid.
void OnAlsConfigCheckDone(bool is_config_valid);
// Called when we've tried to read ALS path. If |path| is empty, it would
// reschedule another attempt up to |kMaxInitialAttempts|.
void OnAlsPathReadAttempted(const std::string& path);
// Tries to read ALS path.
void RetryAlsPath();
// Notifies all observers with |status_| after AlsReaderImpl is initialized.
void OnInitializationComplete();
// Polls ambient light periodically and notifies all observers if a sample is
// read.
void ReadAlsPeriodically();
// This is called after ambient light (represented as |data|) is sampled. It
// parses |data| to int, notifies its observers and starts |als_timer_| for
// next sample.
void OnAlsRead(const std::string& data);
AlsInitStatus status_ = AlsInitStatus::kInProgress;
base::FilePath ambient_light_path_;
int num_failed_initialization_ = 0;
// Timer used to retry initialization and also for periodic ambient light
// sampling.
base::OneShotTimer als_timer_;
scoped_refptr<base::SequencedTaskRunner> als_task_runner_;
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<AlsReaderImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AlsReaderImpl);
};
} // namespace auto_screen_brightness
} // namespace power
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_ALS_READER_IMPL_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader.h" #include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader_impl.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
...@@ -50,9 +50,9 @@ class TestObserver : public AlsReader::Observer { ...@@ -50,9 +50,9 @@ class TestObserver : public AlsReader::Observer {
}; };
} // namespace } // namespace
class AlsReaderTest : public testing::Test { class AlsReaderImplTest : public testing::Test {
public: public:
AlsReaderTest() AlsReaderImplTest()
: scoped_task_environment_( : scoped_task_environment_(
std::make_unique<base::test::ScopedTaskEnvironment>( std::make_unique<base::test::ScopedTaskEnvironment>(
base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME)) { base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME)) {
...@@ -63,7 +63,7 @@ class AlsReaderTest : public testing::Test { ...@@ -63,7 +63,7 @@ class AlsReaderTest : public testing::Test {
als_reader_.InitForTesting(ambient_light_path_); als_reader_.InitForTesting(ambient_light_path_);
} }
~AlsReaderTest() override = default; ~AlsReaderImplTest() override { als_reader_.RemoveObserver(&test_observer_); }
protected: protected:
void WriteLux(int lux) { void WriteLux(int lux) {
...@@ -81,39 +81,39 @@ class AlsReaderTest : public testing::Test { ...@@ -81,39 +81,39 @@ class AlsReaderTest : public testing::Test {
std::unique_ptr<base::test::ScopedTaskEnvironment> scoped_task_environment_; std::unique_ptr<base::test::ScopedTaskEnvironment> scoped_task_environment_;
TestObserver test_observer_; TestObserver test_observer_;
AlsReader als_reader_; AlsReaderImpl als_reader_;
private: private:
DISALLOW_COPY_AND_ASSIGN(AlsReaderTest); DISALLOW_COPY_AND_ASSIGN(AlsReaderImplTest);
}; };
TEST_F(AlsReaderTest, CheckInitStatusAlsFileFound) { TEST_F(AlsReaderImplTest, CheckInitStatusAlsFileFound) {
EXPECT_EQ(AlsReader::AlsInitStatus::kSuccess, als_reader_.GetInitStatus()); EXPECT_EQ(AlsReader::AlsInitStatus::kSuccess, als_reader_.GetInitStatus());
} }
TEST_F(AlsReaderTest, OnAlsReaderInitialized) { TEST_F(AlsReaderImplTest, OnAlsReaderInitialized) {
EXPECT_EQ(AlsReader::AlsInitStatus::kSuccess, test_observer_.get_status()); EXPECT_EQ(AlsReader::AlsInitStatus::kSuccess, test_observer_.get_status());
} }
TEST_F(AlsReaderTest, OneAlsValue) { TEST_F(AlsReaderImplTest, OneAlsValue) {
WriteLux(10); WriteLux(10);
scoped_task_environment_->RunUntilIdle(); scoped_task_environment_->RunUntilIdle();
EXPECT_EQ(10, test_observer_.get_ambient_light()); EXPECT_EQ(10, test_observer_.get_ambient_light());
EXPECT_EQ(1, test_observer_.get_num_received_ambient_lights()); EXPECT_EQ(1, test_observer_.get_num_received_ambient_lights());
} }
TEST_F(AlsReaderTest, TwoAlsValues) { TEST_F(AlsReaderImplTest, TwoAlsValues) {
WriteLux(10); WriteLux(10);
// Ambient light is read immediately after initialization, and then // Ambient light is read immediately after initialization, and then
// periodically every |kAlsPollInterval|. Below we move time for half of // periodically every |kAlsPollInterval|. Below we move time for half of
// |kAlsPollInterval| to ensure there is only one reading attempt. // |kAlsPollInterval| to ensure there is only one reading attempt.
scoped_task_environment_->FastForwardBy(AlsReader::kAlsPollInterval / 2); scoped_task_environment_->FastForwardBy(AlsReaderImpl::kAlsPollInterval / 2);
EXPECT_EQ(10, test_observer_.get_ambient_light()); EXPECT_EQ(10, test_observer_.get_ambient_light());
EXPECT_EQ(1, test_observer_.get_num_received_ambient_lights()); EXPECT_EQ(1, test_observer_.get_num_received_ambient_lights());
WriteLux(20); WriteLux(20);
// Now move time for another |kAlsPollInterval| to trigger another read. // Now move time for another |kAlsPollInterval| to trigger another read.
scoped_task_environment_->FastForwardBy(AlsReader::kAlsPollInterval); scoped_task_environment_->FastForwardBy(AlsReaderImpl::kAlsPollInterval);
EXPECT_EQ(20, test_observer_.get_ambient_light()); EXPECT_EQ(20, test_observer_.get_ambient_light());
EXPECT_EQ(2, test_observer_.get_num_received_ambient_lights()); EXPECT_EQ(2, test_observer_.get_num_received_ambient_lights());
} }
......
// 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 "chrome/browser/chromeos/power/auto_screen_brightness/fake_als_reader.h"
namespace chromeos {
namespace power {
namespace auto_screen_brightness {
FakeAlsReader::FakeAlsReader() : weak_ptr_factory_(this) {}
FakeAlsReader::~FakeAlsReader() = default;
void FakeAlsReader::ReportAmbientLightUpdate(const int lux) {
for (auto& observer : observers_)
observer.OnAmbientLightUpdated(lux);
}
void FakeAlsReader::ReportReaderInitialized() {
for (auto& observer : observers_)
observer.OnAlsReaderInitialized(status_);
}
AlsReader::AlsInitStatus FakeAlsReader::GetInitStatus() const {
return status_;
}
void FakeAlsReader::AddObserver(Observer* const observer) {
DCHECK(observer);
observers_.AddObserver(observer);
}
void FakeAlsReader::RemoveObserver(Observer* const observer) {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
base::WeakPtr<FakeAlsReader> FakeAlsReader::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace auto_screen_brightness
} // namespace power
} // 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 CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_FAKE_ALS_READER_H_
#define CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_FAKE_ALS_READER_H_
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chrome/browser/chromeos/power/auto_screen_brightness/als_reader.h"
namespace chromeos {
namespace power {
namespace auto_screen_brightness {
// This is a fake AlsReader used for testing only.
class FakeAlsReader : public AlsReader {
public:
FakeAlsReader();
~FakeAlsReader() override;
void set_als_init_status(AlsInitStatus status) { status_ = status; }
void ReportReaderInitialized();
void ReportAmbientLightUpdate(int lux);
// AlsReader overrides:
AlsInitStatus GetInitStatus() const override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
base::WeakPtr<FakeAlsReader> AsWeakPtr();
private:
AlsInitStatus status_ = AlsInitStatus::kInProgress;
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<FakeAlsReader> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeAlsReader);
};
} // namespace auto_screen_brightness
} // namespace power
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_POWER_AUTO_SCREEN_BRIGHTNESS_FAKE_ALS_READER_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 "chrome/browser/chromeos/power/auto_screen_brightness/fake_als_reader.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace power {
namespace auto_screen_brightness {
namespace {
class TestObserver : public AlsReader::Observer {
public:
TestObserver() {}
~TestObserver() override = default;
// AlsReader::Observer overrides:
void OnAmbientLightUpdated(const int lux) override {
ambient_light_ = lux;
++num_received_ambient_lights_;
}
void OnAlsReaderInitialized(const AlsReader::AlsInitStatus status) override {
status_ = base::Optional<AlsReader::AlsInitStatus>(status);
}
int get_ambient_light() const { return ambient_light_; }
int get_num_received_ambient_lights() const {
return num_received_ambient_lights_;
}
AlsReader::AlsInitStatus get_status() const {
CHECK(status_);
return status_.value();
}
private:
int ambient_light_ = -1;
int num_received_ambient_lights_ = 0;
base::Optional<AlsReader::AlsInitStatus> status_;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
} // namespace
class FakeAlsReaderTest : public testing::Test {
public:
FakeAlsReaderTest() { fake_als_reader_.AddObserver(&test_observer_); }
~FakeAlsReaderTest() override {
fake_als_reader_.RemoveObserver(&test_observer_);
};
protected:
TestObserver test_observer_;
FakeAlsReader fake_als_reader_;
private:
DISALLOW_COPY_AND_ASSIGN(FakeAlsReaderTest);
};
TEST_F(FakeAlsReaderTest, GetInitStatus) {
fake_als_reader_.set_als_init_status(AlsReader::AlsInitStatus::kDisabled);
EXPECT_EQ(AlsReader::AlsInitStatus::kDisabled,
fake_als_reader_.GetInitStatus());
}
TEST_F(FakeAlsReaderTest, ReportReaderInitialized) {
fake_als_reader_.set_als_init_status(AlsReader::AlsInitStatus::kSuccess);
fake_als_reader_.ReportReaderInitialized();
EXPECT_EQ(AlsReader::AlsInitStatus::kSuccess, test_observer_.get_status());
}
TEST_F(FakeAlsReaderTest, ReportAmbientLightUpdate) {
fake_als_reader_.ReportAmbientLightUpdate(10);
EXPECT_EQ(10, test_observer_.get_ambient_light());
EXPECT_EQ(1, test_observer_.get_num_received_ambient_lights());
}
} // namespace auto_screen_brightness
} // namespace power
} // namespace chromeos
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