Commit 35827436 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Introduce PowerManagerClient Conversion Helpers

- Adds helper methods to convert between fields in the
PowerSupplyProperties proto and new mojom enums.

Bug: 1128204
Change-Id: I1ceeb4e9987b5975189a725d8f346c39b45186b3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2431477Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812546}
parent 50606ac1
...@@ -8,6 +8,8 @@ static_library("backend") { ...@@ -8,6 +8,8 @@ static_library("backend") {
sources = [ sources = [
"cros_healthd_helpers.cc", "cros_healthd_helpers.cc",
"cros_healthd_helpers.h", "cros_healthd_helpers.h",
"power_manager_client_conversions.cc",
"power_manager_client_conversions.h",
"system_data_provider.cc", "system_data_provider.cc",
"system_data_provider.h", "system_data_provider.h",
] ]
...@@ -15,6 +17,8 @@ static_library("backend") { ...@@ -15,6 +17,8 @@ static_library("backend") {
deps = [ deps = [
"//base", "//base",
"//chromeos/components/diagnostics_ui/mojom", "//chromeos/components/diagnostics_ui/mojom",
"//chromeos/dbus/power",
"//chromeos/dbus/power:power_manager_proto",
"//chromeos/services/cros_healthd/public/cpp", "//chromeos/services/cros_healthd/public/cpp",
"//chromeos/services/cros_healthd/public/mojom", "//chromeos/services/cros_healthd/public/mojom",
] ]
...@@ -23,12 +27,18 @@ static_library("backend") { ...@@ -23,12 +27,18 @@ static_library("backend") {
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
sources = [ "system_data_provider_unittest.cc" ] sources = [
"power_manager_client_conversions_unittest.cc",
"system_data_provider_unittest.cc",
]
deps = [ deps = [
":backend", ":backend",
"//base/test:test_support", "//base/test:test_support",
"//chromeos/components/diagnostics_ui/mojom",
"//chromeos/dbus/cros_healthd", "//chromeos/dbus/cros_healthd",
"//chromeos/dbus/power",
"//chromeos/dbus/power:power_manager_proto",
"//chromeos/services/cros_healthd/public/cpp", "//chromeos/services/cros_healthd/public/cpp",
"//chromeos/services/cros_healthd/public/mojom", "//chromeos/services/cros_healthd/public/mojom",
"//testing/gtest", "//testing/gtest",
......
// Copyright 2020 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 "chromeos/components/diagnostics_ui/backend/power_manager_client_conversions.h"
#include "base/i18n/time_formatting.h"
#include "base/time/time.h"
namespace chromeos {
namespace diagnostics {
mojom::BatteryState ConvertBatteryStateFromProto(
power_manager::PowerSupplyProperties::BatteryState battery_state) {
DCHECK_NE(power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT,
battery_state);
switch (battery_state) {
case power_manager::PowerSupplyProperties_BatteryState_CHARGING:
return mojom::BatteryState::kCharging;
case power_manager::PowerSupplyProperties_BatteryState_DISCHARGING:
return mojom::BatteryState::kDischarging;
case power_manager::PowerSupplyProperties_BatteryState_FULL:
return mojom::BatteryState::kFull;
default:
NOTREACHED();
return mojom::BatteryState::kFull;
}
}
mojom::ExternalPowerSource ConvertPowerSourceFromProto(
power_manager::PowerSupplyProperties::ExternalPower power_source) {
switch (power_source) {
case power_manager::PowerSupplyProperties_ExternalPower_AC:
return mojom::ExternalPowerSource::kAc;
case power_manager::PowerSupplyProperties_ExternalPower_USB:
return mojom::ExternalPowerSource::kUsb;
case power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED:
return mojom::ExternalPowerSource::kDisconnected;
default:
NOTREACHED();
return mojom::ExternalPowerSource::kDisconnected;
}
}
base::string16 ConstructPowerTime(
mojom::BatteryState battery_state,
const power_manager::PowerSupplyProperties& power_supply_props) {
if (battery_state == mojom::BatteryState::kFull) {
// Return an empty string if the battery is full.
return base::string16();
}
int64_t time_in_seconds;
if (battery_state == mojom::BatteryState::kCharging) {
time_in_seconds = power_supply_props.has_battery_time_to_full_sec()
? power_supply_props.battery_time_to_full_sec()
: -1;
} else {
DCHECK(battery_state == mojom::BatteryState::kDischarging);
time_in_seconds = power_supply_props.has_battery_time_to_empty_sec()
? power_supply_props.battery_time_to_empty_sec()
: -1;
}
if (power_supply_props.is_calculating_battery_time() || time_in_seconds < 0) {
// If power manager is still calculating battery time or |time_in_seconds|
// is negative (meaning power manager couldn't compute a reasonable time)
// return an empty string.
return base::string16();
}
const base::TimeDelta as_time_delta =
base::TimeDelta::FromSeconds(time_in_seconds);
base::string16 time_duration;
if (!base::TimeDurationFormat(as_time_delta, base::DURATION_WIDTH_NARROW,
&time_duration)) {
LOG(ERROR) << "Failed to format time duration " << as_time_delta;
return base::string16();
}
return time_duration;
}
} // namespace diagnostics
} // namespace chromeos
// Copyright 2020 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.
#ifndef CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_POWER_MANAGER_CLIENT_CONVERSIONS_H_
#define CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_POWER_MANAGER_CLIENT_CONVERSIONS_H_
#include "base/strings/string16.h"
#include "chromeos/components/diagnostics_ui/mojom/system_data_provider.mojom.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
namespace chromeos {
namespace diagnostics {
mojom::BatteryState ConvertBatteryStateFromProto(
power_manager::PowerSupplyProperties::BatteryState battery_state);
mojom::ExternalPowerSource ConvertPowerSourceFromProto(
power_manager::PowerSupplyProperties::ExternalPower power_source);
// Constructs a time-formatted string representing the amount of time remaining
// to either charge or discharge the battery. If the battery is full or the
// amount of time is unreliable / still being calculated, this returns an
// empty string. Otherwise, the time is returned in DURATION_WIDTH_NARROW
// format.
base::string16 ConstructPowerTime(
mojom::BatteryState battery_state,
const power_manager::PowerSupplyProperties& power_supply_props);
} // namespace diagnostics
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_POWER_MANAGER_CLIENT_CONVERSIONS_H_
// Copyright 2020 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 "chromeos/components/diagnostics_ui/backend/power_manager_client_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/diagnostics_ui/mojom/system_data_provider.mojom.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace diagnostics {
namespace {
// One of |time_to_full| or |time_to_empty| must be 0. The other can be either
// -1 to signify that the time is being calculated or a positive number to
// represent the number of seconds until empty or full.
power_manager::PowerSupplyProperties ConstructPowerSupplyProperties(
bool is_calculating_battery_time,
int64_t time_to_full,
int64_t time_to_empty) {
power_manager::PowerSupplyProperties props;
props.set_is_calculating_battery_time(is_calculating_battery_time);
props.set_battery_time_to_full_sec(time_to_full);
props.set_battery_time_to_empty_sec(time_to_empty);
return props;
}
} // namespace
class PowerManagerClientConversionsTest : public testing::Test {
public:
PowerManagerClientConversionsTest() = default;
~PowerManagerClientConversionsTest() override = default;
};
TEST_F(PowerManagerClientConversionsTest, BatteryState) {
EXPECT_EQ(mojom::BatteryState::kCharging,
ConvertBatteryStateFromProto(
power_manager::PowerSupplyProperties_BatteryState_CHARGING));
EXPECT_EQ(mojom::BatteryState::kDischarging,
ConvertBatteryStateFromProto(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING));
EXPECT_EQ(mojom::BatteryState::kFull,
ConvertBatteryStateFromProto(
power_manager::PowerSupplyProperties_BatteryState_FULL));
}
TEST_F(PowerManagerClientConversionsTest, PowerSource) {
EXPECT_EQ(mojom::ExternalPowerSource::kAc,
ConvertPowerSourceFromProto(
power_manager::PowerSupplyProperties_ExternalPower_AC));
EXPECT_EQ(mojom::ExternalPowerSource::kUsb,
ConvertPowerSourceFromProto(
power_manager::PowerSupplyProperties_ExternalPower_USB));
EXPECT_EQ(
mojom::ExternalPowerSource::kDisconnected,
ConvertPowerSourceFromProto(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED));
}
TEST_F(PowerManagerClientConversionsTest, PowerTime) {
// Full BatteryState returns an empty string
auto props = ConstructPowerSupplyProperties(false, 125, 0);
EXPECT_EQ(base::string16(),
ConstructPowerTime(mojom::BatteryState::kFull, props));
// If the battery is charging but is_calculating_battery_time is true, expect
// an empty string.
props = ConstructPowerSupplyProperties(true, 125, 0);
EXPECT_EQ(base::string16(),
ConstructPowerTime(mojom::BatteryState::kCharging, props));
// If the battery is discharging but is_calculating_battery_time is true,
// expect an empty string.
props = ConstructPowerSupplyProperties(true, 0, 125);
EXPECT_EQ(base::string16(),
ConstructPowerTime(mojom::BatteryState::kDischarging, props));
// If the battery is charging but time_to_full is -1, expect an empty string.
props = ConstructPowerSupplyProperties(false, -1, 0);
EXPECT_EQ(base::string16(),
ConstructPowerTime(mojom::BatteryState::kCharging, props));
// If the battery is discharging but time_to_empty is -1, expect an empty
// string.
props = ConstructPowerSupplyProperties(false, 0, -1);
EXPECT_EQ(base::string16(),
ConstructPowerTime(mojom::BatteryState::kDischarging, props));
// Battery charging with 11220 seconds (3h 7m) remaining.
props = ConstructPowerSupplyProperties(false, 11220, 0);
EXPECT_EQ(base::UTF8ToUTF16("3h 7m"),
ConstructPowerTime(mojom::BatteryState::kCharging, props));
// Battery discharging with 10380 seconds (2h 53m) remaining.
props = ConstructPowerSupplyProperties(false, 0, 10380);
EXPECT_EQ(base::UTF8ToUTF16("2h 53m"),
ConstructPowerTime(mojom::BatteryState::kDischarging, props));
}
} // namespace diagnostics
} // namespace chromeos
...@@ -30,6 +30,18 @@ struct BatteryInfo { ...@@ -30,6 +30,18 @@ struct BatteryInfo {
uint32 charge_full_design_milliamp_hours; uint32 charge_full_design_milliamp_hours;
}; };
enum ExternalPowerSource {
kAc,
kUsb,
kDisconnected,
};
enum BatteryState {
kCharging,
kDischarging,
kFull,
};
// Provides telemetric information about the system. This API is exposed to the // Provides telemetric information about the system. This API is exposed to the
// Diagnostics SWA. // Diagnostics SWA.
interface SystemDataProvider { interface SystemDataProvider {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment