summaryrefslogtreecommitdiffstats
path: root/chromeos/power/power_manager_handler_unittest.cc
blob: e66c7cb746644a0dfe8cb6429923f20ef0699c09 (plain)
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
// Copyright (c) 2013 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/power/power_manager_handler.h"

#include <set>
#include <string>

#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

namespace {

void ValidatePowerSupplyStatus(const PowerSupplyStatus& status) {
  EXPECT_TRUE(status.battery_is_present);
  EXPECT_GT(status.battery_percentage, 0.0);
  if (status.battery_state == PowerSupplyStatus::DISCHARGING) {
    EXPECT_GT(status.battery_seconds_to_empty, 0);
    EXPECT_EQ(status.battery_seconds_to_full, 0);
  } else {
    EXPECT_GT(status.battery_seconds_to_full, 0);
    EXPECT_EQ(status.battery_seconds_to_empty, 0);
  }
}

class TestObserver : public PowerManagerHandler::Observer {
 public:
  TestObserver() : power_changed_count_(0) {
  }
  virtual ~TestObserver() {}

  virtual void OnPowerStatusChanged(
      const PowerSupplyStatus& power_status) OVERRIDE {
    ++power_changed_count_;
    ValidatePowerSupplyStatus(power_status);
  }

  int power_changed_count() const { return power_changed_count_; }

 private:
  int power_changed_count_;

  DISALLOW_COPY_AND_ASSIGN(TestObserver);
};

}  // namespace

class PowerManagerHandlerTest : public testing::Test {
 public:
  PowerManagerHandlerTest() {}
  virtual ~PowerManagerHandlerTest() {}

  virtual void SetUp() OVERRIDE {
    // Initialize DBusThreadManager with a stub implementation.
    DBusThreadManager::InitializeWithStub();

    power_handler_.reset(new PowerManagerHandler);
    test_observer_.reset(new TestObserver);
    power_handler_->AddObserver(test_observer_.get());
  }

  virtual void TearDown() OVERRIDE {
    power_handler_.reset();
    test_observer_.reset();
    DBusThreadManager::Shutdown();
  }

 protected:
  base::MessageLoopForUI message_loop_;
  scoped_ptr<PowerManagerHandler> power_handler_;
  scoped_ptr<TestObserver> test_observer_;

 private:
  DISALLOW_COPY_AND_ASSIGN(PowerManagerHandlerTest);
};

TEST_F(PowerManagerHandlerTest, PowerManagerInitializeAndUpdate) {
  // Test that the initial power supply state should be acquired after
  // PowerManagerHandler is instantiated. This depends on
  // PowerManagerClientStubImpl, which responds to power status update
  // requests, pretends there is a battery present, and generate some
  // valid power supply status data.
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test_observer_->power_changed_count(), 0);
  PowerSupplyStatus init_status = power_handler_->GetPowerSupplyStatus();
  ValidatePowerSupplyStatus(init_status);

  // Test RequestUpdate, test_obsever_ should be notified for power suuply
  // status change.
  power_handler_->RequestStatusUpdate();
  message_loop_.RunUntilIdle();
  EXPECT_GE(test_observer_->power_changed_count(), 1);
}

}  // namespace chromeos