Commit 8de90c26 authored by thestig@chromium.org's avatar thestig@chromium.org

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
parent 0d92efc5
......@@ -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)
// This needs to happen before SystemMonitor's ctor.
SystemMonitor::AllocateSystemIOPorts();
#endif
system_monitor_.reset(new SystemMonitor);
}
virtual ~SystemMonitorTest() {}
MessageLoop message_loop_;
scoped_ptr<SystemMonitor> 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<std::vector<SystemMonitor::MediaDeviceInfo> > 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,
system_monitor_->ProcessMediaDeviceAttached(kDeviceId1,
kDeviceName1,
kDevicePath1);
loop.RunAllPending();
message_loop_.RunAllPending();
scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > 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,
system_monitor_->ProcessMediaDeviceAttached(kDeviceId2,
kDeviceName2,
kDevicePath2);
loop.RunAllPending();
devices.reset(system_monitor.GetAttachedMediaDevices());
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment