From edeae477e1060f0ab1919550034eceb01b6a89de Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Sat, 7 May 2011 02:12:09 +0000 Subject: [PATCH] implemented google mock config and gave it a quick try (see: add_newValue_writeWasCalled) --- src/lib/platform/CMSWindowsClipboard.cpp | 18 ++++++---- src/lib/platform/CMSWindowsClipboard.h | 7 ++++ .../platform/CMSWindowsClipboardFacade.cpp | 29 ++++++++++++++++ src/lib/platform/CMSWindowsClipboardFacade.h | 30 ++++++++++++++++ src/lib/platform/CMakeLists.txt | 2 ++ src/lib/platform/IMSWindowsClipboardFacade.h | 34 +++++++++++++++++++ src/test/CMakeLists.txt | 11 +++--- src/test/integtests/CMakeLists.txt | 5 +-- .../platform/CMSWindowsClipboardTests.cpp | 21 +++++++++++- src/test/unittests/CMakeLists.txt | 7 ++-- 10 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 src/lib/platform/CMSWindowsClipboardFacade.cpp create mode 100644 src/lib/platform/CMSWindowsClipboardFacade.h create mode 100644 src/lib/platform/IMSWindowsClipboardFacade.h diff --git a/src/lib/platform/CMSWindowsClipboard.cpp b/src/lib/platform/CMSWindowsClipboard.cpp index f90a92bb..7ba83480 100644 --- a/src/lib/platform/CMSWindowsClipboard.cpp +++ b/src/lib/platform/CMSWindowsClipboard.cpp @@ -22,6 +22,7 @@ #include "CMSWindowsClipboardHTMLConverter.h" #include "CLog.h" #include "CArchMiscWindows.h" +#include "CMSWindowsClipboardFacade.h" // // CMSWindowsClipboard @@ -31,7 +32,9 @@ UINT CMSWindowsClipboard::s_ownershipFormat = 0; CMSWindowsClipboard::CMSWindowsClipboard(HWND window) : m_window(window), - m_time(0) + m_time(0), + m_facade(new CMSWindowsClipboardFacade()), + m_deleteFacade(true) { // add converters, most desired first m_converters.push_back(new CMSWindowsClipboardUTF16Converter); @@ -47,6 +50,12 @@ CMSWindowsClipboard::CMSWindowsClipboard(HWND window) : CMSWindowsClipboard::~CMSWindowsClipboard() { clearConverters(); + + // dependency injection causes confusion over ownership, so we need + // logic to decide whether or not we delete the facade. there must + // be a more elegant way of doing this. + if (m_deleteFacade) + delete m_facade; } bool @@ -94,12 +103,7 @@ CMSWindowsClipboard::add(EFormat format, const CString& data) HANDLE win32Data = converter->fromIClipboard(data); if (win32Data != NULL) { UINT win32Format = converter->getWin32Format(); - if (SetClipboardData(win32Format, win32Data) == NULL) { - // free converted data if we couldn't put it on - // the clipboard. - // nb: couldn't cause this in integ tests. - GlobalFree(win32Data); - } + m_facade->write(win32Data, win32Format); } } } diff --git a/src/lib/platform/CMSWindowsClipboard.h b/src/lib/platform/CMSWindowsClipboard.h index eae6b303..f00d7647 100644 --- a/src/lib/platform/CMSWindowsClipboard.h +++ b/src/lib/platform/CMSWindowsClipboard.h @@ -19,16 +19,19 @@ #define CMSWINDOWSCLIPBOARD_H #include "IClipboard.h" +#include "CMSWindowsClipboardFacade.h" #include "stdvector.h" #define WIN32_LEAN_AND_MEAN #include class IMSWindowsClipboardConverter; +class IMSWindowsClipboardFacade; //! Microsoft windows clipboard implementation class CMSWindowsClipboard : public IClipboard { public: CMSWindowsClipboard(HWND window); + CMSWindowsClipboard(HWND window, IMSWindowsClipboardFacade &facade); virtual ~CMSWindowsClipboard(); //! Empty clipboard without ownership @@ -58,6 +61,8 @@ public: virtual bool has(EFormat) const; virtual CString get(EFormat) const; + void setFacade(IMSWindowsClipboardFacade& facade) { m_facade = &facade; m_deleteFacade = false; } + private: void clearConverters(); @@ -74,6 +79,8 @@ private: mutable Time m_time; ConverterList m_converters; static UINT s_ownershipFormat; + IMSWindowsClipboardFacade* m_facade; + bool m_deleteFacade; }; //! Clipboard format converter interface diff --git a/src/lib/platform/CMSWindowsClipboardFacade.cpp b/src/lib/platform/CMSWindowsClipboardFacade.cpp new file mode 100644 index 00000000..0639b9bf --- /dev/null +++ b/src/lib/platform/CMSWindowsClipboardFacade.cpp @@ -0,0 +1,29 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "CMSWindowsClipboard.h" +#include "CMSWindowsClipboardFacade.h" + +void CMSWindowsClipboardFacade::write(HANDLE win32Data, UINT win32Format) +{ + if (SetClipboardData(win32Format, win32Data) == NULL) { + // free converted data if we couldn't put it on + // the clipboard. + // nb: couldn't cause this in integ tests. + GlobalFree(win32Data); + } +} diff --git a/src/lib/platform/CMSWindowsClipboardFacade.h b/src/lib/platform/CMSWindowsClipboardFacade.h new file mode 100644 index 00000000..db21e1bc --- /dev/null +++ b/src/lib/platform/CMSWindowsClipboardFacade.h @@ -0,0 +1,30 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CMSWINDOWSCLIPBOARDFACADE_H +#define CMSWINDOWSCLIPBOARDFACADE_H + +#include "IMSWindowsClipboardFacade.h" +#include "IClipboard.h" + +class CMSWindowsClipboardFacade : public IMSWindowsClipboardFacade +{ +public: + virtual void write(HANDLE win32Data, UINT win32Format); +}; + +#endif \ No newline at end of file diff --git a/src/lib/platform/CMakeLists.txt b/src/lib/platform/CMakeLists.txt index 44fd51e1..a1914753 100644 --- a/src/lib/platform/CMakeLists.txt +++ b/src/lib/platform/CMakeLists.txt @@ -29,11 +29,13 @@ if (WIN32) CMSWindowsScreenSaver.h CMSWindowsUtil.h CMSWindowsRelauncher.h + IMSWindowsClipboardFacade.h ) set(src ${inc} CMSWindowsClipboard.cpp + CMSWindowsClipboardFacade.cpp CMSWindowsClipboardAnyTextConverter.cpp CMSWindowsClipboardBitmapConverter.cpp CMSWindowsClipboardHTMLConverter.cpp diff --git a/src/lib/platform/IMSWindowsClipboardFacade.h b/src/lib/platform/IMSWindowsClipboardFacade.h new file mode 100644 index 00000000..23202f10 --- /dev/null +++ b/src/lib/platform/IMSWindowsClipboardFacade.h @@ -0,0 +1,34 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef IMWINDOWSCLIPBOARDFACADE +#define IMWINDOWSCLIPBOARDFACADE + +#include "IInterface.h" +#define WIN32_LEAN_AND_MEAN +#include + +class IMSWindowsClipboardConverter; + +class IMSWindowsClipboardFacade : public IInterface +{ +public: + virtual void write(HANDLE win32Data, UINT win32Format) = 0; + virtual ~IMSWindowsClipboardFacade() { } +}; + +#endif \ No newline at end of file diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 82b8922e..c6a59b84 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -14,10 +14,13 @@ # along with this program. If not, see . include_directories( - ../../tools/gtest - ../../tools/gtest/include) - -add_library(gtest STATIC ../../tools/gtest/src/gtest-all.cc) + ../../tools/gtest-1.6.0 + ../../tools/gtest-1.6.0/include + ../../tools/gmock-1.6.0 + ../../tools/gmock-1.6.0/include) + +add_library(gtest STATIC ../../tools/gtest-1.6.0/src/gtest-all.cc) +add_library(gmock STATIC ../../tools/gmock-1.6.0/src/gmock-all.cc) add_subdirectory(integtests) add_subdirectory(unittests) diff --git a/src/test/integtests/CMakeLists.txt b/src/test/integtests/CMakeLists.txt index 8fb2380d..5e0c1091 100644 --- a/src/test/integtests/CMakeLists.txt +++ b/src/test/integtests/CMakeLists.txt @@ -50,7 +50,8 @@ set(inc ../../lib/net ../../lib/platform ../../lib/synergy - ../../../tools/gtest/include + ../../../tools/gtest-1.6.0/include + ../../../tools/gmock-1.6.0/include ) if (UNIX) @@ -62,4 +63,4 @@ endif() include_directories(${inc}) add_executable(integtests ${src}) target_link_libraries(integtests - arch base client common io mt net platform server synergy gtest ${libs}) + arch base client common io mt net platform server synergy gtest gmock ${libs}) diff --git a/src/test/integtests/platform/CMSWindowsClipboardTests.cpp b/src/test/integtests/platform/CMSWindowsClipboardTests.cpp index 3b1e050d..aa0b9e4e 100644 --- a/src/test/integtests/platform/CMSWindowsClipboardTests.cpp +++ b/src/test/integtests/platform/CMSWindowsClipboardTests.cpp @@ -16,8 +16,9 @@ */ #include +#include #include "CMSWindowsClipboard.h" - +#include "IMSWindowsClipboardFacade.h" class CMSWindowsClipboardTests : public ::testing::Test { @@ -41,6 +42,12 @@ private: } }; +class MockFacade : public IMSWindowsClipboardFacade +{ +public: + MOCK_METHOD2(write, void(HANDLE, UINT)); +}; + TEST_F(CMSWindowsClipboardTests, emptyUnowned_openCalled_returnsTrue) { CMSWindowsClipboard clipboard(NULL); @@ -84,6 +91,18 @@ TEST_F(CMSWindowsClipboardTests, add_newValue_valueWasStored) EXPECT_EQ("synergy rocks!", actual); } +TEST_F(CMSWindowsClipboardTests, add_newValue_writeWasCalled) +{ + MockFacade facade; + EXPECT_CALL(facade, write(testing::_, testing::_)); + + CMSWindowsClipboard clipboard(NULL); + clipboard.setFacade(facade); + clipboard.open(0); + + clipboard.add(IClipboard::kText, "synergy rocks!"); +} + TEST_F(CMSWindowsClipboardTests, add_replaceValue_valueWasReplaced) { CMSWindowsClipboard clipboard(NULL); diff --git a/src/test/unittests/CMakeLists.txt b/src/test/unittests/CMakeLists.txt index 1b7f6ad2..e6943a46 100644 --- a/src/test/unittests/CMakeLists.txt +++ b/src/test/unittests/CMakeLists.txt @@ -14,7 +14,7 @@ # along with this program. If not, see . set(src - ../../../tools/gtest/src/gtest_main.cc + ../../../tools/gtest-1.6.0/src/gtest_main.cc synergy/CClipboardTests.cpp ) @@ -28,7 +28,8 @@ set(inc ../../lib/net ../../lib/platform ../../lib/synergy - ../../../tools/gtest/include + ../../../tools/gtest-1.6.0/include + ../../../tools/gmock-1.6.0/include ) if (UNIX) @@ -40,4 +41,4 @@ endif() include_directories(${inc}) add_executable(unittests ${src}) target_link_libraries(unittests - arch base client common io mt net platform server synergy gtest ${libs}) + arch base client common io mt net platform server synergy gtest gmock ${libs})