1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108

base / power_monitor / battery_level_provider_unittest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/power_monitor/battery_level_provider.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace {

class FakeBatteryLevelProvider : public BatteryLevelProvider {
 public:
  using BatteryDetails = BatteryLevelProvider::BatteryDetails;

  FakeBatteryLevelProvider() = default;

  static BatteryState MakeBatteryState(
      const std::vector<BatteryDetails>& battery_details) {
    return BatteryLevelProvider::MakeBatteryState(battery_details);
  }
};

}  // namespace

using BatteryDetails = FakeBatteryLevelProvider::BatteryDetails;

TEST(BatteryLevelProviderTest, NoBattery) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState({});
  EXPECT_EQ(0, state.battery_count);
  EXPECT_TRUE(state.is_external_power_connected);
  EXPECT_FALSE(state.current_capacity.has_value());
  EXPECT_FALSE(state.full_charged_capacity.has_value());
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

TEST(BatteryLevelProviderTest, SingleBatteryWithExternalPower) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState(
      {BatteryDetails({.is_external_power_connected = true,
                       .current_capacity = 42,
                       .full_charged_capacity = 100})});
  EXPECT_EQ(1, state.battery_count);
  EXPECT_TRUE(state.is_external_power_connected);
  EXPECT_EQ(42U, state.current_capacity);
  EXPECT_EQ(100U, state.full_charged_capacity);
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

TEST(BatteryLevelProviderTest, SingleBatteryDischarging) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState(
      {BatteryDetails({.is_external_power_connected = false,
                       .current_capacity = 42,
                       .full_charged_capacity = 100})});
  EXPECT_EQ(1, state.battery_count);
  EXPECT_FALSE(state.is_external_power_connected);
  EXPECT_EQ(42U, state.current_capacity);
  EXPECT_EQ(100U, state.full_charged_capacity);
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

TEST(BatteryLevelProviderTest, MultipleBatteriesWithExternalPower) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState(
      {BatteryDetails({.is_external_power_connected = false,
                       .current_capacity = 42,
                       .full_charged_capacity = 100}),
       BatteryDetails({.is_external_power_connected = true,
                       .current_capacity = 10,
                       .full_charged_capacity = 100})});
  EXPECT_EQ(2, state.battery_count);
  EXPECT_TRUE(state.is_external_power_connected);
  EXPECT_EQ(std::nullopt, state.current_capacity);
  EXPECT_EQ(std::nullopt, state.full_charged_capacity);
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

TEST(BatteryLevelProviderTest, MultipleBatteriesDischarging) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState(
      {BatteryDetails({.is_external_power_connected = false,
                       .current_capacity = 42,
                       .full_charged_capacity = 100}),
       BatteryDetails({.is_external_power_connected = false,
                       .current_capacity = 10,
                       .full_charged_capacity = 100})});
  EXPECT_EQ(2, state.battery_count);
  EXPECT_FALSE(state.is_external_power_connected);
  EXPECT_EQ(std::nullopt, state.current_capacity);
  EXPECT_EQ(std::nullopt, state.full_charged_capacity);
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

TEST(BatteryLevelProviderTest, SingleBatteryMAh) {
  auto state = FakeBatteryLevelProvider::MakeBatteryState({BatteryDetails(
      {.is_external_power_connected = false,
       .current_capacity = 42,
       .full_charged_capacity = 100,
       .voltage_mv = 12,
       .charge_unit = BatteryLevelProvider::BatteryLevelUnit::kMAh})});
  EXPECT_EQ(1, state.battery_count);
  EXPECT_FALSE(state.is_external_power_connected);
  EXPECT_EQ(42U, state.current_capacity);
  EXPECT_EQ(100U, state.full_charged_capacity);
  EXPECT_TRUE(state.voltage_mv.has_value());
  EXPECT_EQ(12U, *state.voltage_mv);
  EXPECT_EQ(BatteryLevelProvider::BatteryLevelUnit::kMAh, state.charge_unit);
  EXPECT_NE(base::TimeTicks(), state.capture_time);
}

}  // namespace base