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

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

Issue 2823583002: chromeos: Add CPU temperature reader (Closed)
Patch Set: Factor out temp and label reading; Add class and unit test 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_temp_reader.h"
6
7 #include <algorithm>
8 #include <memory>
9 #include <string>
10
11 #include "base/files/file.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chromeos {
17 namespace system {
18
19 class CPUTemperatureReaderTest : public ::testing::Test {
20 public:
21 void SetUp() override {
22 dir_.reset(new base::ScopedTempDir);
23 CHECK(dir_->CreateUniqueTempDir());
24 hwmon_path_ = dir_->GetPath();
25 reader_.set_hwmon_dir_for_test(hwmon_path_.MaybeAsASCII());
26 }
27 void TearDown() override { dir_.reset(); }
28
29 protected:
30 base::FilePath CreateHwmonSubdir(const std::string& name) {
31 base::FilePath subdir_path = hwmon_path_.Append(name);
32 CHECK(base::CreateDirectory(subdir_path));
33 return subdir_path;
34 }
35
36 bool CreateFileWithContents(const base::FilePath& path,
37 const std::string& contents) {
38 return WriteFile(path, contents.data(), contents.size()) ==
39 static_cast<int>(contents.size());
40 }
41
42 std::unique_ptr<base::ScopedTempDir> dir_;
43 base::FilePath hwmon_path_;
44 CPUTemperatureReader reader_;
45 };
46
47 TEST_F(CPUTemperatureReaderTest, EmptyDir) {
48 auto subdir_empty = CreateHwmonSubdir("hwmon0");
49 auto subdir_not_temp = CreateHwmonSubdir("hwmon1");
50 EXPECT_TRUE(CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"),
51 "garbage"));
52
53 EXPECT_EQ(0U, reader_.GetCPUTemperatures().size());
54 }
55
56 TEST_F(CPUTemperatureReaderTest, SingleDir) {
57 auto subdir = CreateHwmonSubdir("hwmon0");
58 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
59
60 auto cpu_temp_readings = reader_.GetCPUTemperatures();
61
62 ASSERT_EQ(1U, cpu_temp_readings.size());
63 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
64 EXPECT_EQ("", cpu_temp_readings[0].label);
65 }
66
67 TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) {
68 auto subdir = CreateHwmonSubdir("hwmon0");
69 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
70 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
71
72 auto cpu_temp_readings = reader_.GetCPUTemperatures();
73
74 ASSERT_EQ(1U, cpu_temp_readings.size());
75 EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius);
76 EXPECT_EQ("t2", cpu_temp_readings[0].label);
77 }
78
79 TEST_F(CPUTemperatureReaderTest, SingleDirWithName) {
80 auto subdir = CreateHwmonSubdir("hwmon0");
81 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
82 EXPECT_TRUE(CreateFileWithContents(subdir.Append("name"), "t3"));
83
84 auto cpu_temp_readings = reader_.GetCPUTemperatures();
85
86 ASSERT_EQ(1U, cpu_temp_readings.size());
87 EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius);
88 EXPECT_EQ("t3", cpu_temp_readings[0].label);
89 }
90
91 TEST_F(CPUTemperatureReaderTest, MultipleDirs) {
92 {
93 auto subdir = CreateHwmonSubdir("hwmon0");
94 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000"));
95 }
96 {
97 auto subdir = CreateHwmonSubdir("hwmon1");
98 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000"));
99 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2"));
100 }
101 {
102 // This should not result in a CPU temperature reading.
103 auto subdir = CreateHwmonSubdir("hwmon2");
104 EXPECT_TRUE(
105 CreateFileWithContents(subdir.Append("not_cpu_temp"), "garbage"));
106 }
107 {
108 auto subdir = CreateHwmonSubdir("hwmon3");
109 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000"));
110 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_label"), "t3"));
111 }
112
113 auto cpu_temp_readings = reader_.GetCPUTemperatures();
114
115 // The order in which these directories were read is not guaranteed. Sort them
116 // first.
117 std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end());
118
119 ASSERT_EQ(3U, cpu_temp_readings.size());
120 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius);
121 EXPECT_EQ("", cpu_temp_readings[0].label);
122 EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius);
123 EXPECT_EQ("t2", cpu_temp_readings[1].label);
124 EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius);
125 EXPECT_EQ("t3", cpu_temp_readings[2].label);
126 }
127
128 } // namespace system
129 } // namespace chromeos
OLDNEW
« chromeos/system/cpu_temp_reader.h ('K') | « chromeos/system/cpu_temp_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698