• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/traced/probes/power/linux_power_sysfs_data_source.h"
18 #include "src/base/test/tmp_dir_tree.h"
19 #include "test/gtest_and_gmock.h"
20 
21 namespace perfetto {
22 namespace {
23 
TEST(LinuxPowerSysfsDataSourceTest,BatteryCounters)24 TEST(LinuxPowerSysfsDataSourceTest, BatteryCounters) {
25   base::TmpDirTree tmpdir;
26   std::unique_ptr<LinuxPowerSysfsDataSource::BatteryInfo> battery_info_;
27 
28   tmpdir.AddDir("BAT0");
29   tmpdir.AddFile("BAT0/type", "Battery\n");
30   tmpdir.AddFile("BAT0/present", "1\n");
31   tmpdir.AddFile("BAT0/capacity", "95\n");         // 95 percent.
32   tmpdir.AddFile("BAT0/charge_now", "3074000\n");  // 3074000 µAh.
33   tmpdir.AddFile("BAT0/current_now", "245000\n");  // 245000 µA.
34   tmpdir.AddFile("BAT0/current_avg", "240000\n");  // 240000 µA.
35 
36   battery_info_.reset(
37       new LinuxPowerSysfsDataSource::BatteryInfo(tmpdir.path().c_str()));
38 
39   EXPECT_EQ(battery_info_->num_batteries(), 1u);
40   EXPECT_EQ(*battery_info_->GetCapacityPercent(0), 95);
41   EXPECT_EQ(*battery_info_->GetCurrentNowUa(0), 245000);
42   EXPECT_EQ(*battery_info_->GetAverageCurrentUa(0), 240000);
43   EXPECT_EQ(*battery_info_->GetChargeCounterUah(0), 3074000);
44 }
45 
TEST(LinuxPowerSysfsDataSourceTest,HidDeviceCounters)46 TEST(LinuxPowerSysfsDataSourceTest, HidDeviceCounters) {
47   base::TmpDirTree tmpdir;
48   std::unique_ptr<LinuxPowerSysfsDataSource::BatteryInfo> battery_info_;
49 
50   // Some HID devices (e.g. stylus) can also report battery info.
51   tmpdir.AddDir("hid-0001-battery");
52   tmpdir.AddFile("hid-0001-battery/type", "Battery\n");
53   tmpdir.AddFile("hid-0001-battery/present", "1\n");
54   tmpdir.AddFile("hid-0001-battery/capacity", "88\n");  // 88 percent.
55   // The HID device only reports the battery capacity in percent.
56 
57   battery_info_.reset(
58       new LinuxPowerSysfsDataSource::BatteryInfo(tmpdir.path().c_str()));
59 
60   EXPECT_EQ(battery_info_->num_batteries(), 1u);
61   EXPECT_EQ(*battery_info_->GetCapacityPercent(0), 88);
62   EXPECT_EQ(battery_info_->GetCurrentNowUa(0), base::nullopt);
63   EXPECT_EQ(battery_info_->GetAverageCurrentUa(0), base::nullopt);
64   EXPECT_EQ(battery_info_->GetChargeCounterUah(0), base::nullopt);
65 }
66 
67 }  // namespace
68 }  // namespace perfetto
69