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 const int kNumObservers = 3; |
| 17 |
| 18 // Provide a set number of power events. |
| 19 class TestPowerDataProvider : public PowerDataProvider { |
| 20 public: |
| 21 TestPowerDataProvider(int count) : num_events_to_send_(count) {} |
| 22 virtual ~TestPowerDataProvider() {} |
| 23 |
| 24 virtual PowerEventVector GetData() OVERRIDE { |
| 25 PowerEventVector events; |
| 26 if (num_events_to_send_ == 0) |
| 27 return events; |
| 28 |
| 29 PowerEvent event; |
| 30 event.type = PowerEvent::SOC_PACKAGE; |
| 31 event.time = base::TimeTicks::Now(); |
| 32 event.value = 1.0; |
| 33 events.push_back(event); |
| 34 |
| 35 num_events_to_send_--; |
| 36 return events; |
| 37 } |
| 38 |
| 39 private: |
| 40 int num_events_to_send_; |
| 41 DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider); |
| 42 }; |
| 43 |
| 44 class TestPowerProfilerObserver : public PowerProfilerObserver { |
| 45 public: |
| 46 TestPowerProfilerObserver() |
| 47 : valid_event_count_(0), |
| 48 total_num_events_received_(NULL) {} |
| 49 virtual ~TestPowerProfilerObserver() {} |
| 50 |
| 51 virtual void OnPowerEvent(const PowerEventVector& events) OVERRIDE { |
| 52 if (IsValidEvent(events[0])) { |
| 53 ++valid_event_count_; |
| 54 (*total_num_events_received_)++; |
| 55 if (*total_num_events_received_ >= kNumEvents * kNumObservers) { |
| 56 // All expected events received, exiting. |
| 57 base::MessageLoop::current()->Quit(); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 int valid_event_count() const { return valid_event_count_; } |
| 63 |
| 64 void set_total_num_events_received(int* total_num_events_received) { |
| 65 total_num_events_received_ = total_num_events_received; |
| 66 } |
| 67 |
| 68 private: |
| 69 bool IsValidEvent(const PowerEvent& event) { |
| 70 return event.type == PowerEvent::SOC_PACKAGE && |
| 71 !event.time.is_null() && |
| 72 event.value > 0; |
| 73 } |
| 74 |
| 75 int valid_event_count_; |
| 76 int* total_num_events_received_; |
| 77 DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver); |
| 78 }; |
| 79 |
| 80 } // namespace |
| 81 |
| 82 class PowerProfilerServiceTest : public testing::Test { |
| 83 public: |
| 84 void ServiceStartTest() { |
| 85 service_ = new PowerProfilerService( |
| 86 make_scoped_ptr<PowerDataProvider>( |
| 87 new TestPowerDataProvider(kNumEvents)), |
| 88 message_loop_.message_loop_proxy(), |
| 89 base::TimeDelta::FromMilliseconds(1)); |
| 90 EXPECT_TRUE(service_->IsAvailable()); |
| 91 } |
| 92 |
| 93 void AddObserverTest() { |
| 94 for (int index = 0; index < kNumObservers; ++index) |
| 95 service_->AddObserver(&observers_[index]); |
| 96 |
| 97 // No PowerEvents received. |
| 98 for (int index = 0; index < kNumObservers; ++index) |
| 99 EXPECT_EQ(observers_[0].valid_event_count(), 0); |
| 100 } |
| 101 |
| 102 void RemoveObserverTest() { |
| 103 for (int index = 0; index < kNumObservers; ++index) |
| 104 service_->RemoveObserver(&observers_[index]); |
| 105 |
| 106 // Everyone received |kNumEvents| events. |
| 107 for (int index = 0; index < kNumObservers; ++index) |
| 108 EXPECT_EQ(observers_[index].valid_event_count(), kNumEvents); |
| 109 } |
| 110 |
| 111 protected: |
| 112 PowerProfilerServiceTest() |
| 113 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 114 total_num_events_received(0) { |
| 115 for (int index = 0; index < kNumObservers; ++index) { |
| 116 observers_[index].set_total_num_events_received( |
| 117 &total_num_events_received); |
| 118 } |
| 119 } |
| 120 virtual ~PowerProfilerServiceTest() {} |
| 121 |
| 122 private: |
| 123 PowerProfilerService* service_; |
| 124 TestPowerProfilerObserver observers_[kNumObservers]; |
| 125 |
| 126 // UI thread. |
| 127 base::MessageLoopForUI message_loop_; |
| 128 BrowserThreadImpl ui_thread_; |
| 129 |
| 130 int total_num_events_received; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest); |
| 133 }; |
| 134 |
| 135 // Test whether PowerProfilerService dispatches power events to observers |
| 136 // properly. |
| 137 TEST_F(PowerProfilerServiceTest, AvailableService) { |
| 138 ServiceStartTest(); |
| 139 AddObserverTest(); |
| 140 base::MessageLoop::current()->Run(); |
| 141 RemoveObserverTest(); |
| 142 } |
| 143 |
| 144 } // namespace content |
OLD | NEW |