Commit f259009e authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Create PhoneStatusModel

This class includes phone metadata such as connection state and battery
levels. It also includes a few inner types such as enums representing
various states.

This CL adds a test for the class, but the class is not instantiated in
real code yet. A follow-up CL will utilize this class within a larger
phone model.

Bug: 1106937
Change-Id: I40be26a6381c70033d35fb0eae7cb8de2e50a09d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2365295Reviewed-by: default avatarRegan Hsu <hsuregan@chromium.org>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799923}
parent 40862cac
...@@ -22,6 +22,8 @@ static_library("phonehub") { ...@@ -22,6 +22,8 @@ static_library("phonehub") {
"notification_access_setup_operation.h", "notification_access_setup_operation.h",
"phone_hub_manager.cc", "phone_hub_manager.cc",
"phone_hub_manager.h", "phone_hub_manager.h",
"phone_status_model.cc",
"phone_status_model.h",
"pref_names.cc", "pref_names.cc",
"pref_names.h", "pref_names.h",
] ]
...@@ -46,6 +48,8 @@ static_library("test_support") { ...@@ -46,6 +48,8 @@ static_library("test_support") {
"fake_feature_status_provider.h", "fake_feature_status_provider.h",
"fake_notification_access_manager.cc", "fake_notification_access_manager.cc",
"fake_notification_access_manager.h", "fake_notification_access_manager.h",
"phone_model_test_util.cc",
"phone_model_test_util.h",
] ]
public_deps = [ ":phonehub" ] public_deps = [ ":phonehub" ]
...@@ -59,6 +63,7 @@ source_set("unit_tests") { ...@@ -59,6 +63,7 @@ source_set("unit_tests") {
sources = [ sources = [
"feature_status_provider_impl_unittest.cc", "feature_status_provider_impl_unittest.cc",
"notification_access_manager_impl_unittest.cc", "notification_access_manager_impl_unittest.cc",
"phone_status_model_unittest.cc",
] ]
deps = [ deps = [
......
// 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/phonehub/phone_model_test_util.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
namespace chromeos {
namespace phonehub {
const char kFakeMobileProviderName[] = "Fake Mobile Provider";
const PhoneStatusModel::MobileConnectionMetadata&
CreateFakeMobileConnectionMetadata() {
static const base::NoDestructor<PhoneStatusModel::MobileConnectionMetadata>
fake_mobile_connection_metadata{
{PhoneStatusModel::SignalStrength::kFourBars,
base::UTF8ToUTF16(kFakeMobileProviderName)}};
return *fake_mobile_connection_metadata;
}
const PhoneStatusModel& CreateFakePhoneStatusModel() {
static const base::NoDestructor<PhoneStatusModel> fake_status_model{
PhoneStatusModel::MobileStatus::kSimWithReception,
CreateFakeMobileConnectionMetadata(),
PhoneStatusModel::ChargingState::kNotCharging,
PhoneStatusModel::BatterySaverState::kOff,
/*battery_percentage=*/100u};
return *fake_status_model;
}
} // namespace phonehub
} // 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_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_
#include "chromeos/components/phonehub/phone_status_model.h"
namespace chromeos {
namespace phonehub {
extern const char kFakeMobileProviderName[];
// Creates fake data for use in tests.
const PhoneStatusModel::MobileConnectionMetadata&
CreateFakeMobileConnectionMetadata();
const PhoneStatusModel& CreateFakePhoneStatusModel();
} // namespace phonehub
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_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/phonehub/phone_status_model.h"
#include "chromeos/components/multidevice/logging/logging.h"
namespace chromeos {
namespace phonehub {
bool PhoneStatusModel::MobileConnectionMetadata::operator==(
const MobileConnectionMetadata& other) const {
return signal_strength == other.signal_strength &&
mobile_provider == other.mobile_provider;
}
bool PhoneStatusModel::MobileConnectionMetadata::operator!=(
const MobileConnectionMetadata& other) const {
return !(*this == other);
}
PhoneStatusModel::PhoneStatusModel(
MobileStatus mobile_status,
const base::Optional<MobileConnectionMetadata>& mobile_connection_metadata,
ChargingState charging_state,
BatterySaverState battery_saver_state,
uint32_t battery_percentage)
: mobile_status_(mobile_status),
mobile_connection_metadata_(mobile_connection_metadata),
charging_state_(charging_state),
battery_saver_state_(battery_saver_state),
battery_percentage_(battery_percentage) {
if (battery_percentage_ > 100) {
PA_LOG(WARNING) << "Provided battery percentage (" << battery_percentage_
<< "%) is >100%; setting to 100%.";
battery_percentage_ = 100;
}
if (mobile_status_ != MobileStatus::kSimWithReception &&
mobile_connection_metadata_.has_value()) {
PA_LOG(WARNING) << "Provided MobileStatus " << mobile_status_ << " "
<< "indicates no reception, but MobileConnectionMetadata "
<< *mobile_connection_metadata_
<< " was provided. Clearing "
<< "metadata.";
mobile_connection_metadata_.reset();
} else if (mobile_status_ == MobileStatus::kSimWithReception &&
!mobile_connection_metadata_.has_value()) {
PA_LOG(WARNING) << "MobileStatus is " << mobile_status_ << ", but no "
<< "connection metadata was provided. Changing status to "
<< MobileStatus::kSimButNoReception << ".";
mobile_status_ = MobileStatus::kSimButNoReception;
}
}
PhoneStatusModel::PhoneStatusModel(const PhoneStatusModel& other) = default;
PhoneStatusModel::~PhoneStatusModel() = default;
bool PhoneStatusModel::operator==(const PhoneStatusModel& other) const {
return mobile_status_ == other.mobile_status_ &&
mobile_connection_metadata_ == other.mobile_connection_metadata_ &&
charging_state_ == other.charging_state_ &&
battery_saver_state_ == other.battery_saver_state_ &&
battery_percentage_ == other.battery_percentage_;
}
bool PhoneStatusModel::operator!=(const PhoneStatusModel& other) const {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::MobileStatus mobile_status) {
switch (mobile_status) {
case PhoneStatusModel::MobileStatus::kNoSim:
stream << "[No SIM]";
break;
case PhoneStatusModel::MobileStatus::kSimButNoReception:
stream << "[SIM present; no reception]";
break;
case PhoneStatusModel::MobileStatus::kSimWithReception:
stream << "[SIM present with reception]";
break;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::SignalStrength signal_strength) {
switch (signal_strength) {
case PhoneStatusModel::SignalStrength::kZeroBars:
stream << "[0 bars / 4]";
break;
case PhoneStatusModel::SignalStrength::kOneBar:
stream << "[1 bar / 4]";
break;
case PhoneStatusModel::SignalStrength::kTwoBars:
stream << "[2 bars / 4]";
break;
case PhoneStatusModel::SignalStrength::kThreeBars:
stream << "[3 bars / 4]";
break;
case PhoneStatusModel::SignalStrength::kFourBars:
stream << "[4 bars / 4]";
break;
}
return stream;
}
std::ostream& operator<<(
std::ostream& stream,
PhoneStatusModel::MobileConnectionMetadata mobile_connection_metadata) {
stream << "{SignalStrength: " << mobile_connection_metadata.signal_strength
<< ", MobileProvider: \"" << mobile_connection_metadata.mobile_provider
<< "\"}";
return stream;
}
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::ChargingState charging_state) {
switch (charging_state) {
case PhoneStatusModel::ChargingState::kNotCharging:
stream << "[Not charging]";
break;
case PhoneStatusModel::ChargingState::kChargingAc:
stream << "[Charging via an AC adapter]";
break;
case PhoneStatusModel::ChargingState::kChargingUsb:
stream << "[Charging via USB]";
break;
}
return stream;
}
std::ostream& operator<<(
std::ostream& stream,
PhoneStatusModel::BatterySaverState battery_saver_state) {
switch (battery_saver_state) {
case PhoneStatusModel::BatterySaverState::kOff:
stream << "[Battery Saver off]";
break;
case PhoneStatusModel::BatterySaverState::kOn:
stream << "[Battery Saver on]";
break;
}
return stream;
}
} // namespace phonehub
} // 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_PHONEHUB_PHONE_STATUS_MODEL_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_PHONE_STATUS_MODEL_H_
#include <stdint.h>
#include <ostream>
#include "base/optional.h"
#include "base/strings/string16.h"
namespace chromeos {
namespace phonehub {
// Contains properties representing a phone's status, including mobile
// connection state and battery/power state.
class PhoneStatusModel {
public:
enum class MobileStatus {
// The phone does not have a physical SIM inserted or an eSIM profile set
// up.
kNoSim,
// The phone has a SIM, but it is not connected to a mobile network.
kSimButNoReception,
// The phone has a SIM and is connected to a mobile network using that SIM.
kSimWithReception
};
// Number of "bars" in the connection strength; only applies when the device
// has reception.
enum class SignalStrength {
kZeroBars,
kOneBar,
kTwoBars,
kThreeBars,
kFourBars
};
struct MobileConnectionMetadata {
bool operator==(const MobileConnectionMetadata& other) const;
bool operator!=(const MobileConnectionMetadata& other) const;
SignalStrength signal_strength;
// Name of the service provider (e.g., "Google Fi").
base::string16 mobile_provider;
};
enum class ChargingState {
// Not charging (i.e., on battery power).
kNotCharging,
// Charging via AC adapter.
kChargingAc,
// Charging via a USB connection.
kChargingUsb
};
// Android devices can enable "battery saver" mode, which causes the battery
// charge to last longer by reducing/eliminating functionality with
// significant power impact.
enum class BatterySaverState { kOff, kOn };
// Note: If |mobile_status| is not kSimWithReception,
// |mobile_connection_metadata| should be null.
PhoneStatusModel(MobileStatus mobile_status,
const base::Optional<MobileConnectionMetadata>&
mobile_connection_metadata,
ChargingState charging_state,
BatterySaverState battery_saver_state,
uint32_t battery_percentage);
PhoneStatusModel(const PhoneStatusModel& other);
~PhoneStatusModel();
bool operator==(const PhoneStatusModel& other) const;
bool operator!=(const PhoneStatusModel& other) const;
MobileStatus mobile_status() const { return mobile_status_; }
// Note: Null when mobile_status() is not kSimWithReception.
const base::Optional<MobileConnectionMetadata>& mobile_connection_metadata()
const {
return mobile_connection_metadata_;
}
ChargingState charging_state() const { return charging_state_; }
BatterySaverState battery_saver_state() const { return battery_saver_state_; }
uint32_t battery_percentage() const { return battery_percentage_; }
private:
MobileStatus mobile_status_;
base::Optional<MobileConnectionMetadata> mobile_connection_metadata_;
ChargingState charging_state_;
BatterySaverState battery_saver_state_;
uint32_t battery_percentage_;
};
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::MobileStatus mobile_status);
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::SignalStrength signal_strength);
std::ostream& operator<<(
std::ostream& stream,
PhoneStatusModel::MobileConnectionMetadata mobile_connection_metadata);
std::ostream& operator<<(std::ostream& stream,
PhoneStatusModel::ChargingState charging_state);
std::ostream& operator<<(
std::ostream& stream,
PhoneStatusModel::BatterySaverState battery_saver_state);
} // namespace phonehub
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_PHONEHUB_PHONE_STATUS_MODEL_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/phonehub/phone_status_model.h"
#include "chromeos/components/phonehub/phone_model_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace phonehub {
TEST(PhoneStatusModelTest, Initialization) {
PhoneStatusModel success(PhoneStatusModel::MobileStatus::kSimWithReception,
CreateFakeMobileConnectionMetadata(),
PhoneStatusModel::ChargingState::kNotCharging,
PhoneStatusModel::BatterySaverState::kOff,
/*battery_percentage=*/100u);
EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimWithReception,
success.mobile_status());
EXPECT_EQ(CreateFakeMobileConnectionMetadata(),
*success.mobile_connection_metadata());
EXPECT_EQ(PhoneStatusModel::ChargingState::kNotCharging,
success.charging_state());
EXPECT_EQ(PhoneStatusModel::BatterySaverState::kOff,
success.battery_saver_state());
EXPECT_EQ(100u, success.battery_percentage());
// If battery is >100, it is set to 100.
PhoneStatusModel high_battery(
PhoneStatusModel::MobileStatus::kSimWithReception,
CreateFakeMobileConnectionMetadata(),
PhoneStatusModel::ChargingState::kNotCharging,
PhoneStatusModel::BatterySaverState::kOff,
/*battery_percentage=*/1000u);
EXPECT_EQ(100u, high_battery.battery_percentage());
// If the MobileStatus does not indicate reception, connection metadata should
// be cleared.
PhoneStatusModel no_sim(PhoneStatusModel::MobileStatus::kNoSim,
CreateFakeMobileConnectionMetadata(),
PhoneStatusModel::ChargingState::kNotCharging,
PhoneStatusModel::BatterySaverState::kOff,
/*battery_percentage=*/100u);
EXPECT_FALSE(no_sim.mobile_connection_metadata().has_value());
// If the MobileStatus does indicate reception but no connection metadata is
// available, the status is set back to no reception.
PhoneStatusModel no_connection_metadata(
PhoneStatusModel::MobileStatus::kSimWithReception,
/*mobile_connection_metadata=*/base::nullopt,
PhoneStatusModel::ChargingState::kNotCharging,
PhoneStatusModel::BatterySaverState::kOff,
/*battery_percentage=*/100u);
EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimButNoReception,
no_connection_metadata.mobile_status());
}
} // namespace phonehub
} // namespace chromeos
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