OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/system/cpu_temperature_reader.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "base/files/file.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace chromeos { |
| 16 namespace system { |
| 17 |
| 18 class CPUTemperatureReaderTest : public ::testing::Test { |
| 19 public: |
| 20 CPUTemperatureReaderTest() { |
| 21 CHECK(dir_.CreateUniqueTempDir()); |
| 22 hwmon_path_ = dir_.GetPath(); |
| 23 reader_.set_hwmon_dir_for_test(hwmon_path_); |
| 24 } |
| 25 |
| 26 ~CPUTemperatureReaderTest() override = default; |
| 27 |
| 28 protected: |
| 29 using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo; |
| 30 |
| 31 // Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full |
| 32 // path of the new subdirectory. |
| 33 base::FilePath CreateHwmonSubdir(const std::string& name) { |
| 34 base::FilePath subdir_path = hwmon_path_.Append(name); |
| 35 CHECK(base::CreateDirectory(subdir_path)); |
| 36 return subdir_path; |
| 37 } |
| 38 |
| 39 // Creates a file at |path| containing data |contents|. |
| 40 void CreateFileWithContents(const base::FilePath& path, |
| 41 const std::string& contents) { |
| 42 CHECK_EQ(WriteFile(path, contents.data(), contents.size()), |
| 43 static_cast<int>(contents.size())); |
| 44 } |
| 45 |
| 46 // Creates a temporary dir to act as the hwmon directory passed to |reader_|. |
| 47 base::ScopedTempDir dir_; |
| 48 |
| 49 // Path of the temporary dir created by |dir_|. |
| 50 base::FilePath hwmon_path_; |
| 51 |
| 52 // Instance of the class under test |
| 53 CPUTemperatureReader reader_; |
| 54 }; |
| 55 |
| 56 TEST_F(CPUTemperatureReaderTest, EmptyDir) { |
| 57 base::FilePath subdir_empty = CreateHwmonSubdir("hwmon0"); |
| 58 base::FilePath subdir_not_temp = CreateHwmonSubdir("hwmon1"); |
| 59 CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"), "garbage"); |
| 60 |
| 61 EXPECT_EQ(0U, reader_.GetCPUTemperatures().size()); |
| 62 } |
| 63 |
| 64 TEST_F(CPUTemperatureReaderTest, SingleDir) { |
| 65 base::FilePath subdir = CreateHwmonSubdir("hwmon0"); |
| 66 CreateFileWithContents(subdir.Append("temp1_input"), "10000\n"); |
| 67 |
| 68 std::vector<CPUTemperatureInfo> cpu_temp_readings = |
| 69 reader_.GetCPUTemperatures(); |
| 70 |
| 71 ASSERT_EQ(1U, cpu_temp_readings.size()); |
| 72 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius); |
| 73 EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label); |
| 74 } |
| 75 |
| 76 TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) { |
| 77 base::FilePath subdir = CreateHwmonSubdir("hwmon0"); |
| 78 CreateFileWithContents(subdir.Append("temp2_input"), "20000\n"); |
| 79 CreateFileWithContents(subdir.Append("temp2_label"), "t2\n"); |
| 80 |
| 81 std::vector<CPUTemperatureInfo> cpu_temp_readings = |
| 82 reader_.GetCPUTemperatures(); |
| 83 |
| 84 ASSERT_EQ(1U, cpu_temp_readings.size()); |
| 85 EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius); |
| 86 EXPECT_EQ("t2", cpu_temp_readings[0].label); |
| 87 } |
| 88 |
| 89 TEST_F(CPUTemperatureReaderTest, SingleDirWithName) { |
| 90 base::FilePath subdir = CreateHwmonSubdir("hwmon0"); |
| 91 CreateFileWithContents(subdir.Append("temp3_input"), "30000\n"); |
| 92 CreateFileWithContents(subdir.Append("temp3_label"), "\n"); |
| 93 CreateFileWithContents(subdir.Append("name"), "t3\n"); |
| 94 |
| 95 std::vector<CPUTemperatureInfo> cpu_temp_readings = |
| 96 reader_.GetCPUTemperatures(); |
| 97 |
| 98 ASSERT_EQ(1U, cpu_temp_readings.size()); |
| 99 EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius); |
| 100 EXPECT_EQ("t3", cpu_temp_readings[0].label); |
| 101 } |
| 102 |
| 103 TEST_F(CPUTemperatureReaderTest, MultipleDirs) { |
| 104 base::FilePath subdir0 = CreateHwmonSubdir("hwmon0"); |
| 105 CreateFileWithContents(subdir0.Append("temp1_input"), "10000\n"); |
| 106 |
| 107 base::FilePath subdir1 = CreateHwmonSubdir("hwmon1"); |
| 108 CreateFileWithContents(subdir1.Append("temp2_input"), "20000\n"); |
| 109 CreateFileWithContents(subdir1.Append("temp2_label"), "t2\n"); |
| 110 |
| 111 // This should not result in a CPU temperature reading. |
| 112 base::FilePath subdir2 = CreateHwmonSubdir("hwmon2"); |
| 113 CreateFileWithContents(subdir2.Append("not_cpu_temp"), "garbage"); |
| 114 |
| 115 base::FilePath subdir3 = CreateHwmonSubdir("hwmon3"); |
| 116 CreateFileWithContents(subdir3.Append("temp3_input"), "30000\n"); |
| 117 CreateFileWithContents(subdir3.Append("temp3_label"), "t3\n"); |
| 118 |
| 119 std::vector<CPUTemperatureInfo> cpu_temp_readings = |
| 120 reader_.GetCPUTemperatures(); |
| 121 |
| 122 // The order in which these directories were read is not guaranteed. Sort them |
| 123 // first. |
| 124 std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end()); |
| 125 |
| 126 ASSERT_EQ(3U, cpu_temp_readings.size()); |
| 127 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius); |
| 128 EXPECT_EQ(subdir0.Append("temp1_label").value(), cpu_temp_readings[0].label); |
| 129 EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius); |
| 130 EXPECT_EQ("t2", cpu_temp_readings[1].label); |
| 131 EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius); |
| 132 EXPECT_EQ("t3", cpu_temp_readings[2].label); |
| 133 } |
| 134 |
| 135 } // namespace system |
| 136 } // namespace chromeos |
OLD | NEW |