diff options
| author | Sam Spilsbury <sam.spilsbury@canonical.com> | 2011-12-22 14:27:26 +0800 |
|---|---|---|
| committer | Sam Spilsbury <sam.spilsbury@canonical.com> | 2011-12-22 14:27:26 +0800 |
| commit | 89754d3de4eb13f217cf1b407122e1d06eb64939 (patch) | |
| tree | ee63af0d2f00f7fc17b7c7f20734b4bcf90d8c4c /tests | |
| parent | 68c5e0f2c6906915d3f7dc48756fe88ae9d26cf8 (diff) | |
Added Google Test and Google Mock tests for unity::MT::GrabHandle
(bzr r1794.4.1)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 21 | ||||
| -rw-r--r-- | tests/test_grabhandle.cpp | 187 |
2 files changed, 205 insertions, 3 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 15d0c8b6c..16d64dc38 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,6 +40,7 @@ set (LIB_PATHS ${TEST_UNIT_DEPS_LIBRARY_DIRS}) link_directories (${CMAKE_BINARY_DIR}/UnityCore ${LIB_PATHS}) include_directories (. .. ../services ../UnityCore ${UNITY_SRC} ${CMAKE_BINARY_DIR}) +include_directories (${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src) # We can't have convenience libs so we need to rebuild with what we need # Please keep actual test files alphabetically at top and then files @@ -88,7 +89,13 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_glib_signals_utils_ma enable_testing() find_package(GTest) -if (${GTEST_FOUND} STREQUAL TRUE) +# :( +find_library (GMOCK_LIB gmock) +find_library (GMOCK_MAIN_LIB gmock_main) + +if (GTEST_FOUND AND + GMOCK_LIB AND + GMOCK_MAIN_LIB) include_directories(${GTEST_INCLUDE_DIRS}) # The service that provides DBus services to test against @@ -113,6 +120,7 @@ if (${GTEST_FOUND} STREQUAL TRUE) test_favorite_store_gsettings.cpp test_introspection.cpp test_main_xless.cpp + test_grabhandle.cpp ${UNITY_SRC}/AbstractLauncherIcon.h ${UNITY_SRC}/Animator.cpp ${UNITY_SRC}/Animator.h @@ -135,8 +143,13 @@ if (${GTEST_FOUND} STREQUAL TRUE) ${UNITY_SRC}/Timer.h ${UNITY_SRC}/WindowManager.cpp ${UNITY_SRC}/WindowManager.h + ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle.cpp + ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-group.cpp + ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-impl-factory.cpp + ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-layout.cpp + ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-texture.cpp ) - target_link_libraries(test-gtest-xless ${GTEST_BOTH_LIBRARIES}) + target_link_libraries(test-gtest-xless ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIB} ${GMOCK_MAIN_LIB}) add_test(UnityGTestXless test-gtest-xless) add_dependencies(test-gtest-xless unity-core-${UNITY_API_VERSION}) @@ -171,7 +184,9 @@ if (${GTEST_FOUND} STREQUAL TRUE) add_test(UnityGTest test-gtest) add_dependencies(test-gtest unity-core-${UNITY_API_VERSION}) -endif (${GTEST_FOUND} STREQUAL TRUE) +endif (GTEST_FOUND AND + GMOCK_LIB AND + GMOCK_MAIN_LIB) # # check target diff --git a/tests/test_grabhandle.cpp b/tests/test_grabhandle.cpp new file mode 100644 index 000000000..afef025db --- /dev/null +++ b/tests/test_grabhandle.cpp @@ -0,0 +1,187 @@ +#include <list> +#include <algorithm> +#include <gtest/gtest.h> +#include <gmock/gmock.h> +#include <unity-mt-grab-handle.h> +#include <unity-mt-grab-handle-layout.h> +#include <unity-mt-grab-handle-group.h> +#include <unity-mt-grab-handle-impl-factory.h> +#include <unity-mt-texture.h> +#include <boost/shared_ptr.hpp> + +unsigned int unity::MT::MaximizedHorzMask = (1 << 0); +unsigned int unity::MT::MaximizedVertMask = (1 << 1); +unsigned int unity::MT::MoveMask = (1 << 0); +unsigned int unity::MT::ResizeMask = (1 << 1); + +using namespace unity::MT; +using ::testing::AtLeast; +using ::testing::_; + +class MockGrabHandleImpl : public unity::MT::GrabHandle::Impl +{ +public: + MockGrabHandleImpl () : unity::MT::GrabHandle::Impl () + { + EXPECT_CALL (*this, damage (_)).Times (AtLeast (3)); + EXPECT_CALL (*this, lockPosition (_, _, unity::MT::PositionSet | unity::MT::PositionLock)).Times (AtLeast (1)); + } + + MOCK_METHOD0 (show, void ()); + MOCK_METHOD0 (hide, void ()); + MOCK_CONST_METHOD3 (buttonPress, void (int, int, unsigned int)); + MOCK_METHOD3 (lockPosition, void (int, int, unsigned int)); + MOCK_METHOD1 (damage, void (const nux::Geometry &)); +}; + +class MockGrabHandleImplFactory : public unity::MT::GrabHandle::ImplFactory +{ +public: + MockGrabHandleImplFactory () : ImplFactory () {}; + + virtual GrabHandle::Impl * create (const GrabHandle::Ptr &h); +}; + +class MockGrabHandleTexture : public unity::MT::Texture +{ +public: + typedef boost::shared_ptr <MockGrabHandleTexture> Ptr; + MockGrabHandleTexture () : unity::MT::Texture () {}; +}; + +class MockGrabHandleTextureFactory : public unity::MT::Texture::Factory +{ +public: + MockGrabHandleTextureFactory () : Factory () {}; + + virtual unity::MT::Texture::Ptr create (); +}; + +class MockGrabHandleWindow : public unity::MT::GrabHandleWindow +{ +public: + MockGrabHandleWindow () : GrabHandleWindow () {}; + MOCK_METHOD4 (requestMovement, void (int, int, unsigned int, unsigned int)); + MOCK_METHOD1 (raiseGrabHandle, void (const boost::shared_ptr <const unity::MT::GrabHandle> &)); +}; + +Texture::Ptr +MockGrabHandleTextureFactory::create () +{ + return boost::shared_static_cast <Texture> (MockGrabHandleTexture::Ptr (new MockGrabHandleTexture ())); +} + +GrabHandle::Impl * +MockGrabHandleImplFactory::create (const GrabHandle::Ptr &h) +{ + return new MockGrabHandleImpl (); +} + +namespace { + +// The fixture for testing class UnityMTGrabHandleTest. +class UnityMTGrabHandleTest : public ::testing::Test { +protected: + + UnityMTGrabHandleTest() : + handlesMask (0), + window (new MockGrabHandleWindow) + { + unity::MT::GrabHandle::ImplFactory::SetDefault (new MockGrabHandleImplFactory ()); + unity::MT::Texture::Factory::SetDefault (new MockGrabHandleTextureFactory ()); + } + + virtual ~UnityMTGrabHandleTest() { + } + + virtual void SetUp() { + } + + virtual void TearDown() { + } + + unsigned int handlesMask; + MockGrabHandleWindow *window; +}; + +TEST_F(UnityMTGrabHandleTest, TestLayoutMasks) { + handlesMask = unity::MT::getLayoutForMask (MaximizedVertMask, MoveMask | ResizeMask); + EXPECT_EQ (handlesMask, LeftHandle | RightHandle | MiddleHandle); + + handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, MoveMask | ResizeMask); + EXPECT_EQ (handlesMask, BottomHandle | TopHandle | MiddleHandle); + + handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask | MaximizedVertMask, MoveMask | ResizeMask); + EXPECT_EQ (handlesMask, MiddleHandle); + + handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask | MaximizedVertMask, ResizeMask); + EXPECT_EQ (handlesMask, 0); + + handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, ResizeMask); + EXPECT_EQ (handlesMask, BottomHandle | TopHandle); + + handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, ResizeMask); + EXPECT_EQ (handlesMask, BottomHandle | TopHandle); + + handlesMask = unity::MT::getLayoutForMask (MaximizedVertMask, ResizeMask); + EXPECT_EQ (handlesMask, RightHandle | LeftHandle); + + handlesMask = unity::MT::getLayoutForMask (0, ResizeMask); + EXPECT_EQ (handlesMask, TopLeftHandle | TopHandle | TopRightHandle | + LeftHandle | RightHandle | + BottomLeftHandle | BottomHandle | BottomRightHandle); + + handlesMask = unity::MT::getLayoutForMask (0, ResizeMask | MoveMask); + EXPECT_EQ (handlesMask, TopLeftHandle | TopHandle | TopRightHandle | + LeftHandle | RightHandle | MiddleHandle | + BottomLeftHandle | BottomHandle | BottomRightHandle); + + handlesMask = unity::MT::getLayoutForMask (0, MoveMask); + EXPECT_EQ (handlesMask, MiddleHandle); + + handlesMask = unity::MT::getLayoutForMask (0, 0); + EXPECT_EQ (handlesMask, 0); +} + +TEST_F(UnityMTGrabHandleTest, TestLayouts) +{ + std::vector <TextureSize> textures; + + for (unsigned int i = 0; i < unity::MT::NUM_HANDLES; i++) + textures.push_back (TextureSize (MockGrabHandleTextureFactory::Default ()->create (), nux::Geometry (0, 0, 100, 100))); + + GrabHandleGroup::Ptr group = GrabHandleGroup::create (window, textures); + + group->relayout (nux::Geometry (250, 250, 1000, 1000), true); + + /* Offset by x = -50, y = -50 + * since that is size / 2 */ + struct expected_positions + { + float x; + float y; + } positions[9] = + { + { 200, 200 }, + { 700, 200 }, + { 1200, 200 }, + { 1200, 700 }, + { 1200, 1200 }, + { 700, 1200 }, + { 200, 1200 }, + { 200, 700 }, + { 700, 700 } + }; + + unsigned int count = 0; + + /* Check handle positions */ + group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h) + { + EXPECT_EQ (h->x (), positions[count].x); + EXPECT_EQ (h->y (), positions[count].y); + count++; + }); +} + +} // namespace |
