| 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..1a9859ce53bbcf0b84f4db39d6a0848781aa1835
|
| --- /dev/null
|
| +++ b/content/browser/power_profiler/power_profiler_service_unittest.cc
|
| @@ -0,0 +1,117 @@
|
| +// 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/public/browser/power_profiler_service.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +const int kEvents = 3;
|
| +const int kObservers = 3;
|
| +
|
| +class TestPowerDataProvider : public PowerDataProvider {
|
| + public:
|
| + TestPowerDataProvider(int count) : event_number_(count) {}
|
| + ~TestPowerDataProvider() {}
|
| +
|
| + virtual bool GetData(PowerEvent* event) OVERRIDE {
|
| + if (event_number_ == 0)
|
| + return false;
|
| +
|
| + event->time = base::TimeTicks::Now();
|
| + event->value = 1.0;
|
| + event_number_--;
|
| + return true;
|
| + }
|
| +
|
| + private:
|
| + int event_number_;
|
| + DISALLOW_COPY_AND_ASSIGN(TestPowerDataProvider);
|
| +};
|
| +
|
| +class TestPowerProfilerObserver : public PowerProfilerObserver {
|
| + public:
|
| + TestPowerProfilerObserver() : valid_event_count_(0) {}
|
| + ~TestPowerProfilerObserver() {}
|
| +
|
| + virtual void OnPowerEvent(const PowerEvent& event) OVERRIDE {
|
| + if (IsValidEvent(event))
|
| + ++valid_event_count_;
|
| + }
|
| +
|
| + int valid_event_count() const { return valid_event_count_; }
|
| +
|
| + private:
|
| + bool IsValidEvent(const PowerEvent& event) {
|
| + return !event.time.is_null() && event.value > 0;
|
| + }
|
| +
|
| + int valid_event_count_;
|
| + DISALLOW_COPY_AND_ASSIGN(TestPowerProfilerObserver);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class PowerProfilerServiceTest : public testing::Test {
|
| + public:
|
| + void ServiceStartTest() {
|
| + service_ = new PowerProfilerService(
|
| + make_scoped_ptr<PowerDataProvider>(new TestPowerDataProvider(kEvents)),
|
| + message_loop_.message_loop_proxy(),
|
| + base::TimeDelta::FromMilliseconds(5));
|
| + EXPECT_TRUE(service_->IsAvailable());
|
| + }
|
| +
|
| + void AddObserverTest() {
|
| + for (int index = 0; index < kObservers; ++index)
|
| + service_->AddObserver(&observers_[index]);
|
| +
|
| + // No one received PowerEvent now.
|
| + for (int index = 0; index < kObservers; ++index)
|
| + EXPECT_EQ(observers_[0].valid_event_count(), 0);
|
| + }
|
| +
|
| + void RemoveObserverTest() {
|
| + for (int index = 0; index < kObservers; ++index)
|
| + service_->RemoveObserver(&observers_[index]);
|
| +
|
| + // Everyone received |kEvents| events.
|
| + for (int index = 0; index < kObservers; ++index)
|
| + EXPECT_EQ(observers_[index].valid_event_count(), kEvents);
|
| + }
|
| +
|
| + void RunLoopForDelay(base::TimeDelta delay) {
|
| + message_loop_.PostDelayedTask(
|
| + FROM_HERE, base::MessageLoop::QuitClosure(), delay);
|
| + base::RunLoop().Run();
|
| + }
|
| +
|
| + protected:
|
| + PowerProfilerServiceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {}
|
| + virtual ~PowerProfilerServiceTest() {}
|
| +
|
| + private:
|
| + PowerProfilerService* service_;
|
| + TestPowerProfilerObserver observers_[kObservers];
|
| +
|
| + // UI thread
|
| + base::MessageLoopForUI message_loop_;
|
| + BrowserThreadImpl ui_thread_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest);
|
| +};
|
| +
|
| +TEST_F(PowerProfilerServiceTest, AvailableService) {
|
| + ServiceStartTest();
|
| + AddObserverTest();
|
| + RunLoopForDelay(base::TimeDelta::FromMilliseconds(20));
|
| + RemoveObserverTest();
|
| +}
|
| +
|
| +} // namespace content
|
|
|