Chromium Code Reviews| Index: chrome/browser/chromeos/power/cpu_data_collector_unittest.cc |
| diff --git a/chrome/browser/chromeos/power/cpu_data_collector_unittest.cc b/chrome/browser/chromeos/power/cpu_data_collector_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2ae5c50acd2559b012f3ed4dcb147076e2df45c6 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/power/cpu_data_collector_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// 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 <string> |
| +#include <vector> |
| + |
| +#include "base/files/file.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "chrome/browser/chromeos/power/cpu_data_collector.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| +namespace { |
| +// The suffix path of fake cpu frequency file for cpu0. |
|
Daniel Erat
2017/05/03 22:35:43
nit: please add a blank line above each comment to
weidongg
2017/05/03 23:16:09
Done.
|
| +const char kTimeInStateSuffixPathCpu0[] = "cpu0/time_in_state"; |
| +// The suffix path of fake cpu frequency file for cpu1. |
| +const char kTimeInStateSuffixPathCpu1[] = "cpu1/time_in_state"; |
| +// The suffix path of fake cpu frequency file for all cpus. |
| +const char kAllTimeInStateSuffixPath[] = "all_time_in_state"; |
| +// The string content of the fake cpu frequency file for cpu0. |
| +const char kTimeInStateContentCpu0[] = |
| + "20000 30000000\n" |
| + "60000 90000\n" |
| + "100000 50000\n"; |
| +// The string content of the fake cpu frequency file for cpu1. |
| +const char kTimeInStateContentCpu1[] = |
| + "20000 31000000\n" |
| + "60000 91000\n" |
| + "100000 51000\n"; |
| +// The string content of the fake cpu frequency file for all cpus. |
| +const char kAllTimeInStateContent[] = |
| + "freq\t\tcpu0\t\tcpu1\t\t\n" |
| + "20000\t\t30000000\t\t31000000\t\t\n" |
| + "60000\t\t90000\t\t91000\t\t\n" |
| + "100000\t\t50000\t\t51000\t\t\n"; |
| +} // namespace |
|
Daniel Erat
2017/05/03 22:35:43
nit: add a blank line above this one
weidongg
2017/05/03 23:16:09
Done.
|
| + |
| +class CpuDataCollectorTest : public testing::Test { |
| + public: |
| + CpuDataCollectorTest() |
| + : kExpectedCpuFreqStateNames({"20", "60", "100"}), |
| + kExpectedTimeInStateCpu0({300000000, 900000, 500000}), |
| + kExpectedTimeInStateCpu1({310000000, 910000, 510000}) { |
| + CHECK(temp_dir_.CreateUniqueTempDir()); |
| + |
| + time_in_state_path_cpu0_ = |
| + temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu0); |
| + time_in_state_path_cpu1_ = |
| + temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu1); |
| + all_time_in_state_path_ = |
| + temp_dir_.GetPath().AppendASCII(kAllTimeInStateSuffixPath); |
| + |
| + CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu0_)); |
| + CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu1_)); |
| + CHECK(base::CreateTemporaryFile(&all_time_in_state_path_)); |
| + CHECK(base::WriteFile(time_in_state_path_cpu0_, kTimeInStateContentCpu0, |
| + static_cast<int>(strlen(kTimeInStateContentCpu0))) != |
| + -1); |
| + CHECK(base::WriteFile(time_in_state_path_cpu1_, kTimeInStateContentCpu1, |
| + static_cast<int>(strlen(kTimeInStateContentCpu1))) != |
| + -1); |
| + CHECK(base::WriteFile(all_time_in_state_path_, kAllTimeInStateContent, |
| + static_cast<int>(strlen(kAllTimeInStateContent))) != |
| + -1); |
| + } |
| + |
| + protected: |
| + const base::FilePath GetTimeInStatePathCpu0() const { |
| + return time_in_state_path_cpu0_; |
| + } |
| + const base::FilePath GetTimeInStatePathCpu1() const { |
| + return time_in_state_path_cpu1_; |
| + } |
| + const base::FilePath GetAllTimeInStatePath() const { |
| + return all_time_in_state_path_; |
| + } |
| + // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState| |
| + // or |ReadCpuFreqAllTimeInState| |
| + const std::vector<std::string> kExpectedCpuFreqStateNames; |
| + // Expected time_in_state of sample for cpu0. |
| + const std::vector<int64_t> kExpectedTimeInStateCpu0; |
| + // Expected time_in_state of sample for cpu1. |
| + const std::vector<int64_t> kExpectedTimeInStateCpu1; |
| + |
| + private: |
| + base::ScopedTempDir temp_dir_; |
| + base::FilePath time_in_state_path_cpu0_; |
| + base::FilePath time_in_state_path_cpu1_; |
| + base::FilePath all_time_in_state_path_; |
|
Daniel Erat
2017/05/03 22:35:43
you can just make all these members protected and
weidongg
2017/05/03 23:16:09
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest); |
| +}; |
| + |
| +TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) { |
| + std::vector<std::string> cpu_freq_state_names; |
| + CpuDataCollector::StateOccupancySample freq_sample_cpu0; |
| + CpuDataCollector::StateOccupancySample freq_sample_cpu1; |
| + |
| + CpuDataCollector::ReadCpuFreqTimeInState( |
| + GetTimeInStatePathCpu0(), &cpu_freq_state_names, &freq_sample_cpu0); |
| + EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); |
| + EXPECT_EQ(kExpectedTimeInStateCpu0, freq_sample_cpu0.time_in_state); |
| + |
| + cpu_freq_state_names.clear(); |
| + CpuDataCollector::ReadCpuFreqTimeInState( |
| + GetTimeInStatePathCpu1(), &cpu_freq_state_names, &freq_sample_cpu1); |
| + EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); |
| + EXPECT_EQ(kExpectedTimeInStateCpu1, freq_sample_cpu1.time_in_state); |
| +} |
| + |
| +TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) { |
| + std::vector<std::string> cpu_freq_state_names; |
| + std::vector<CpuDataCollector::StateOccupancySample> freq_samples; |
| + CpuDataCollector::StateOccupancySample freq_sample_cpu0; |
| + CpuDataCollector::StateOccupancySample freq_sample_cpu1; |
| + // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online. |
| + freq_sample_cpu0.cpu_online = true; |
| + freq_sample_cpu1.cpu_online = true; |
| + freq_samples.push_back(freq_sample_cpu0); |
| + freq_samples.push_back(freq_sample_cpu1); |
| + |
| + CpuDataCollector::ReadCpuFreqAllTimeInState( |
| + 2, GetAllTimeInStatePath(), &cpu_freq_state_names, &freq_samples); |
| + EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); |
| + EXPECT_EQ(kExpectedTimeInStateCpu0, freq_samples[0].time_in_state); |
| + EXPECT_EQ(kExpectedTimeInStateCpu1, freq_samples[1].time_in_state); |
| +} |
| + |
| +} // namespace chromeos |