added failing unit test and event queue DI to ms desks class.

This commit is contained in:
Nick Bolton 2012-07-20 12:24:55 +00:00
parent 34a87502f6
commit de30d04c72
4 changed files with 49 additions and 31 deletions

View File

@ -29,6 +29,7 @@
#include "TMethodJob.h" #include "TMethodJob.h"
#include "CArchMiscWindows.h" #include "CArchMiscWindows.h"
#include <malloc.h> #include <malloc.h>
#include "IEventQueue.h"
// these are only defined when WINVER >= 0x0500 // these are only defined when WINVER >= 0x0500
#if !defined(SPI_GETMOUSESPEED) #if !defined(SPI_GETMOUSESPEED)
@ -90,7 +91,8 @@
CMSWindowsDesks::CMSWindowsDesks( CMSWindowsDesks::CMSWindowsDesks(
bool isPrimary, bool noHooks, HINSTANCE hookLibrary, bool isPrimary, bool noHooks, HINSTANCE hookLibrary,
const IScreenSaver* screensaver, IJob* updateKeys) : const IScreenSaver* screensaver, IEventQueue& eventQueue,
IJob* updateKeys) :
m_isPrimary(isPrimary), m_isPrimary(isPrimary),
m_noHooks(noHooks), m_noHooks(noHooks),
m_is95Family(CArchMiscWindows::isWindows95Family()), m_is95Family(CArchMiscWindows::isWindows95Family()),
@ -107,7 +109,8 @@ CMSWindowsDesks::CMSWindowsDesks(
m_activeDeskName(), m_activeDeskName(),
m_mutex(), m_mutex(),
m_deskReady(&m_mutex, false), m_deskReady(&m_mutex, false),
m_updateKeys(updateKeys) m_updateKeys(updateKeys),
m_eventQueue(eventQueue)
{ {
if (hookLibrary != NULL) if (hookLibrary != NULL)
queryHookLibrary(hookLibrary); queryHookLibrary(hookLibrary);
@ -138,8 +141,8 @@ CMSWindowsDesks::enable()
// which desk is active and reinstalls the hooks as necessary. // which desk is active and reinstalls the hooks as necessary.
// we wouldn't need this if windows notified us of a desktop // we wouldn't need this if windows notified us of a desktop
// change but as far as i can tell it doesn't. // change but as far as i can tell it doesn't.
m_timer = EVENTQUEUE->newTimer(0.2, NULL); m_timer = m_eventQueue.newTimer(0.2, NULL);
EVENTQUEUE->adoptHandler(CEvent::kTimer, m_timer, m_eventQueue.adoptHandler(CEvent::kTimer, m_timer,
new TMethodEventJob<CMSWindowsDesks>( new TMethodEventJob<CMSWindowsDesks>(
this, &CMSWindowsDesks::handleCheckDesk)); this, &CMSWindowsDesks::handleCheckDesk));
@ -151,8 +154,8 @@ CMSWindowsDesks::disable()
{ {
// remove timer // remove timer
if (m_timer != NULL) { if (m_timer != NULL) {
EVENTQUEUE->removeHandler(CEvent::kTimer, m_timer); m_eventQueue.removeHandler(CEvent::kTimer, m_timer);
EVENTQUEUE->deleteTimer(m_timer); m_eventQueue.deleteTimer(m_timer);
m_timer = NULL; m_timer = NULL;
} }

View File

@ -34,6 +34,7 @@ class CEventQueueTimer;
class CThread; class CThread;
class IJob; class IJob;
class IScreenSaver; class IScreenSaver;
class IEventQueue;
//! Microsoft Windows desk handling //! Microsoft Windows desk handling
/*! /*!
@ -62,8 +63,10 @@ public:
updated in a thread attached to the current desk. updated in a thread attached to the current desk.
\p hookLibrary must be a handle to the hook library. \p hookLibrary must be a handle to the hook library.
*/ */
CMSWindowsDesks(bool isPrimary, bool noHooks, HINSTANCE hookLibrary, CMSWindowsDesks(
const IScreenSaver* screensaver, IJob* updateKeys); bool isPrimary, bool noHooks, HINSTANCE hookLibrary,
const IScreenSaver* screensaver, IEventQueue& eventQueue,
IJob* updateKeys);
~CMSWindowsDesks(); ~CMSWindowsDesks();
//! @name manipulators //! @name manipulators
@ -289,8 +292,10 @@ private:
// hook library stuff // hook library stuff
InstallFunc m_install; InstallFunc m_install;
UninstallFunc m_uninstall; UninstallFunc m_uninstall;
InstallScreenSaverFunc m_installScreensaver; InstallScreenSaverFunc
UninstallScreenSaverFunc m_uninstallScreensaver; m_installScreensaver;
UninstallScreenSaverFunc
m_uninstallScreensaver;
// keyboard stuff // keyboard stuff
IJob* m_updateKeys; IJob* m_updateKeys;
@ -298,6 +303,8 @@ private:
// options // options
bool m_leaveForegroundOption; bool m_leaveForegroundOption;
IEventQueue& m_eventQueue;
}; };
#endif #endif

View File

@ -122,6 +122,7 @@ CMSWindowsScreen::CMSWindowsScreen(bool isPrimary, bool noHooks, const CGameDevi
m_desks = new CMSWindowsDesks( m_desks = new CMSWindowsDesks(
m_isPrimary, m_noHooks, m_isPrimary, m_noHooks,
m_hookLibrary, m_screensaver, m_hookLibrary, m_screensaver,
*EVENTQUEUE,
new TMethodJob<CMSWindowsScreen>(this, new TMethodJob<CMSWindowsScreen>(this,
&CMSWindowsScreen::updateKeysCB)); &CMSWindowsScreen::updateKeysCB));
m_keyState = new CMSWindowsKeyState(m_desks, getEventTarget()); m_keyState = new CMSWindowsKeyState(m_desks, getEventTarget());

View File

@ -17,6 +17,10 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gmock/gmock.h> #include <gmock/gmock.h>
#define TEST_ENV
#include "Global.h"
#include "CMSWindowsKeyState.h" #include "CMSWindowsKeyState.h"
#include "CMSWindowsDesks.h" #include "CMSWindowsDesks.h"
#include "CMSWindowsScreen.h" #include "CMSWindowsScreen.h"
@ -39,21 +43,19 @@ protected:
// load synrgyhk.dll // load synrgyhk.dll
m_hookLibrary = m_hookLibraryLoader.openHookLibrary("synrgyhk"); m_hookLibrary = m_hookLibraryLoader.openHookLibrary("synrgyhk");
m_screensaver = new CMSWindowsScreenSaver(); m_screensaver = new CMSWindowsScreenSaver();
m_desks = new CMSWindowsDesks(
true, false, m_hookLibrary, m_screensaver,
new TMethodJob<CMSWindowsKeyStateTests>(
this, &CMSWindowsKeyStateTests::updateKeysCB));
} }
virtual void TearDown() virtual void TearDown()
{ {
delete m_screensaver; delete m_screensaver;
delete m_desks;
} }
CMSWindowsDesks* getDesks() const CMSWindowsDesks* newDesks(IEventQueue& eventQueue)
{ {
return m_desks; return new CMSWindowsDesks(
true, false, m_hookLibrary, m_screensaver, eventQueue,
new TMethodJob<CMSWindowsKeyStateTests>(
this, &CMSWindowsKeyStateTests::updateKeysCB));
} }
void* getEventTarget() const void* getEventTarget() const
@ -65,70 +67,75 @@ private:
void updateKeysCB(void*) { } void updateKeysCB(void*) { }
HINSTANCE m_hookLibrary; HINSTANCE m_hookLibrary;
IScreenSaver* m_screensaver; IScreenSaver* m_screensaver;
CMSWindowsDesks* m_desks;
CMSWindowsHookLibraryLoader m_hookLibraryLoader; CMSWindowsHookLibraryLoader m_hookLibraryLoader;
}; };
TEST_F(CMSWindowsKeyStateTests, disable_nonWin95OS_eventQueueNotUsed) TEST_F(CMSWindowsKeyStateTests, disable_nonWin95OS_eventQueueNotUsed)
{ {
NiceMock<CMockEventQueue> eventQueue; NiceMock<CMockEventQueue> eventQueue;
CMSWindowsDesks* desks = newDesks(eventQueue);
CMockKeyMap keyMap; CMockKeyMap keyMap;
CMSWindowsKeyState keyState(getDesks(), getEventTarget(), eventQueue, keyMap); CMSWindowsKeyState keyState(desks, getEventTarget(), eventQueue, keyMap);
// in anything above win95-family, event handler should not be called. // in anything above win95-family, event handler should not be called.
EXPECT_CALL(eventQueue, removeHandler(_, _)).Times(0); EXPECT_CALL(eventQueue, removeHandler(_, _)).Times(0);
keyState.disable(); keyState.disable();
delete desks;
} }
TEST_F(CMSWindowsKeyStateTests, testAutoRepeat_noRepeatAndButtonIsZero_resultIsTrue) TEST_F(CMSWindowsKeyStateTests, testAutoRepeat_noRepeatAndButtonIsZero_resultIsTrue)
{ {
NiceMock<CMockEventQueue> eventQueue; NiceMock<CMockEventQueue> eventQueue;
CMSWindowsDesks* desks = newDesks(eventQueue);
CMockKeyMap keyMap; CMockKeyMap keyMap;
CMSWindowsKeyState keyState(getDesks(), getEventTarget(), eventQueue, keyMap); CMSWindowsKeyState keyState(desks, getEventTarget(), eventQueue, keyMap);
keyState.setLastDown(1); keyState.setLastDown(1);
bool actual = keyState.testAutoRepeat(true, false, 1); bool actual = keyState.testAutoRepeat(true, false, 1);
ASSERT_TRUE(actual); ASSERT_TRUE(actual);
delete desks;
} }
TEST_F(CMSWindowsKeyStateTests, testAutoRepeat_pressFalse_lastDownIsZero) TEST_F(CMSWindowsKeyStateTests, testAutoRepeat_pressFalse_lastDownIsZero)
{ {
NiceMock<CMockEventQueue> eventQueue; NiceMock<CMockEventQueue> eventQueue;
CMSWindowsDesks* desks = newDesks(eventQueue);
CMockKeyMap keyMap; CMockKeyMap keyMap;
CMSWindowsKeyState keyState(getDesks(), getEventTarget(), eventQueue, keyMap); CMSWindowsKeyState keyState(desks, getEventTarget(), eventQueue, keyMap);
keyState.setLastDown(1); keyState.setLastDown(1);
keyState.testAutoRepeat(false, false, 1); keyState.testAutoRepeat(false, false, 1);
ASSERT_EQ(0, keyState.getLastDown()); ASSERT_EQ(0, keyState.getLastDown());
delete desks;
} }
TEST_F(CMSWindowsKeyStateTests, saveModifiers_noModifiers_savedModifiers0) TEST_F(CMSWindowsKeyStateTests, saveModifiers_noModifiers_savedModifiers0)
{ {
NiceMock<CMockEventQueue> eventQueue; NiceMock<CMockEventQueue> eventQueue;
CMSWindowsDesks* desks = newDesks(eventQueue);
CMockKeyMap keyMap; CMockKeyMap keyMap;
CMSWindowsKeyState keyState(getDesks(), getEventTarget(), eventQueue, keyMap); CMSWindowsKeyState keyState(desks, getEventTarget(), eventQueue, keyMap);
keyState.saveModifiers(); keyState.saveModifiers();
ASSERT_EQ(0, keyState.getSavedModifiers()); ASSERT_EQ(0, keyState.getSavedModifiers());
delete desks;
} }
/*
// TODO: fix Assertion failed: s_instance != NULL,
// file ..\..\..\..\src\lib\base\IEventQueue.cpp, line 37
TEST_F(CMSWindowsKeyStateTests, saveModifiers_shiftKeyDown_savedModifiers4) TEST_F(CMSWindowsKeyStateTests, saveModifiers_shiftKeyDown_savedModifiers4)
{ {
NiceMock<CMockEventQueue> eventQueue; NiceMock<CMockEventQueue> eventQueue;
CMSWindowsDesks* desks = newDesks(eventQueue);
CMockKeyMap keyMap; CMockKeyMap keyMap;
CMSWindowsKeyState keyState(getDesks(), getEventTarget(), eventQueue, keyMap); CMSWindowsKeyState keyState(desks, getEventTarget(), eventQueue, keyMap);
getDesks()->enable(); desks->enable();
getDesks()->fakeKeyEvent(1, 1, true, false); desks->fakeKeyEvent(1, 1, true, false);
keyState.saveModifiers(); keyState.saveModifiers();
ASSERT_EQ(1, keyState.getSavedModifiers()); ASSERT_EQ(1, keyState.getSavedModifiers());
delete desks;
} }
*/