Index: chromeos/system/cpu_temp_reader_unittest.cc |
diff --git a/chromeos/system/cpu_temp_reader_unittest.cc b/chromeos/system/cpu_temp_reader_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d2f540868a2655b756396946421d5bdfd0881d98 |
--- /dev/null |
+++ b/chromeos/system/cpu_temp_reader_unittest.cc |
@@ -0,0 +1,129 @@ |
+// 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_temp_reader.h" |
+ |
+#include <algorithm> |
+#include <memory> |
+#include <string> |
+ |
+#include "base/files/file.h" |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace chromeos { |
+namespace system { |
+ |
+class CPUTemperatureReaderTest : public ::testing::Test { |
+ public: |
+ 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(); } |
+ |
+ protected: |
+ base::FilePath CreateHwmonSubdir(const std::string& name) { |
+ base::FilePath subdir_path = hwmon_path_.Append(name); |
+ CHECK(base::CreateDirectory(subdir_path)); |
+ return subdir_path; |
+ } |
+ |
+ bool CreateFileWithContents(const base::FilePath& path, |
+ const std::string& contents) { |
+ return WriteFile(path, contents.data(), contents.size()) == |
+ static_cast<int>(contents.size()); |
+ } |
+ |
+ std::unique_ptr<base::ScopedTempDir> dir_; |
+ base::FilePath hwmon_path_; |
+ 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, reader_.GetCPUTemperatures().size()); |
+} |
+ |
+TEST_F(CPUTemperatureReaderTest, SingleDir) { |
+ auto subdir = CreateHwmonSubdir("hwmon0"); |
+ EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000")); |
+ |
+ auto cpu_temp_readings = reader_.GetCPUTemperatures(); |
+ |
+ 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 = reader_.GetCPUTemperatures(); |
+ |
+ 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 = reader_.GetCPUTemperatures(); |
+ |
+ 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 = reader_.GetCPUTemperatures(); |
+ |
+ // 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 |