OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "content/browser/browser_thread_impl.h" |
| 8 #include "content/browser/power_profiler/power_profiler_service.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 namespace { |
| 14 |
| 15 const int kNumEvents = 3; |
| 16 |
| 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 virtual ~TestPowerDataProvider() {} |
| 22 |
| 23 virtual PowerEventVector GetData() OVERRIDE { |
| 24 PowerEventVector events; |
| 25 if (num_events_to_send_ == 0) |
| 26 return events; |
| 27 |
| 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); |
| 33 |
| 34 num_events_to_send_--; |
| 35 return events; |
| 36 } |
| 37 |
| 38 private: |
| 39 int num_events_to_send_; |
| 40 DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider); |
| 41 }; |
| 42 |
| 43 class TestPowerProfilerObserver : public PowerProfilerObserver { |
| 44 public: |
| 45 TestPowerProfilerObserver() |
| 46 : valid_event_count_(0), |
| 47 total_num_events_received_(0) {} |
| 48 virtual ~TestPowerProfilerObserver() {} |
| 49 |
| 50 virtual void OnPowerEvent(const PowerEventVector& events) OVERRIDE { |
| 51 if (IsValidEvent(events[0])) |
| 52 ++valid_event_count_; |
| 53 |
| 54 total_num_events_received_++; |
| 55 if (total_num_events_received_ >= kNumEvents) { |
| 56 // All expected events received, exiting. |
| 57 quit_closure_.Run(); |
| 58 } |
| 59 } |
| 60 |
| 61 int valid_event_count() const { return valid_event_count_; } |
| 62 void set_quit_closure(base::Closure closure) { quit_closure_ = closure; } |
| 63 |
| 64 private: |
| 65 bool IsValidEvent(const PowerEvent& event) { |
| 66 return event.type == PowerEvent::SOC_PACKAGE && |
| 67 !event.time.is_null() && |
| 68 event.value > 0; |
| 69 } |
| 70 |
| 71 int valid_event_count_; |
| 72 int total_num_events_received_; |
| 73 base::Closure quit_closure_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver); |
| 76 }; |
| 77 |
| 78 } // namespace |
| 79 |
| 80 class PowerProfilerServiceTest : public testing::Test { |
| 81 public: |
| 82 void ServiceStartTest() { |
| 83 service_.reset(new PowerProfilerService( |
| 84 make_scoped_ptr<PowerDataProvider>( |
| 85 new TestPowerDataProvider(kNumEvents)), |
| 86 message_loop_.message_loop_proxy(), |
| 87 base::TimeDelta::FromMilliseconds(1))); |
| 88 EXPECT_TRUE(service_->IsAvailable()); |
| 89 } |
| 90 |
| 91 void AddObserverTest() { |
| 92 service_->AddObserver(&observer_); |
| 93 |
| 94 // No PowerEvents received. |
| 95 EXPECT_EQ(observer_.valid_event_count(), 0); |
| 96 } |
| 97 |
| 98 void RemoveObserverTest() { |
| 99 service_->RemoveObserver(&observer_); |
| 100 |
| 101 // Received |kNumEvents| events. |
| 102 EXPECT_EQ(observer_.valid_event_count(), kNumEvents); |
| 103 } |
| 104 |
| 105 protected: |
| 106 PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
| 107 virtual ~PowerProfilerServiceTest() {} |
| 108 |
| 109 void RegisterQuitClosure(base::Closure closure) { |
| 110 observer_.set_quit_closure(closure); |
| 111 } |
| 112 |
| 113 private: |
| 114 scoped_ptr<PowerProfilerService> service_; |
| 115 TestPowerProfilerObserver observer_; |
| 116 |
| 117 // UI thread. |
| 118 base::MessageLoopForUI message_loop_; |
| 119 BrowserThreadImpl ui_thread_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest); |
| 122 }; |
| 123 |
| 124 // Test whether PowerProfilerService dispatches power events to observer |
| 125 // properly. |
| 126 TEST_F(PowerProfilerServiceTest, AvailableService) { |
| 127 base::RunLoop run_loop; |
| 128 RegisterQuitClosure(run_loop.QuitClosure()); |
| 129 |
| 130 ServiceStartTest(); |
| 131 AddObserverTest(); |
| 132 |
| 133 run_loop.Run(); |
| 134 |
| 135 RemoveObserverTest(); |
| 136 } |
| 137 |
| 138 } // namespace content |
OLD | NEW |