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