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

Unified Diff: chromeos/dbus/fake_power_manager_client_unittest.cc

Issue 1206733002: ChromeOs Power Emulation Impl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Making a better battery emu! Created 5 years, 5 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
Index: chromeos/dbus/fake_power_manager_client_unittest.cc
diff --git a/chromeos/dbus/fake_power_manager_client_unittest.cc b/chromeos/dbus/fake_power_manager_client_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fd6b9f276d93fad340093891514fcab026fe06db
--- /dev/null
+++ b/chromeos/dbus/fake_power_manager_client_unittest.cc
@@ -0,0 +1,138 @@
+// Copyright 2015 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 "chromeos/dbus/fake_power_manager_client.h"
+
+#include "base/basictypes.h"
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chromeos {
+namespace {
+
+const double kInitialBatteryPercent = 85;
+const double kUpdatedBatteryPercent = 70;
+const power_manager::PowerSupplyProperties_BatteryState
+ kBatteryStateDischarging =
Daniel Erat 2015/07/15 22:39:06 nit: kInitialBatteryState
mozartalouis 2015/07/15 22:55:43 Done.
+ power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
+const power_manager::PowerSupplyProperties_ExternalPower kExternalPowerUsb =
Daniel Erat 2015/07/15 22:39:06 nit: kInitialExternalPower
mozartalouis 2015/07/15 22:55:43 Done.
+ power_manager::PowerSupplyProperties_ExternalPower_USB;
+const power_manager::PowerSupplyProperties_ExternalPower kExternalPowerAc =
Daniel Erat 2015/07/15 22:39:06 you can delete this; i meant that you you should u
mozartalouis 2015/07/15 22:55:43 Done.
+ power_manager::PowerSupplyProperties_ExternalPower_AC;
+
+class TestObserver : public PowerManagerClient::Observer {
+ public:
+ TestObserver() : num_power_changed_(0) {}
+ ~TestObserver() override {}
+
+ const power_manager::PowerSupplyProperties& props() const { return props_; }
+ int num_power_changed() const { return num_power_changed_; }
+
+ void ClearProps() { props_.Clear(); }
+
+ void PowerChanged(
+ const power_manager::PowerSupplyProperties& proto) override {
+ props_ = proto;
+ ++num_power_changed_;
+ }
+
+ private:
+ int num_power_changed_;
+ power_manager::PowerSupplyProperties props_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestObserver);
+};
+
+void SetTestProperties(power_manager::PowerSupplyProperties* props) {
+ props->set_battery_percent(kInitialBatteryPercent);
+ props->set_is_calculating_battery_time(true);
+ props->set_battery_state(kBatteryStateDischarging);
+ props->set_external_power(kExternalPowerUsb);
+}
+
+} // namespace
+
+TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) {
+ // Checking to verify when UpdatePowerProperties is called,
+ // |props_| values are updated.
+ FakePowerManagerClient client;
+ power_manager::PowerSupplyProperties props;
+
+ SetTestProperties(&props);
+ client.UpdatePowerProperties(props);
+
+ EXPECT_EQ(kInitialBatteryPercent, client.props().battery_percent());
+ EXPECT_TRUE(client.props().is_calculating_battery_time());
+ EXPECT_EQ(kBatteryStateDischarging, client.props().battery_state());
+ EXPECT_EQ(kExternalPowerUsb, client.props().external_power());
+
+ // Test if when the values are changed, the correct data is set in the
+ // FakePowerManagerClient.
+ props = client.props();
+ props.set_battery_percent(kUpdatedBatteryPercent);
+ client.UpdatePowerProperties(props);
+
+ EXPECT_EQ(kUpdatedBatteryPercent, client.props().battery_percent());
+ EXPECT_TRUE(client.props().is_calculating_battery_time());
+ EXPECT_EQ(kBatteryStateDischarging, client.props().battery_state());
+ EXPECT_EQ(kExternalPowerUsb, client.props().external_power());
+};
+
+TEST(FakePowerManagerClientTest, NotifyObserversTest) {
+ FakePowerManagerClient client;
+ TestObserver test_observer;
+
+ // Test adding observer.
+ client.AddObserver(&test_observer);
+ EXPECT_TRUE(client.HasObserver(&test_observer));
+
+ // Test if NotifyObservers() sends the correct values to |observer|.
+ // Check number of times NotifyObservers() is called.
+ power_manager::PowerSupplyProperties props;
+ SetTestProperties(&props);
+ client.UpdatePowerProperties(props);
+
+ EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
+ EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
+ EXPECT_EQ(kBatteryStateDischarging, test_observer.props().battery_state());
+ EXPECT_EQ(kExternalPowerUsb, test_observer.props().external_power());
+ EXPECT_EQ(1, test_observer.num_power_changed());
+
+ // Test if RequestStatusUpdate() will propagate the data to the observer.
+ // Check number of times NotifyObservers is called.
+ // RequestStatusUpdate posts to the current message loop. This is
+ // necessary because we want to make sure that NotifyObservers() is
+ // called as a result of RequestStatusUpdate().
+ base::MessageLoopForUI message_loop;
+ test_observer.ClearProps();
+ client.RequestStatusUpdate();
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
+ EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
+ EXPECT_EQ(kBatteryStateDischarging, test_observer.props().battery_state());
+ EXPECT_EQ(kExternalPowerUsb, test_observer.props().external_power());
+ EXPECT_EQ(2, test_observer.num_power_changed());
+
+ // Check when values are changed, the correct values are propagated to the
+ // observer
+ props = client.props();
+ props.set_battery_percent(kUpdatedBatteryPercent);
+ props.set_external_power(kExternalPowerAc);
+ client.UpdatePowerProperties(props);
+
+ EXPECT_EQ(kUpdatedBatteryPercent, test_observer.props().battery_percent());
+ EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
+ EXPECT_EQ(kBatteryStateDischarging, test_observer.props().battery_state());
+ EXPECT_EQ(kExternalPowerAc, test_observer.props().external_power());
+ EXPECT_EQ(3, test_observer.num_power_changed());
+
+ // Test removing observer.
+ client.RemoveObserver(&test_observer);
+ EXPECT_FALSE(client.HasObserver(&test_observer));
+};
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698