Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chromeos/system/cpu_temperature_reader_unittest.cc

Issue 2823583002: chromeos: Add CPU temperature reader (Closed)
Patch Set: Make runnable on main UI thread Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <memory>
9 #include <string>
10
11 #include "base/bind.h"
12 #include "base/files/file.h"
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_temp_dir.h"
15 #include "base/run_loop.h"
16 #include "base/test/scoped_task_scheduler.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace chromeos {
20 namespace system {
21
22 class CPUTemperatureReaderTest : public ::testing::Test {
23 public:
24 ~CPUTemperatureReaderTest() override {}
25
26 void SetUp() override {
27 dir_.reset(new base::ScopedTempDir);
28 CHECK(dir_->CreateUniqueTempDir());
29 hwmon_path_ = dir_->GetPath();
30 reader_.set_hwmon_dir_for_test(hwmon_path_.MaybeAsASCII());
31 }
32
33 void TearDown() override {
34 dir_.reset();
35 stored_cpu_temperatures_.clear();
36 }
37
38 protected:
39 using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo;
40
41 // Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full
42 // path of the new subdirectory.
43 base::FilePath CreateHwmonSubdir(const std::string& name) {
44 base::FilePath subdir_path = hwmon_path_.Append(name);
45 CHECK(base::CreateDirectory(subdir_path));
46 return subdir_path;
47 }
48
49 // Creates a file at |path| containing data |contents|. Returns true if it was
50 // successfully created and filled with |contents|.
51 bool CreateFileWithContents(const base::FilePath& path,
52 const std::string& contents) {
53 return WriteFile(path, contents.data(), contents.size()) ==
54 static_cast<int>(contents.size());
55 }
56
57 // Uses |reader_| to read CPU temperature from the temp dir. Returns the CPU
58 // temperature info from each sensor.
59 std::vector<CPUTemperatureInfo> ReadCPUTemperaturesFromDir() {
60 reader_.GetCPUTemperatures(
61 base::Bind(&CPUTemperatureReaderTest::StoreCPUTemperatures,
62 base::Unretained(this)));
63 base::RunLoop run_loop;
64 run_loop.RunUntilIdle();
65 return stored_cpu_temperatures_;
66 }
67
68 void StoreCPUTemperatures(const std::vector<CPUTemperatureInfo>& temps) {
69 CHECK_EQ(stored_cpu_temperatures_.size(), 0U);
70 stored_cpu_temperatures_ = temps;
71 }
72
73 // Creates a temporary dir to act as the hwmon directory passed to |reader_|.
74 std::unique_ptr<base::ScopedTempDir> dir_;
75
76 // Path of the temporary dir created by |dir_|.
77 base::FilePath hwmon_path_;
78
79 // Used for running scheduled tasks.
80 base::test::ScopedTaskScheduler scheduler_;
81
82 // Each call to ReadCPUTemperaturesFromDir() stores a set of
83 // CPUTemperatureInfo in here.
84 std::vector<CPUTemperatureInfo> stored_cpu_temperatures_;
85
86 // Instance of the class under test
87 CPUTemperatureReader reader_;
88 };
89
90 TEST_F(CPUTemperatureReaderTest, EmptyDir) {
91 auto subdir_empty = CreateHwmonSubdir("hwmon0");
92 auto subdir_not_temp = CreateHwmonSubdir("hwmon1");
93 EXPECT_TRUE(CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"),
94 "garbage"));
95
96 EXPECT_EQ(0U, ReadCPUTemperaturesFromDir().size());
97 }
98
99 TEST_F(CPUTemperatureReaderTest, SingleDir) {
100 auto subdir = CreateHwmonSubdir("hwmon0");
101 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
102
103 auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
104
105 ASSERT_EQ(1U, cpu_temp_readings.size());
106 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
107 EXPECT_EQ("", cpu_temp_readings[0].label);
108 }
109
110 TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) {
111 auto subdir = CreateHwmonSubdir("hwmon0");
112 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
113 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
114
115 auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
116
117 ASSERT_EQ(1U, cpu_temp_readings.size());
118 EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius);
119 EXPECT_EQ("t2", cpu_temp_readings[0].label);
120 }
121
122 TEST_F(CPUTemperatureReaderTest, SingleDirWithName) {
123 auto subdir = CreateHwmonSubdir("hwmon0");
124 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
125 EXPECT_TRUE(CreateFileWithContents(subdir.Append("name"), "t3"));
126
127 auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
128
129 ASSERT_EQ(1U, cpu_temp_readings.size());
130 EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius);
131 EXPECT_EQ("t3", cpu_temp_readings[0].label);
132 }
133
134 TEST_F(CPUTemperatureReaderTest, MultipleDirs) {
135 {
136 auto subdir = CreateHwmonSubdir("hwmon0");
137 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
138 }
139 {
140 auto subdir = CreateHwmonSubdir("hwmon1");
141 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
142 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
143 }
144 {
145 // This should not result in a CPU temperature reading.
146 auto subdir = CreateHwmonSubdir("hwmon2");
147 EXPECT_TRUE(
148 CreateFileWithContents(subdir.Append("not_cpu_temp"), "garbage"));
149 }
150 {
151 auto subdir = CreateHwmonSubdir("hwmon3");
152 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
153 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_label"), "t3"));
154 }
155
156 auto cpu_temp_readings = ReadCPUTemperaturesFromDir();
157
158 // The order in which these directories were read is not guaranteed. Sort them
159 // first.
160 std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end());
161
162 ASSERT_EQ(3U, cpu_temp_readings.size());
163 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
164 EXPECT_EQ("", cpu_temp_readings[0].label);
165 EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
166 EXPECT_EQ("t2", cpu_temp_readings[1].label);
167 EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius);
168 EXPECT_EQ("t3", cpu_temp_readings[2].label);
169 }
170
171 } // namespace system
172 } // namespace chromeos
OLDNEW
« chromeos/system/cpu_temperature_reader.cc ('K') | « chromeos/system/cpu_temperature_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698