Chromium Code Reviews| 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() : valid_event_count_(0) {} | |
| 47 virtual ~TestPowerProfilerObserver() {} | |
| 48 | |
| 49 virtual void OnPowerEvent(const PowerEventVector& events) OVERRIDE { | |
| 50 if (IsValidEvent(events[0])) { | |
| 51 ++valid_event_count_; | |
| 52 if (valid_event_count_ == kNumEvents) | |
| 53 ++all_receive_counter; | |
| 54 if (all_receive_counter == kNumObservers) | |
| 55 base::MessageLoop::current()->Quit(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 int valid_event_count() const { return valid_event_count_; } | |
| 60 | |
| 61 static int all_receive_counter; | |
|
jeremy
2014/02/18 10:28:57
Much better!
The only issue is the use of a class
Pan
2014/02/18 12:58:39
thanks, done
| |
| 62 | |
| 63 private: | |
| 64 bool IsValidEvent(const PowerEvent& event) { | |
| 65 return event.type == PowerEvent::SOC_PACKAGE && | |
| 66 !event.time.is_null() && | |
| 67 event.value > 0; | |
| 68 } | |
| 69 | |
| 70 int valid_event_count_; | |
| 71 DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver); | |
| 72 }; | |
| 73 | |
| 74 int TestPowerProfilerObserver::all_receive_counter = 0; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 class PowerProfilerServiceTest : public testing::Test { | |
| 79 public: | |
| 80 void ServiceStartTest() { | |
| 81 service_ = new PowerProfilerService( | |
| 82 make_scoped_ptr<PowerDataProvider>( | |
| 83 new TestPowerDataProvider(kNumEvents)), | |
| 84 message_loop_.message_loop_proxy(), | |
| 85 base::TimeDelta::FromMilliseconds(5)); | |
|
qsr
2014/02/18 10:01:22
If you do not check anything related to time, make
Pan
2014/02/18 12:58:39
thanks, done.
| |
| 86 EXPECT_TRUE(service_->IsAvailable()); | |
| 87 } | |
| 88 | |
| 89 void AddObserverTest() { | |
| 90 for (int index = 0; index < kNumObservers; ++index) | |
| 91 service_->AddObserver(&observers_[index]); | |
| 92 | |
| 93 // No PowerEvents received. | |
| 94 for (int index = 0; index < kNumObservers; ++index) | |
| 95 EXPECT_EQ(observers_[0].valid_event_count(), 0); | |
| 96 } | |
| 97 | |
| 98 void RemoveObserverTest() { | |
| 99 for (int index = 0; index < kNumObservers; ++index) | |
| 100 service_->RemoveObserver(&observers_[index]); | |
| 101 | |
| 102 // Everyone received |kNumEvents| events. | |
| 103 for (int index = 0; index < kNumObservers; ++index) | |
| 104 EXPECT_EQ(observers_[index].valid_event_count(), kNumEvents); | |
| 105 } | |
| 106 | |
| 107 protected: | |
| 108 PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | |
| 109 virtual ~PowerProfilerServiceTest() {} | |
| 110 | |
| 111 private: | |
| 112 PowerProfilerService* service_; | |
| 113 TestPowerProfilerObserver observers_[kNumObservers]; | |
| 114 | |
| 115 // UI thread. | |
| 116 base::MessageLoopForUI message_loop_; | |
| 117 BrowserThreadImpl ui_thread_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest); | |
| 120 }; | |
| 121 | |
| 122 // Test whether PowerProfilerService dispatches power events to observers | |
| 123 // properly. | |
| 124 TEST_F(PowerProfilerServiceTest, AvailableService) { | |
| 125 ServiceStartTest(); | |
| 126 AddObserverTest(); | |
| 127 base::MessageLoop::current()->Run(); | |
| 128 RemoveObserverTest(); | |
| 129 } | |
| 130 | |
| 131 } // namespace content | |
| OLD | NEW |