Chromium Code Reviews| 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..92f64e81ec0ab7e56fb66b492b58a42afaf21c5e |
| --- /dev/null |
| +++ b/content/browser/power_profiler/power_profiler_service_unittest.cc |
| @@ -0,0 +1,82 @@ |
| +// 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 "content/browser/browser_thread_impl.h" |
| +#include "content/public/browser/power_profiler_service.h" |
| +#include "content/test/test_power_data_provider.h" |
| +#include "content/test/test_power_profiler_observer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +class PowerProfilerServiceTest : public testing::Test { |
|
qsr
2014/01/27 10:01:47
See off review patch for suggestion on this: mainl
Pan
2014/01/28 13:32:58
Done.
|
| + public: |
| + void ServiceStartTest() { |
| + service_ = new PowerProfilerService(make_scoped_ptr<PowerDataProvider>( |
| + new TestPowerDataProvider(kEvents))); |
| + 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); |
| + } |
| + |
| + protected: |
| + PowerProfilerServiceTest() {} |
| + virtual ~PowerProfilerServiceTest() {} |
| + |
| + virtual void SetUp() { |
| + ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
| + ui_thread_->Start(); |
| + } |
| + |
| + virtual void TearDown() { |
| + ui_thread_->Stop(); |
| + } |
| + |
| + private: |
| + const static int kEvents = 5; |
| + const static int kObservers = 3; |
| + |
| + PowerProfilerService* service_; |
| + TestPowerProfilerObserver observers_[kObservers]; |
| + |
| + scoped_ptr<BrowserThreadImpl> ui_thread_; |
| + base::MessageLoop message_loop_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PowerProfilerServiceTest); |
| +}; |
| + |
| +TEST_F(PowerProfilerServiceTest, AvailableService) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&PowerProfilerServiceTest::ServiceStartTest, |
| + base::Unretained(this))); |
| + |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&PowerProfilerServiceTest::AddObserverTest, |
| + base::Unretained(this))); |
| + |
| + base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(300)); |
| + |
| + BrowserThread::PostTaskAndReply(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&PowerProfilerServiceTest::RemoveObserverTest, |
| + base::Unretained(this)), base::MessageLoop::QuitClosure()); |
| +} |
| + |
| +} // namespace content |