Chromium Code Reviews| 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 <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 ~CPUTemperatureReaderTest() override {} | |
| 22 | |
| 23 void SetUp() override { | |
|
Daniel Erat
2017/04/21 21:01:20
put things in c'tor and d'tor instead of SetUp whe
Simon Que
2017/04/24 13:59:17
Done.
| |
| 24 dir_.reset(new base::ScopedTempDir); | |
| 25 CHECK(dir_->CreateUniqueTempDir()); | |
| 26 hwmon_path_ = dir_->GetPath(); | |
| 27 reader_.set_hwmon_dir_for_test(hwmon_path_.MaybeAsASCII()); | |
| 28 } | |
| 29 | |
| 30 void TearDown() override { dir_.reset(); } | |
|
Daniel Erat
2017/04/21 21:01:20
why reset this? the d'tor will already destroy it
Simon Que
2017/04/24 13:59:17
Done.
| |
| 31 | |
| 32 protected: | |
| 33 using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo; | |
| 34 | |
| 35 // Creates a subdirectory in |hwmon_path_| with name |name|. Returns the full | |
| 36 // path of the new subdirectory. | |
| 37 base::FilePath CreateHwmonSubdir(const std::string& name) { | |
| 38 base::FilePath subdir_path = hwmon_path_.Append(name); | |
| 39 CHECK(base::CreateDirectory(subdir_path)); | |
| 40 return subdir_path; | |
| 41 } | |
| 42 | |
| 43 // Creates a file at |path| containing data |contents|. Returns true if it was | |
| 44 // successfully created and filled with |contents|. | |
| 45 bool CreateFileWithContents(const base::FilePath& path, | |
|
Daniel Erat
2017/04/21 21:01:20
you never expect this to fail, right? return void
Simon Que
2017/04/24 13:59:17
Done.
| |
| 46 const std::string& contents) { | |
| 47 return WriteFile(path, contents.data(), contents.size()) == | |
| 48 static_cast<int>(contents.size()); | |
| 49 } | |
| 50 | |
| 51 // Creates a temporary dir to act as the hwmon directory passed to |reader_|. | |
| 52 std::unique_ptr<base::ScopedTempDir> dir_; | |
|
Daniel Erat
2017/04/21 21:01:20
why does this need to be in a unique_ptr?
Simon Que
2017/04/24 13:59:17
Done.
| |
| 53 | |
| 54 // Path of the temporary dir created by |dir_|. | |
| 55 base::FilePath hwmon_path_; | |
| 56 | |
| 57 // Instance of the class under test | |
| 58 CPUTemperatureReader reader_; | |
| 59 }; | |
| 60 | |
| 61 TEST_F(CPUTemperatureReaderTest, EmptyDir) { | |
| 62 auto subdir_empty = CreateHwmonSubdir("hwmon0"); | |
|
Daniel Erat
2017/04/21 21:01:20
use explicit type rather than 'auto' since the typ
Simon Que
2017/04/24 13:59:17
Done.
| |
| 63 auto subdir_not_temp = CreateHwmonSubdir("hwmon1"); | |
| 64 EXPECT_TRUE(CreateFileWithContents(subdir_not_temp.Append("not_cpu_temp"), | |
| 65 "garbage")); | |
| 66 | |
| 67 EXPECT_EQ(0U, reader_.GetCPUTemperatures().size()); | |
| 68 } | |
| 69 | |
| 70 TEST_F(CPUTemperatureReaderTest, SingleDir) { | |
| 71 auto subdir = CreateHwmonSubdir("hwmon0"); | |
| 72 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp1_input"), "10000\n")); | |
| 73 | |
| 74 auto cpu_temp_readings = reader_.GetCPUTemperatures(); | |
|
Daniel Erat
2017/04/21 21:01:20
use explicit types instead of 'auto'
Simon Que
2017/04/24 13:59:17
Done.
| |
| 75 | |
| 76 ASSERT_EQ(1U, cpu_temp_readings.size()); | |
| 77 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius); | |
| 78 EXPECT_EQ(subdir.Append("temp1_label").value(), cpu_temp_readings[0].label); | |
| 79 } | |
| 80 | |
| 81 TEST_F(CPUTemperatureReaderTest, SingleDirWithLabel) { | |
| 82 auto subdir = CreateHwmonSubdir("hwmon0"); | |
| 83 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_input"), "20000\n")); | |
| 84 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp2_label"), "t2\n")); | |
| 85 | |
| 86 auto cpu_temp_readings = reader_.GetCPUTemperatures(); | |
| 87 | |
| 88 ASSERT_EQ(1U, cpu_temp_readings.size()); | |
| 89 EXPECT_EQ(20.0f, cpu_temp_readings[0].temp_celsius); | |
| 90 EXPECT_EQ("t2", cpu_temp_readings[0].label); | |
| 91 } | |
| 92 | |
| 93 TEST_F(CPUTemperatureReaderTest, SingleDirWithName) { | |
| 94 auto subdir = CreateHwmonSubdir("hwmon0"); | |
| 95 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_input"), "30000\n")); | |
| 96 EXPECT_TRUE(CreateFileWithContents(subdir.Append("temp3_label"), "\n")); | |
| 97 EXPECT_TRUE(CreateFileWithContents(subdir.Append("name"), "t3\n")); | |
| 98 | |
| 99 auto cpu_temp_readings = reader_.GetCPUTemperatures(); | |
| 100 | |
| 101 ASSERT_EQ(1U, cpu_temp_readings.size()); | |
| 102 EXPECT_EQ(30.0f, cpu_temp_readings[0].temp_celsius); | |
| 103 EXPECT_EQ("t3", cpu_temp_readings[0].label); | |
| 104 } | |
| 105 | |
| 106 TEST_F(CPUTemperatureReaderTest, MultipleDirs) { | |
| 107 auto subdir0 = CreateHwmonSubdir("hwmon0"); | |
| 108 EXPECT_TRUE(CreateFileWithContents(subdir0.Append("temp1_input"), "10000\n")); | |
| 109 | |
| 110 auto subdir1 = CreateHwmonSubdir("hwmon1"); | |
| 111 EXPECT_TRUE(CreateFileWithContents(subdir1.Append("temp2_input"), "20000\n")); | |
| 112 EXPECT_TRUE(CreateFileWithContents(subdir1.Append("temp2_label"), "t2\n")); | |
| 113 | |
| 114 // This should not result in a CPU temperature reading. | |
| 115 auto subdir2 = CreateHwmonSubdir("hwmon2"); | |
| 116 EXPECT_TRUE( | |
| 117 CreateFileWithContents(subdir2.Append("not_cpu_temp"), "garbage")); | |
| 118 | |
| 119 auto subdir3 = CreateHwmonSubdir("hwmon3"); | |
| 120 EXPECT_TRUE(CreateFileWithContents(subdir3.Append("temp3_input"), "30000\n")); | |
| 121 EXPECT_TRUE(CreateFileWithContents(subdir3.Append("temp3_label"), "t3\n")); | |
| 122 | |
| 123 auto cpu_temp_readings = reader_.GetCPUTemperatures(); | |
| 124 | |
| 125 // The order in which these directories were read is not guaranteed. Sort them | |
| 126 // first. | |
| 127 std::sort(cpu_temp_readings.begin(), cpu_temp_readings.end()); | |
| 128 | |
| 129 ASSERT_EQ(3U, cpu_temp_readings.size()); | |
| 130 EXPECT_EQ(10.0f, cpu_temp_readings[0].temp_celsius); | |
| 131 EXPECT_EQ(subdir0.Append("temp1_label").value(), cpu_temp_readings[0].label); | |
| 132 EXPECT_EQ(20.0f, cpu_temp_readings[1].temp_celsius); | |
| 133 EXPECT_EQ("t2", cpu_temp_readings[1].label); | |
| 134 EXPECT_EQ(30.0f, cpu_temp_readings[2].temp_celsius); | |
| 135 EXPECT_EQ("t3", cpu_temp_readings[2].label); | |
| 136 } | |
| 137 | |
| 138 } // namespace system | |
| 139 } // namespace chromeos | |
| OLD | NEW |