blob: 8d3ed325853b26958351393484e0edc4b12dc915 (
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
|
// Copyright 2015 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 <content/browser/background_sync/background_sync_power_observer.h>
#include "base/run_loop.h"
#include "base/test/power_monitor_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
class BackgroundSyncPowerObserverTest : public testing::Test {
protected:
BackgroundSyncPowerObserverTest() : power_changed_count_(0) {
power_monitor_source_ = new base::PowerMonitorTestSource();
power_monitor_.reset(new base::PowerMonitor(
scoped_ptr<base::PowerMonitorSource>(power_monitor_source_)));
power_observer_.reset(new BackgroundSyncPowerObserver(
base::Bind(&BackgroundSyncPowerObserverTest::OnPowerChanged,
base::Unretained(this))));
}
void SetOnBatteryPower(bool on_battery_power) {
power_monitor_source_->GeneratePowerStateEvent(on_battery_power);
}
void OnPowerChanged() { power_changed_count_++; }
// power_monitor_source_ is owned by power_monitor_
base::PowerMonitorTestSource* power_monitor_source_;
scoped_ptr<base::PowerMonitor> power_monitor_;
scoped_ptr<BackgroundSyncPowerObserver> power_observer_;
int power_changed_count_;
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPowerObserverTest);
};
TEST_F(BackgroundSyncPowerObserverTest, PowerChangeInvokesCallback) {
SetOnBatteryPower(true);
power_changed_count_ = 0;
SetOnBatteryPower(false);
EXPECT_EQ(1, power_changed_count_);
SetOnBatteryPower(true);
EXPECT_EQ(2, power_changed_count_);
SetOnBatteryPower(true);
EXPECT_EQ(2, power_changed_count_);
}
TEST_F(BackgroundSyncPowerObserverTest, PowerSufficientAuto) {
SetOnBatteryPower(false);
EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AUTO));
SetOnBatteryPower(true);
EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AUTO));
}
TEST_F(BackgroundSyncPowerObserverTest, PowerSufficientAvoidDraining) {
SetOnBatteryPower(false);
EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AVOID_DRAINING));
SetOnBatteryPower(true);
EXPECT_FALSE(power_observer_->PowerSufficient(POWER_STATE_AVOID_DRAINING));
}
} // namespace
} // namespace content
|