| Index: content/browser/power_profiler/power_profiler_service_unittest.cc
|
| diff --git a/content/browser/power_profiler/power_profiler_service_unittest.cc b/content/browser/power_profiler/power_profiler_service_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d317d43a6145148c64ae49ca9f91e57339a77921
|
| --- /dev/null
|
| +++ b/content/browser/power_profiler/power_profiler_service_unittest.cc
|
| @@ -0,0 +1,144 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "content/browser/browser_thread_impl.h"
|
| +#include "content/browser/power_profiler/power_profiler_service.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +const int kNumEvents = 3;
|
| +const int kNumObservers = 3;
|
| +
|
| +// Provide a set number of power events.
|
| +class TestPowerDataProvider : public PowerDataProvider {
|
| + public:
|
| + TestPowerDataProvider(int count) : num_events_to_send_(count) {}
|
| + virtual ~TestPowerDataProvider() {}
|
| +
|
| + virtual PowerEventVector GetData() OVERRIDE {
|
| + PowerEventVector events;
|
| + if (num_events_to_send_ == 0)
|
| + return events;
|
| +
|
| + PowerEvent event;
|
| + event.type = PowerEvent::SOC_PACKAGE;
|
| + event.time = base::TimeTicks::Now();
|
| + event.value = 1.0;
|
| + events.push_back(event);
|
| +
|
| + num_events_to_send_--;
|
| + return events;
|
| + }
|
| +
|
| + private:
|
| + int num_events_to_send_;
|
| + DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider);
|
| +};
|
| +
|
| +class TestPowerProfilerObserver : public PowerProfilerObserver {
|
| + public:
|
| + TestPowerProfilerObserver()
|
| + : valid_event_count_(0),
|
| + total_num_events_received_(NULL) {}
|
| + virtual ~TestPowerProfilerObserver() {}
|
| +
|
| + virtual void OnPowerEvent(const PowerEventVector& events) OVERRIDE {
|
| + if (IsValidEvent(events[0])) {
|
| + ++valid_event_count_;
|
| + (*total_num_events_received_)++;
|
| + if (*total_num_events_received_ >= kNumEvents * kNumObservers) {
|
| + // All expected events received, exiting.
|
| + base::MessageLoop::current()->Quit();
|
| + }
|
| + }
|
| + }
|
| +
|
| + int valid_event_count() const { return valid_event_count_; }
|
| +
|
| + void set_total_num_events_received(int* total_num_events_received) {
|
| + total_num_events_received_ = total_num_events_received;
|
| + }
|
| +
|
| + private:
|
| + bool IsValidEvent(const PowerEvent& event) {
|
| + return event.type == PowerEvent::SOC_PACKAGE &&
|
| + !event.time.is_null() &&
|
| + event.value > 0;
|
| + }
|
| +
|
| + int valid_event_count_;
|
| + int* total_num_events_received_;
|
| + DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class PowerProfilerServiceTest : public testing::Test {
|
| + public:
|
| + void ServiceStartTest() {
|
| + service_ = new PowerProfilerService(
|
| + make_scoped_ptr<PowerDataProvider>(
|
| + new TestPowerDataProvider(kNumEvents)),
|
| + message_loop_.message_loop_proxy(),
|
| + base::TimeDelta::FromMilliseconds(1));
|
| + EXPECT_TRUE(service_->IsAvailable());
|
| + }
|
| +
|
| + void AddObserverTest() {
|
| + for (int index = 0; index < kNumObservers; ++index)
|
| + service_->AddObserver(&observers_[index]);
|
| +
|
| + // No PowerEvents received.
|
| + for (int index = 0; index < kNumObservers; ++index)
|
| + EXPECT_EQ(observers_[0].valid_event_count(), 0);
|
| + }
|
| +
|
| + void RemoveObserverTest() {
|
| + for (int index = 0; index < kNumObservers; ++index)
|
| + service_->RemoveObserver(&observers_[index]);
|
| +
|
| + // Everyone received |kNumEvents| events.
|
| + for (int index = 0; index < kNumObservers; ++index)
|
| + EXPECT_EQ(observers_[index].valid_event_count(), kNumEvents);
|
| + }
|
| +
|
| + protected:
|
| + PowerProfilerServiceTest()
|
| + : ui_thread_(BrowserThread::UI, &message_loop_),
|
| + total_num_events_received(0) {
|
| + for (int index = 0; index < kNumObservers; ++index) {
|
| + observers_[index].set_total_num_events_received(
|
| + &total_num_events_received);
|
| + }
|
| + }
|
| + virtual ~PowerProfilerServiceTest() {}
|
| +
|
| + private:
|
| + PowerProfilerService* service_;
|
| + TestPowerProfilerObserver observers_[kNumObservers];
|
| +
|
| + // UI thread.
|
| + base::MessageLoopForUI message_loop_;
|
| + BrowserThreadImpl ui_thread_;
|
| +
|
| + int total_num_events_received;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest);
|
| +};
|
| +
|
| +// Test whether PowerProfilerService dispatches power events to observers
|
| +// properly.
|
| +TEST_F(PowerProfilerServiceTest, AvailableService) {
|
| + ServiceStartTest();
|
| + AddObserverTest();
|
| + base::MessageLoop::current()->Run();
|
| + RemoveObserverTest();
|
| +}
|
| +
|
| +} // namespace content
|
|
|