From 8de90c26475394b3fa8bfca08aafcc90cb3cdd3a Mon Sep 17 00:00:00 2001 From: "thestig@chromium.org" Date: Tue, 22 May 2012 20:02:28 +0000 Subject: Cleanup: Add test fixture for SystemMonitorTest to remove some redundant code. BUG=none TEST=none Review URL: https://chromiumcodereview.appspot.com/10414032 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138344 0039d316-1c4b-4281-b951-d872f2087c98 --- base/system_monitor/system_monitor_unittest.cc | 171 +++++++++++-------------- 1 file changed, 78 insertions(+), 93 deletions(-) (limited to 'base/system_monitor') diff --git a/base/system_monitor/system_monitor_unittest.cc b/base/system_monitor/system_monitor_unittest.cc index d568799..7e7810a 100644 --- a/base/system_monitor/system_monitor_unittest.cc +++ b/base/system_monitor/system_monitor_unittest.cc @@ -12,6 +12,8 @@ namespace base { +namespace { + class PowerTest : public SystemMonitor::PowerObserver { public: PowerTest() @@ -47,69 +49,70 @@ class PowerTest : public SystemMonitor::PowerObserver { int resumes_; // Count of OnResume notifications. }; -TEST(SystemMonitor, PowerNotifications) { - const int kObservers = 5; - - // Initialize a message loop for this to run on. - MessageLoop loop; - +class SystemMonitorTest : public testing::Test { + protected: + SystemMonitorTest() { #if defined(OS_MACOSX) - SystemMonitor::AllocateSystemIOPorts(); + // This needs to happen before SystemMonitor's ctor. + SystemMonitor::AllocateSystemIOPorts(); #endif + system_monitor_.reset(new SystemMonitor); + } + virtual ~SystemMonitorTest() {} + + MessageLoop message_loop_; + scoped_ptr system_monitor_; + + DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); +}; + +TEST_F(SystemMonitorTest, PowerNotifications) { + const int kObservers = 5; - SystemMonitor system_monitor; PowerTest test[kObservers]; for (int index = 0; index < kObservers; ++index) - system_monitor.AddPowerObserver(&test[index]); + system_monitor_->AddPowerObserver(&test[index]); // Send a bunch of power changes. Since the battery power hasn't // actually changed, we shouldn't get notifications. for (int index = 0; index < 5; index++) { - system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); + system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); EXPECT_EQ(test[0].power_state_changes(), 0); } // Sending resume when not suspended should have no effect. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); + system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); + message_loop_.RunAllPending(); EXPECT_EQ(test[0].resumes(), 0); // Pretend we suspended. - system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - loop.RunAllPending(); + system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); + message_loop_.RunAllPending(); EXPECT_EQ(test[0].suspends(), 1); // Send a second suspend notification. This should be suppressed. - system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - loop.RunAllPending(); + system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); + message_loop_.RunAllPending(); EXPECT_EQ(test[0].suspends(), 1); // Pretend we were awakened. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); + system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); + message_loop_.RunAllPending(); EXPECT_EQ(test[0].resumes(), 1); // Send a duplicate resume notification. This should be suppressed. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); + system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); + message_loop_.RunAllPending(); EXPECT_EQ(test[0].resumes(), 1); } -TEST(SystemMonitor, DeviceChangeNotifications) { +TEST_F(SystemMonitorTest, DeviceChangeNotifications) { const int kObservers = 5; - // Initialize a message loop for this to run on. - MessageLoop loop; - -#if defined(OS_MACOSX) - SystemMonitor::AllocateSystemIOPorts(); -#endif - testing::Sequence mock_sequencer[kObservers]; - SystemMonitor system_monitor; MockDevicesChangedObserver observers[kObservers]; for (int index = 0; index < kObservers; ++index) { - system_monitor.AddDevicesChangedObserver(&observers[index]); + system_monitor_->AddDevicesChangedObserver(&observers[index]); EXPECT_CALL(observers[index], OnDevicesChanged()) .Times(3) @@ -123,56 +126,38 @@ TEST(SystemMonitor, DeviceChangeNotifications) { .InSequence(mock_sequencer[index]); } - system_monitor.ProcessDevicesChanged(); - loop.RunAllPending(); + system_monitor_->ProcessDevicesChanged(); + message_loop_.RunAllPending(); - system_monitor.ProcessDevicesChanged(); - system_monitor.ProcessDevicesChanged(); - loop.RunAllPending(); + system_monitor_->ProcessDevicesChanged(); + system_monitor_->ProcessDevicesChanged(); + message_loop_.RunAllPending(); - system_monitor.ProcessMediaDeviceAttached( + system_monitor_->ProcessMediaDeviceAttached( 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); - loop.RunAllPending(); + message_loop_.RunAllPending(); - system_monitor.ProcessMediaDeviceDetached(1); - system_monitor.ProcessMediaDeviceDetached(2); - loop.RunAllPending(); + system_monitor_->ProcessMediaDeviceDetached(1); + system_monitor_->ProcessMediaDeviceDetached(2); + message_loop_.RunAllPending(); } -TEST(SystemMonitor, GetAttachedMediaDevicesEmpty) { - // Initialize a message loop for this to run on. - MessageLoop loop; - -#if defined(OS_MACOSX) - SystemMonitor::AllocateSystemIOPorts(); -#endif - - SystemMonitor system_monitor; - +TEST_F(SystemMonitorTest, GetAttachedMediaDevicesEmpty) { scoped_ptr > devices; - devices.reset(system_monitor.GetAttachedMediaDevices()); + devices.reset(system_monitor_->GetAttachedMediaDevices()); EXPECT_EQ(0U, devices->size()); } -TEST(SystemMonitor, GetAttachedMediaDevicesAttachDetach) { - // Initialize a message loop for this to run on. - MessageLoop loop; - -#if defined(OS_MACOSX) - SystemMonitor::AllocateSystemIOPorts(); -#endif - - SystemMonitor system_monitor; - +TEST_F(SystemMonitorTest, GetAttachedMediaDevicesAttachDetach) { const SystemMonitor::DeviceIdType kDeviceId1 = 42; const char kDeviceName1[] = "test"; const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); - system_monitor.ProcessMediaDeviceAttached(kDeviceId1, - kDeviceName1, - kDevicePath1); - loop.RunAllPending(); + system_monitor_->ProcessMediaDeviceAttached(kDeviceId1, + kDeviceName1, + kDevicePath1); + message_loop_.RunAllPending(); scoped_ptr > devices; - devices.reset(system_monitor.GetAttachedMediaDevices()); + devices.reset(system_monitor_->GetAttachedMediaDevices()); ASSERT_EQ(1U, devices->size()); EXPECT_EQ(kDeviceId1, (*devices)[0].a); EXPECT_EQ(kDeviceName1, (*devices)[0].b); @@ -181,11 +166,11 @@ TEST(SystemMonitor, GetAttachedMediaDevicesAttachDetach) { const SystemMonitor::DeviceIdType kDeviceId2 = 44; const char kDeviceName2[] = "test2"; const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); - system_monitor.ProcessMediaDeviceAttached(kDeviceId2, - kDeviceName2, - kDevicePath2); - loop.RunAllPending(); - devices.reset(system_monitor.GetAttachedMediaDevices()); + system_monitor_->ProcessMediaDeviceAttached(kDeviceId2, + kDeviceName2, + kDevicePath2); + message_loop_.RunAllPending(); + devices.reset(system_monitor_->GetAttachedMediaDevices()); ASSERT_EQ(2U, devices->size()); EXPECT_EQ(kDeviceId1, (*devices)[0].a); EXPECT_EQ(kDeviceName1, (*devices)[0].b); @@ -194,46 +179,46 @@ TEST(SystemMonitor, GetAttachedMediaDevicesAttachDetach) { EXPECT_EQ(kDeviceName2, (*devices)[1].b); EXPECT_EQ(kDevicePath2, (*devices)[1].c); - system_monitor.ProcessMediaDeviceDetached(kDeviceId1); - loop.RunAllPending(); - devices.reset(system_monitor.GetAttachedMediaDevices()); + system_monitor_->ProcessMediaDeviceDetached(kDeviceId1); + message_loop_.RunAllPending(); + devices.reset(system_monitor_->GetAttachedMediaDevices()); ASSERT_EQ(1U, devices->size()); EXPECT_EQ(kDeviceId2, (*devices)[0].a); EXPECT_EQ(kDeviceName2, (*devices)[0].b); EXPECT_EQ(kDevicePath2, (*devices)[0].c); - system_monitor.ProcessMediaDeviceDetached(kDeviceId2); - loop.RunAllPending(); - devices.reset(system_monitor.GetAttachedMediaDevices()); + system_monitor_->ProcessMediaDeviceDetached(kDeviceId2); + message_loop_.RunAllPending(); + devices.reset(system_monitor_->GetAttachedMediaDevices()); EXPECT_EQ(0U, devices->size()); } -TEST(SystemMonitor, PowerRequirements) { +TEST_F(SystemMonitorTest, PowerRequirements) { #if defined(OS_WIN) - MessageLoop loop; - SystemMonitor system_monitor; - ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); + ASSERT_EQ(0, system_monitor_->GetPowerRequirementsCountForTest()); - system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); - ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); + ASSERT_EQ(1, system_monitor_->GetPowerRequirementsCountForTest()); - system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); - ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); + ASSERT_EQ(2, system_monitor_->GetPowerRequirementsCountForTest()); // A second identical request should not increase the request count. - system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); - ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); + ASSERT_EQ(2, system_monitor_->GetPowerRequirementsCountForTest()); - system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); - ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); + ASSERT_EQ(1, system_monitor_->GetPowerRequirementsCountForTest()); // The request count should not decrease until all identical requests end. - system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); - ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); + ASSERT_EQ(1, system_monitor_->GetPowerRequirementsCountForTest()); - system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); - ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); + system_monitor_->EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); + ASSERT_EQ(0, system_monitor_->GetPowerRequirementsCountForTest()); #endif // defined(OS_WIN) } +} // namespace + } // namespace base -- cgit v1.1