| Index: chromeos/system/cpu_temperature_reader_unittest.cc
|
| diff --git a/chromeos/system/cpu_temperature_reader_unittest.cc b/chromeos/system/cpu_temperature_reader_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f08d01000a8839bc1b746fa83f9daf0ce785cfbc
|
| --- /dev/null
|
| +++ b/chromeos/system/cpu_temperature_reader_unittest.cc
|
| @@ -0,0 +1,172 @@
|
| +// Copyright 2017 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/system/cpu_temperature_reader.h"
|
| +
|
| +#include <algorithm>
|
| +#include <memory>
|
| +#include <string>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/files/file.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/test/scoped_task_scheduler.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace chromeos {
|
| +namespace system {
|
| +
|
| +class CPUTemperatureReaderTest : public ::testing::Test {
|
| + public:
|
| + ~CPUTemperatureReaderTest() override {}
|
| +
|
| + void SetUp() override {
|
| + dir_.reset(new base::ScopedTempDir);
|
| + CHECK(dir_->CreateUniqueTempDir());
|
| + hwmon_path_ = dir_->GetPath();
|
| + reader_.set_hwmon_dir_for_test(hwmon_path_.MaybeAsASCII());
|
| + }
|
| +
|
| + void TearDown() override {
|
| + dir_.reset();
|
| + stored_cpu_temperatures_.clear();
|
| + }
|
| +
|
| + protected:
|
| + using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo;
|
| +
|
| + // Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full
|
| + // path of the new subdirectory.
|
| + base::FilePath CreateHwmonSubdir(const std::string& name) {
|
| + base::FilePath subdir_path = hwmon_path_.Append(name);
|
| + CHECK(base::CreateDirectory(subdir_path));
|
| + return subdir_path;
|
| + }
|
| +
|
| + // Creates a file at |path| containing data |contents|. Returns true if it was
|
| + // successfully created and filled with |contents|.
|
| + bool CreateFileWithContents(const base::FilePath& path,
|
| + const std::string& contents) {
|
| + return WriteFile(path, contents.data(), contents.size()) ==
|
| + static_cast<int>(contents.size());
|
| + }
|
| +
|
| + // Uses |reader_| to read CPU temperature from the temp dir. Returns the CPU
|
| + // temperature info from each sensor.
|
| + std::vector<CPUTemperatureInfo> ReadCPUTemperaturesFromDir() {
|
| + reader_.GetCPUTemperatures(
|
| + base::Bind(&CPUTemperatureReaderTest::StoreCPUTemperatures,
|
| + base::Unretained(this)));
|
| + base::RunLoop run_loop;
|
| + run_loop.RunUntilIdle();
|
| + return stored_cpu_temperatures_;
|
| + }
|
| +
|
| + void StoreCPUTemperatures(const std::vector<CPUTemperatureInfo>& temps) {
|
| + CHECK_EQ(stored_cpu_temperatures_.size(), 0U);
|
| + stored_cpu_temperatures_ = temps;
|
| + }
|
| +
|
| + // Creates a temporary dir to act as the hwmon directory passed to |reader_|.
|
| + std::unique_ptr<base::ScopedTempDir> dir_;
|
| +
|
| + // Path of the temporary dir created by |dir_|.
|
| + base::FilePath hwmon_path_;
|
| +
|
| + // Used for running scheduled tasks.
|
| + base::test::ScopedTaskScheduler scheduler_;
|
| +
|
| + // Each call to ReadCPUTemperaturesFromDir() stores a set of
|
| + // CPUTemperatureInfo in here.
|
| + std::vector<CPUTemperatureInfo> stored_cpu_temperatures_;
|
| +
|
| + // Instance of the class under test
|
| + CPUTemperatureReader reader_;
|
| +};
|
| +
|
| +TEST_F(CPUTemperatureReaderTest, EmptyDir) {
|
| + auto subdir_empty = CreateHwmonSubdir("hwmon0");
|
| + auto subdir_not_temp = CreateHwmonSubdir("hwmon1");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"),
|
| + "garbage"));
|
| +
|
| + EXPECT_EQ(0U, ReadCPUTemperaturesFromDir().size());
|
| +}
|
| +
|
| +TEST_F(CPUTemperatureReaderTest, SingleDir) {
|
| + auto subdir = CreateHwmonSubdir("hwmon0");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
|
| +
|
| + auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
|
| +
|
| + ASSERT_EQ(1U, cpu_temp_readings.size());
|
| + EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
|
| + EXPECT_EQ("", cpu_temp_readings[0].label);
|
| +}
|
| +
|
| +TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) {
|
| + auto subdir = CreateHwmonSubdir("hwmon0");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
|
| +
|
| + auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
|
| +
|
| + ASSERT_EQ(1U, cpu_temp_readings.size());
|
| + EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius);
|
| + EXPECT_EQ("t2", cpu_temp_readings[0].label);
|
| +}
|
| +
|
| +TEST_F(CPUTemperatureReaderTest, SingleDirWithName) {
|
| + auto subdir = CreateHwmonSubdir("hwmon0");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("name"), "t3"));
|
| +
|
| + auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
|
| +
|
| + ASSERT_EQ(1U, cpu_temp_readings.size());
|
| + EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius);
|
| + EXPECT_EQ("t3", cpu_temp_readings[0].label);
|
| +}
|
| +
|
| +TEST_F(CPUTemperatureReaderTest, MultipleDirs) {
|
| + {
|
| + auto subdir = CreateHwmonSubdir("hwmon0");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
|
| + }
|
| + {
|
| + auto subdir = CreateHwmonSubdir("hwmon1");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
|
| + }
|
| + {
|
| + // This should not result in a CPU temperature reading.
|
| + auto subdir = CreateHwmonSubdir("hwmon2");
|
| + EXPECT_TRUE(
|
| + CreateFileWithContents(subdir.Append("not_cpu_temp"), "garbage"));
|
| + }
|
| + {
|
| + auto subdir = CreateHwmonSubdir("hwmon3");
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
|
| + EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_label"), "t3"));
|
| + }
|
| +
|
| + auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
|
| +
|
| + // The order in which these directories were read is not guaranteed. Sort them
|
| + // first.
|
| + std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end());
|
| +
|
| + ASSERT_EQ(3U, cpu_temp_readings.size());
|
| + EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
|
| + EXPECT_EQ("", cpu_temp_readings[0].label);
|
| + EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
|
| + EXPECT_EQ("t2", cpu_temp_readings[1].label);
|
| + EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius);
|
| + EXPECT_EQ("t3", cpu_temp_readings[2].label);
|
| +}
|
| +
|
| +} // namespace system
|
| +} // namespace chromeos
|
|
|