Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Unified Diff: content/browser/power_profiler/power_profiler_service_unittest.cc

Issue 140583003: Chrome power profiler service (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/power_profiler/power_profiler_service.cc ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « content/browser/power_profiler/power_profiler_service.cc ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698