blob: 06a8b89004bdade01e389492b2f21f17487a7aec (
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
// Copyright 2014 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 "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/power_profiler/power_profiler_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
const int kNumEvents = 3;
const int kDefaultSamplePeriodMs = 50;
// Provide a set number of power events.
class TestPowerDataProvider : public PowerDataProvider {
public:
TestPowerDataProvider(int count) : num_events_to_send_(count) {}
~TestPowerDataProvider() override {}
PowerEventVector GetData() override {
PowerEventVector events;
if (num_events_to_send_ == 0)
return events;
PowerEvent event;
event.type = PowerEvent::SOC_PACKAGE;
event.time = base::TimeTicks::Now();
event.value = 1.0;
events.push_back(event);
num_events_to_send_--;
return events;
}
base::TimeDelta GetSamplingRate() override {
return base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs);
}
AccuracyLevel GetAccuracyLevel() override { return High; }
private:
int num_events_to_send_;
DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider);
};
class TestPowerProfilerObserver : public PowerProfilerObserver {
public:
TestPowerProfilerObserver()
: valid_event_count_(0),
total_num_events_received_(0) {}
~TestPowerProfilerObserver() override {}
void OnPowerEvent(const PowerEventVector& events) override {
if (IsValidEvent(events[0]))
++valid_event_count_;
total_num_events_received_++;
if (total_num_events_received_ >= kNumEvents) {
// All expected events received, exiting.
quit_closure_.Run();
}
}
int valid_event_count() const { return valid_event_count_; }
void set_quit_closure(base::Closure closure) { quit_closure_ = closure; }
private:
bool IsValidEvent(const PowerEvent& event) {
return event.type == PowerEvent::SOC_PACKAGE &&
!event.time.is_null() &&
event.value > 0;
}
int valid_event_count_;
int total_num_events_received_;
base::Closure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver);
};
} // namespace
class PowerProfilerServiceTest : public testing::Test {
public:
void ServiceStartTest() {
service_.reset(new PowerProfilerService(
make_scoped_ptr<PowerDataProvider>(
new TestPowerDataProvider(kNumEvents)),
message_loop_.message_loop_proxy(),
base::TimeDelta::FromMilliseconds(1)));
EXPECT_TRUE(service_->IsAvailable());
}
void AddObserverTest() {
service_->AddObserver(&observer_);
// No PowerEvents received.
EXPECT_EQ(observer_.valid_event_count(), 0);
}
void RemoveObserverTest() {
service_->RemoveObserver(&observer_);
// Received |kNumEvents| events.
EXPECT_EQ(observer_.valid_event_count(), kNumEvents);
}
protected:
PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
virtual ~PowerProfilerServiceTest() {}
void RegisterQuitClosure(base::Closure closure) {
observer_.set_quit_closure(closure);
}
private:
scoped_ptr<PowerProfilerService> service_;
TestPowerProfilerObserver observer_;
// UI thread.
base::MessageLoopForUI message_loop_;
BrowserThreadImpl ui_thread_;
DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest);
};
// Test whether PowerProfilerService dispatches power events to observer
// properly.
TEST_F(PowerProfilerServiceTest, AvailableService) {
base::RunLoop run_loop;
RegisterQuitClosure(run_loop.QuitClosure());
ServiceStartTest();
AddObserverTest();
run_loop.Run();
RemoveObserverTest();
}
} // namespace content
|