Home | History | Annotate | Download | only in power
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ash/system/chromeos/power/power_status.h"
      6 
      7 #include <set>
      8 #include <string>
      9 
     10 #include "base/command_line.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "chromeos/dbus/dbus_thread_manager.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "third_party/cros_system_api/dbus/service_constants.h"
     16 
     17 namespace ash {
     18 namespace internal {
     19 
     20 namespace {
     21 
     22 class TestObserver : public PowerStatus::Observer {
     23  public:
     24   TestObserver() : power_changed_count_(0) {}
     25   virtual ~TestObserver() {}
     26 
     27   int power_changed_count() const { return power_changed_count_; }
     28 
     29   // PowerStatus::Observer overrides:
     30   virtual void OnPowerStatusChanged() OVERRIDE { ++power_changed_count_; }
     31 
     32  private:
     33   int power_changed_count_;
     34 
     35   DISALLOW_COPY_AND_ASSIGN(TestObserver);
     36 };
     37 
     38 }  // namespace
     39 
     40 class PowerStatusTest : public testing::Test {
     41  public:
     42   PowerStatusTest() : power_status_(NULL) {}
     43   virtual ~PowerStatusTest() {}
     44 
     45   virtual void SetUp() OVERRIDE {
     46     chromeos::DBusThreadManager::InitializeWithStub();
     47     PowerStatus::Initialize();
     48     power_status_ = PowerStatus::Get();
     49     test_observer_.reset(new TestObserver);
     50     power_status_->AddObserver(test_observer_.get());
     51   }
     52 
     53   virtual void TearDown() OVERRIDE {
     54     power_status_->RemoveObserver(test_observer_.get());
     55     test_observer_.reset();
     56     PowerStatus::Shutdown();
     57     chromeos::DBusThreadManager::Shutdown();
     58   }
     59 
     60  protected:
     61   base::MessageLoopForUI message_loop_;
     62   PowerStatus* power_status_;  // Not owned.
     63   scoped_ptr<TestObserver> test_observer_;
     64 
     65  private:
     66   DISALLOW_COPY_AND_ASSIGN(PowerStatusTest);
     67 };
     68 
     69 TEST_F(PowerStatusTest, InitializeAndUpdate) {
     70   // Test that the initial power supply state should be acquired after
     71   // PowerStatus is instantiated. This depends on
     72   // PowerManagerClientStubImpl, which responds to power status update
     73   // requests, pretends there is a battery present, and generates some valid
     74   // power supply status data.
     75   message_loop_.RunUntilIdle();
     76   EXPECT_EQ(1, test_observer_->power_changed_count());
     77 
     78   // Test RequestUpdate, test_obsever_ should be notified for power suuply
     79   // status change.
     80   power_status_->RequestStatusUpdate();
     81   message_loop_.RunUntilIdle();
     82   EXPECT_EQ(2, test_observer_->power_changed_count());
     83 }
     84 
     85 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) {
     86   EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
     87       base::TimeDelta::FromSeconds(-1)));
     88   EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
     89       base::TimeDelta::FromSeconds(0)));
     90   EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
     91       base::TimeDelta::FromSeconds(59)));
     92   EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
     93       base::TimeDelta::FromSeconds(60)));
     94   EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
     95       base::TimeDelta::FromSeconds(600)));
     96   EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
     97       base::TimeDelta::FromSeconds(3600)));
     98   EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime(
     99       base::TimeDelta::FromSeconds(
    100           PowerStatus::kMaxBatteryTimeToDisplaySec)));
    101   EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime(
    102       base::TimeDelta::FromSeconds(
    103           PowerStatus::kMaxBatteryTimeToDisplaySec + 1)));
    104 }
    105 
    106 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) {
    107   int hours = 0, minutes = 0;
    108   PowerStatus::SplitTimeIntoHoursAndMinutes(
    109       base::TimeDelta::FromSeconds(0), &hours, &minutes);
    110   EXPECT_EQ(0, hours);
    111   EXPECT_EQ(0, minutes);
    112 
    113   PowerStatus::SplitTimeIntoHoursAndMinutes(
    114       base::TimeDelta::FromSeconds(60), &hours, &minutes);
    115   EXPECT_EQ(0, hours);
    116   EXPECT_EQ(1, minutes);
    117 
    118   PowerStatus::SplitTimeIntoHoursAndMinutes(
    119       base::TimeDelta::FromSeconds(3600), &hours, &minutes);
    120   EXPECT_EQ(1, hours);
    121   EXPECT_EQ(0, minutes);
    122 
    123   PowerStatus::SplitTimeIntoHoursAndMinutes(
    124       base::TimeDelta::FromSeconds(3600 + 60), &hours, &minutes);
    125   EXPECT_EQ(1, hours);
    126   EXPECT_EQ(1, minutes);
    127 
    128   PowerStatus::SplitTimeIntoHoursAndMinutes(
    129       base::TimeDelta::FromSeconds(7 * 3600 + 23 * 60), &hours, &minutes);
    130   EXPECT_EQ(7, hours);
    131   EXPECT_EQ(23, minutes);
    132 
    133   // Check that minutes are rounded.
    134   PowerStatus::SplitTimeIntoHoursAndMinutes(
    135       base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 30), &hours, &minutes);
    136   EXPECT_EQ(2, hours);
    137   EXPECT_EQ(4, minutes);
    138 
    139   PowerStatus::SplitTimeIntoHoursAndMinutes(
    140       base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 29), &hours, &minutes);
    141   EXPECT_EQ(2, hours);
    142   EXPECT_EQ(3, minutes);
    143 }
    144 
    145 }  // namespace internal
    146 }  // namespace ash
    147