1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/power/power_data_collector.h"
6 #include "chromeos/dbus/dbus_thread_manager.h"
7 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class PowerDataCollectorTest
: public testing::Test
{
14 PowerDataCollectorTest() : power_data_collector_(NULL
) {}
15 ~PowerDataCollectorTest() override
{}
17 void SetUp() override
{
18 DBusThreadManager::Initialize();
19 PowerDataCollector::InitializeForTesting();
20 power_data_collector_
= PowerDataCollector::Get();
23 void TearDown() override
{
24 PowerDataCollector::Shutdown();
25 DBusThreadManager::Shutdown();
26 power_data_collector_
= NULL
;
30 PowerDataCollector
* power_data_collector_
;
33 TEST_F(PowerDataCollectorTest
, PowerChanged
) {
34 power_manager::PowerSupplyProperties prop1
, prop2
;
36 prop1
.set_external_power(power_manager::PowerSupplyProperties::DISCONNECTED
);
37 prop1
.set_battery_percent(20.00);
38 power_data_collector_
->PowerChanged(prop1
);
39 const std::deque
<PowerDataCollector::PowerSupplySample
>& data1
=
40 power_data_collector_
->power_supply_data();
41 ASSERT_EQ(static_cast<size_t>(1), data1
.size());
42 EXPECT_DOUBLE_EQ(prop1
.battery_percent(), data1
[0].battery_percent
);
43 EXPECT_FALSE(data1
[0].external_power
);
45 prop2
.set_external_power(power_manager::PowerSupplyProperties::AC
);
46 prop2
.set_battery_percent(100.00);
47 power_data_collector_
->PowerChanged(prop2
);
48 const std::deque
<PowerDataCollector::PowerSupplySample
>& data2
=
49 power_data_collector_
->power_supply_data();
50 ASSERT_EQ(static_cast<size_t>(2), data2
.size());
51 EXPECT_DOUBLE_EQ(prop2
.battery_percent(), data2
[1].battery_percent
);
52 EXPECT_TRUE(data2
[1].external_power
);
55 TEST_F(PowerDataCollectorTest
, SuspendDone
) {
56 power_data_collector_
->SuspendDone(base::TimeDelta::FromSeconds(10));
57 const std::deque
<PowerDataCollector::SystemResumedSample
>& data1
=
58 power_data_collector_
->system_resumed_data();
59 ASSERT_EQ(static_cast<size_t>(1), data1
.size());
60 ASSERT_EQ(static_cast<int64
>(10), data1
[0].sleep_duration
.InSeconds());
62 power_data_collector_
->SuspendDone(base::TimeDelta::FromSeconds(20));
63 const std::deque
<PowerDataCollector::SystemResumedSample
>& data2
=
64 power_data_collector_
->system_resumed_data();
65 ASSERT_EQ(static_cast<size_t>(2), data2
.size());
66 ASSERT_EQ(static_cast<int64
>(20), data2
[1].sleep_duration
.InSeconds());
69 TEST_F(PowerDataCollectorTest
, AddSample
) {
70 std::deque
<PowerDataCollector::PowerSupplySample
> sample_deque
;
71 PowerDataCollector::PowerSupplySample sample1
, sample2
;
72 sample1
.time
= base::Time::FromInternalValue(1000);
73 sample2
.time
= sample1
.time
+
74 base::TimeDelta::FromSeconds(PowerDataCollector::kSampleTimeLimitSec
+ 1);
76 AddSample(&sample_deque
, sample1
);
77 ASSERT_EQ(static_cast<size_t>(1), sample_deque
.size());
79 AddSample(&sample_deque
, sample2
);
80 ASSERT_EQ(static_cast<size_t>(1), sample_deque
.size());
81 EXPECT_EQ(sample2
.time
.ToInternalValue(),
82 sample_deque
[0].time
.ToInternalValue());
85 } // namespace chromeos