0

Bring up idletimer unittest on Mac port.

Review URL: http://codereview.chromium.org/7243

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3328 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
jeremy@chromium.org
2008-10-13 23:03:36 +00:00
parent 75b537e299
commit 806ca33bec
4 changed files with 71 additions and 38 deletions

@ -165,6 +165,8 @@
ABF4B9C30DC2BD6C00A6E319 /* values.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403880D92D2CF0006B936 /* values.cc */; };
ABFBD3E60DC793C600E164CB /* md5.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403290D92D2090006B936 /* md5.cc */; };
B52C916C0E9428F500208D01 /* clipboard_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = B52C916B0E9428F500208D01 /* clipboard_unittest.cc */; };
B53C85280E9C298C000F70AB /* idle_timer.cc in Sources */ = {isa = PBXBuildFile; fileRef = 820EB4EB0E3A60FE009668FC /* idle_timer.cc */; };
B57E4D780E9C26340090055D /* idletimer_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = B57E4D770E9C26340090055D /* idletimer_unittest.cc */; };
B5EF235C0E89ABF500E1E114 /* platform_canvas_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABE1BA0C0E756EC4009041DA /* platform_canvas_unittest.cc */; };
BA0F69870E79D7980079A8A1 /* thread_local_storage_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */; };
BA5CC5840E788093004EDD45 /* shared_memory_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */; };
@ -617,6 +619,7 @@
ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard_mac.mm; sourceTree = "<group>"; };
ABF4B9B40DC2BC9F00A6E319 /* path_service.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = path_service.cc; sourceTree = "<group>"; };
B52C916B0E9428F500208D01 /* clipboard_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipboard_unittest.cc; sourceTree = "<group>"; };
B57E4D770E9C26340090055D /* idletimer_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = idletimer_unittest.cc; sourceTree = "<group>"; };
BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_local_storage_unittest.cc; sourceTree = "<group>"; };
BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shared_memory_unittest.cc; sourceTree = "<group>"; };
BA739A000E5E3242009842A7 /* tracked_objects_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tracked_objects_unittest.cc; sourceTree = "<group>"; };
@ -871,6 +874,7 @@
825403130D92D1E80006B936 /* id_map.h */,
820EB4EB0E3A60FE009668FC /* idle_timer.cc */,
820EB4EC0E3A60FE009668FC /* idle_timer.h */,
B57E4D770E9C26340090055D /* idletimer_unittest.cc */,
825403190D92D1F40006B936 /* image_util.cc */,
8254031A0D92D1F40006B936 /* image_util.h */,
8254031B0D92D1F40006B936 /* json_reader.cc */,
@ -1349,6 +1353,7 @@
93611B180E5A875D00F9405D /* histogram.cc in Sources */,
7BAE392A0E6F4EEF00C3F750 /* hmac_mac.cc in Sources */,
8216A5060E34DBDD00EE374C /* icu_util.cc in Sources */,
B53C85280E9C298C000F70AB /* idle_timer.cc in Sources */,
ABF4B9AF0DC2BC6200A6E319 /* json_reader.cc in Sources */,
ABF4B9B00DC2BC6500A6E319 /* json_writer.cc in Sources */,
7BF892DF0E75887C000BAF8A /* lazy_instance.cc in Sources */,
@ -1436,6 +1441,7 @@
7B78D3910E54FE0100609465 /* file_version_info_unittest.cc in Sources */,
93611B1A0E5A878400F9405D /* histogram_unittest.cc in Sources */,
7BAE392B0E6F4EF200C3F750 /* hmac_unittest.cc in Sources */,
B57E4D780E9C26340090055D /* idletimer_unittest.cc in Sources */,
AB956E0A0E5DDC0900BBE9D8 /* image_operations_unittest.cc in Sources */,
7B78D3920E54FE0100609465 /* json_reader_unittest.cc in Sources */,
7B78D3930E54FE0100609465 /* json_writer_unittest.cc in Sources */,

