Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / power_profiler / power_profiler_service_unittest.cc
blob96e96d14ac66e5ab1cf2f30b40cbca0082e93416
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"
10 namespace content {
12 namespace {
14 const int kNumEvents = 3;
15 const int kDefaultSamplePeriodMs = 50;
17 // Provide a set number of power events.
18 class TestPowerDataProvider : public PowerDataProvider {
19 public:
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)
26 return events;
28 PowerEvent event;
29 event.type = PowerEvent::SOC_PACKAGE;
30 event.time = base::TimeTicks::Now();
31 event.value = 1.0;
32 events.push_back(event);
34 num_events_to_send_--;
35 return events;
38 base::TimeDelta GetSamplingRate() override {
39 return base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs);
42 AccuracyLevel GetAccuracyLevel() override { return High; }
44 private:
45 int num_events_to_send_;
46 DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider);
49 class TestPowerProfilerObserver : public PowerProfilerObserver {
50 public:
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]))
58 ++valid_event_count_;
60 total_num_events_received_++;
61 if (total_num_events_received_ >= kNumEvents) {
62 // All expected events received, exiting.
63 quit_closure_.Run();
67 int valid_event_count() const { return valid_event_count_; }
68 void set_quit_closure(base::Closure closure) { quit_closure_ = closure; }
70 private:
71 bool IsValidEvent(const PowerEvent& event) {
72 return event.type == PowerEvent::SOC_PACKAGE &&
73 !event.time.is_null() &&
74 event.value > 0;
77 int valid_event_count_;
78 int total_num_events_received_;
79 base::Closure quit_closure_;
81 DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver);
84 } // namespace
86 class PowerProfilerServiceTest : public testing::Test {
87 public:
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);
110 protected:
111 PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
112 ~PowerProfilerServiceTest() override {}
114 void RegisterQuitClosure(base::Closure closure) {
115 observer_.set_quit_closure(closure);
118 private:
119 scoped_ptr<PowerProfilerService> service_;
120 TestPowerProfilerObserver observer_;
122 // UI thread.
123 base::MessageLoopForUI message_loop_;
124 BrowserThreadImpl ui_thread_;
126 DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest);
129 // Test whether PowerProfilerService dispatches power events to observer
130 // properly.
131 TEST_F(PowerProfilerServiceTest, AvailableService) {
132 base::RunLoop run_loop;
133 RegisterQuitClosure(run_loop.QuitClosure());
135 ServiceStartTest();
136 AddObserverTest();
138 run_loop.Run();
140 RemoveObserverTest();
143 } // namespace content