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 "base/run_loop.h"
6 #include "content/browser/browser_thread_impl.h"
7 #include "content/browser/power_profiler/power_profiler_service.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 const int kNumEvents
= 3;
15 const int kDefaultSamplePeriodMs
= 50;
17 // Provide a set number of power events.
18 class TestPowerDataProvider
: public PowerDataProvider
{
20 TestPowerDataProvider(int count
) : num_events_to_send_(count
) {}
21 ~TestPowerDataProvider() override
{}
23 PowerEventVector
GetData() override
{
24 PowerEventVector events
;
25 if (num_events_to_send_
== 0)
29 event
.type
= PowerEvent::SOC_PACKAGE
;
30 event
.time
= base::TimeTicks::Now();
32 events
.push_back(event
);
34 num_events_to_send_
--;
38 base::TimeDelta
GetSamplingRate() override
{
39 return base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs
);
42 AccuracyLevel
GetAccuracyLevel() override
{ return High
; }
45 int num_events_to_send_
;
46 DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider
);
49 class TestPowerProfilerObserver
: public PowerProfilerObserver
{
51 TestPowerProfilerObserver()
52 : valid_event_count_(0),
53 total_num_events_received_(0) {}
54 ~TestPowerProfilerObserver() override
{}
56 void OnPowerEvent(const PowerEventVector
& events
) override
{
57 if (IsValidEvent(events
[0]))
60 total_num_events_received_
++;
61 if (total_num_events_received_
>= kNumEvents
) {
62 // All expected events received, exiting.
67 int valid_event_count() const { return valid_event_count_
; }
68 void set_quit_closure(base::Closure closure
) { quit_closure_
= closure
; }
71 bool IsValidEvent(const PowerEvent
& event
) {
72 return event
.type
== PowerEvent::SOC_PACKAGE
&&
73 !event
.time
.is_null() &&
77 int valid_event_count_
;
78 int total_num_events_received_
;
79 base::Closure quit_closure_
;
81 DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver
);
86 class PowerProfilerServiceTest
: public testing::Test
{
88 void ServiceStartTest() {
89 service_
.reset(new PowerProfilerService(
90 make_scoped_ptr
<PowerDataProvider
>(
91 new TestPowerDataProvider(kNumEvents
)),
92 message_loop_
.task_runner(), base::TimeDelta::FromMilliseconds(1)));
93 EXPECT_TRUE(service_
->IsAvailable());
96 void AddObserverTest() {
97 service_
->AddObserver(&observer_
);
99 // No PowerEvents received.
100 EXPECT_EQ(observer_
.valid_event_count(), 0);
103 void RemoveObserverTest() {
104 service_
->RemoveObserver(&observer_
);
106 // Received |kNumEvents| events.
107 EXPECT_EQ(observer_
.valid_event_count(), kNumEvents
);
111 PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI
, &message_loop_
) {}
112 ~PowerProfilerServiceTest() override
{}
114 void RegisterQuitClosure(base::Closure closure
) {
115 observer_
.set_quit_closure(closure
);
119 scoped_ptr
<PowerProfilerService
> service_
;
120 TestPowerProfilerObserver observer_
;
123 base::MessageLoopForUI message_loop_
;
124 BrowserThreadImpl ui_thread_
;
126 DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest
);
129 // Test whether PowerProfilerService dispatches power events to observer
131 TEST_F(PowerProfilerServiceTest
, AvailableService
) {
132 base::RunLoop run_loop
;
133 RegisterQuitClosure(run_loop
.QuitClosure());
140 RemoveObserverTest();
143 } // namespace content