@ -4,15 +4,48 @@
#include "base/idle_timer.h"
#if defined(OS_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#endif
#include "base/message_loop.h"
#include "base/time.h"
namespace base {
#if defined(OS_WIN)
bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) {
LASTINPUTINFO lastInputInfo;
lastInputInfo.cbSize = sizeof(lastInputInfo);
if (GetLastInputInfo(&lastInputInfo) == 0) {
return false;
}
int32 last_input_time = lastInputInfo.dwTime;
// Note: On Windows GetLastInputInfo returns a 32bit value which rolls over
// ~49days.
int32 current_time = GetTickCount();
int32 delta = current_time - last_input_time;
// delta will go negative if we've been idle for 2GB of ticks.
if (delta < 0)
delta = -delta;
*milliseconds_interval_since_last_event = delta;
return true;
}
#elif defined(OS_MACOSX)
bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) {
*milliseconds_interval_since_last_event =
CGEventSourceSecondsSinceLastEventType(
kCGEventSourceStateCombinedSessionState,
kCGAnyInputEventType) * 1000.0;
return true;
}
#endif
IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat)
: idle_interval_(idle_time),
repeat_(repeat),
get_last_input_info_fn_(GetLastInputInfo) {
idle_time_source_(OSIdleTimeSource) {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()) <<
"Requires a thread that processes Windows UI events";
}
@ -48,17 +81,8 @@ void IdleTimer::StartTimer() {
}
TimeDelta IdleTimer::CurrentIdleTime() {
// TODO(mbelshe): This is windows-specific code.
LASTINPUTINFO info;
info.cbSize = sizeof(info);
if (get_last_input_info_fn_(&info)) {
// Note: GetLastInputInfo returns a 32bit value which rolls over ~49days.
int32 last_input_time = info.dwTime;
int32 current_time = GetTickCount();
int32 interval = current_time - last_input_time;
// Interval will go negative if we've been idle for 2GB of ticks.
if (interval < 0)
interval = -interval;
int32 interval = 0;
if (idle_time_source_(&interval)) {
return TimeDelta::FromMilliseconds(interval);
}
NOTREACHED();

@ -28,7 +28,9 @@
#ifndef BASE_IDLE_TIMER_H_
#define BASE_IDLE_TIMER_H_
#if defined(OS_WIN)
#include <windows.h>
#endif
#include "base/basictypes.h"
#include "base/task.h"
@ -36,8 +38,9 @@
namespace base {
// Function prototype for GetLastInputInfo.
typedef BOOL (__stdcall *GetLastInputInfoFunction)(PLASTINPUTINFO plii);
// Function prototype - Get the number of milliseconds that the user has been
// idle.
typedef bool (*IdleTimeSource)(int32 *milliseconds_interval_since_last_event);
class IdleTimer {
public:
@ -60,9 +63,9 @@ class IdleTimer {
virtual void OnIdle() = 0;
protected:
// Override the GetLastInputInfo function.
void set_last_input_info_fn(GetLastInputInfoFunction function) {
get_last_input_info_fn_ = function;
// Override the IdleTimeSource.
void set_idle_time_source(IdleTimeSource idle_time_source) {
idle_time_source_ = idle_time_source;
}
private:
@ -84,7 +87,7 @@ class IdleTimer {
// will be 0 until the timer fires the first time.
OneShotTimer<IdleTimer> timer_;
GetLastInputInfoFunction get_last_input_info_fn_;
IdleTimeSource idle_time_source_;
DISALLOW_COPY_AND_ASSIGN(IdleTimer);
};

@ -10,20 +10,15 @@ using base::IdleTimer;
namespace {
class IdleTimerTest : public testing::Test {
private:
// IdleTimer requires a UI message loop on the current thread.
MessageLoopForUI message_loop_;
};
// We Mock the GetLastInputInfo function to return
// the time stored here.
static DWORD mock_idle_time = GetTickCount();
static Time mock_timer_started;
BOOL __stdcall MockGetLastInputInfoFunction(PLASTINPUTINFO plii) {
DCHECK(plii->cbSize == sizeof(LASTINPUTINFO));
plii->dwTime = mock_idle_time;
return TRUE;
bool MockIdleTimeSource(int32 *milliseconds_interval_since_last_event) {
TimeDelta delta = Time::Now() - mock_timer_started;
*milliseconds_interval_since_last_event =
static_cast<int32>(delta.InMilliseconds());
return true;
}
// TestIdle task fires after 100ms of idle time.
@ -32,7 +27,7 @@ class TestIdleTask : public IdleTimer {
TestIdleTask(bool repeat)
: IdleTimer(TimeDelta::FromMilliseconds(100), repeat),
idle_counter_(0) {
set_last_input_info_fn(MockGetLastInputInfoFunction);
set_idle_time_source(MockIdleTimeSource);
}
int get_idle_counter() { return idle_counter_; }
@ -59,11 +54,15 @@ class ResetIdleTask {
public:
ResetIdleTask() {}
void Run() {
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
}
};
} // namespace
class IdleTimerTest : public testing::Test {
private:
// IdleTimer requires a UI message loop on the current thread.
MessageLoopForUI message_loop_;
};
///////////////////////////////////////////////////////////////////////////////
// NoRepeat tests:
@ -75,7 +74,7 @@ TEST_F(IdleTimerTest, NoRepeatIdle) {
// Create a Quit timer which will fire after 1s.
// Verify that we fired exactly once.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(false);
TestFinishedTask finish_task;
@ -94,7 +93,7 @@ TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) {
// Create a timer to reset once, idle after 500ms.
// Verify that we fired exactly twice.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(false);
TestFinishedTask finish_task;
@ -120,7 +119,7 @@ TEST_F(IdleTimerTest, NoRepeatNotIdle) {
// Create a timer to reset idle every 50ms.
// Verify that we never fired.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(false);
TestFinishedTask finish_task;
@ -153,7 +152,7 @@ TEST_F(IdleTimerTest, Repeat) {
// Create an IdleTimer, which should fire repeatedly after 100ms.
// Create a Quit timer which will fire after 1.05s.
// Verify that we fired 10 times.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(true);
TestFinishedTask finish_task;
@ -177,7 +176,7 @@ TEST_F(IdleTimerTest, RepeatIdleReset) {
// Create a Quit timer which will fire after 1s.
// Create a reset timer, which fires after 550ms
// Verify that we fired 9 times.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(true);
ResetIdleTask reset_task;
@ -207,7 +206,7 @@ TEST_F(IdleTimerTest, RepeatNotIdle) {
// Create a timer to reset idle every 50ms.
// Verify that we never fired.
mock_idle_time = GetTickCount();
mock_timer_started = Time::Now();
TestIdleTask test_task(true);
TestFinishedTask finish_task;
@ -229,3 +228,4 @@ TEST_F(IdleTimerTest, RepeatNotIdle) {
EXPECT_EQ(test_task.get_idle_counter(), 0);
}
} // namespace