Back to index

unity  6.0.0
test_grabhandle.cpp
Go to the documentation of this file.
00001 #include <list>
00002 #include <algorithm>
00003 #include <gtest/gtest.h>
00004 #include <gmock/gmock.h>
00005 #include <unity-mt-grab-handle.h>
00006 #include <unity-mt-grab-handle-layout.h>
00007 #include <unity-mt-grab-handle-group.h>
00008 #include <unity-mt-grab-handle-impl-factory.h>
00009 #include <unity-mt-texture.h>
00010 #include <boost/shared_ptr.hpp>
00011 
00012 unsigned int unity::MT::MaximizedHorzMask = (1 << 0);
00013 unsigned int unity::MT::MaximizedVertMask = (1 << 1);
00014 unsigned int unity::MT::MoveMask = (1 << 0);
00015 unsigned int unity::MT::ResizeMask = (1 << 1);
00016 
00017 using namespace unity::MT;
00018 using ::testing::AtLeast;
00019 using ::testing::_;
00020 
00021 class MockLayoutGrabHandleImpl : public unity::MT::GrabHandle::Impl
00022 {
00023 public:
00024   MockLayoutGrabHandleImpl () : unity::MT::GrabHandle::Impl ()
00025   {
00026     EXPECT_CALL (*this, damage (_)).Times (AtLeast (3));
00027     EXPECT_CALL (*this, lockPosition (_, _, unity::MT::PositionSet | unity::MT::PositionLock)).Times (AtLeast (1));
00028   }
00029 
00030   MOCK_METHOD0 (show, void ());
00031   MOCK_METHOD0 (hide, void ());
00032   MOCK_CONST_METHOD3 (buttonPress, void (int, int, unsigned int));
00033   MOCK_METHOD3 (lockPosition, void (int, int, unsigned int));
00034   MOCK_METHOD1 (damage, void (const nux::Geometry &));
00035 };
00036 
00037 class MockLayoutGrabHandleImplFactory : public unity::MT::GrabHandle::ImplFactory
00038 {
00039 public:
00040   MockLayoutGrabHandleImplFactory () : ImplFactory () {};
00041 
00042   GrabHandle::Impl * create (const GrabHandle::Ptr &h);
00043 };
00044 
00045 class MockAnimationGrabHandleImpl : public unity::MT::GrabHandle::Impl
00046 {
00047 public:
00048   MockAnimationGrabHandleImpl () : unity::MT::GrabHandle::Impl ()
00049   {
00050     EXPECT_CALL (*this, damage (_)).Times (AtLeast (unity::MT::FADE_MSEC * 2));
00051     EXPECT_CALL (*this, show ()).Times (AtLeast (1));
00052     EXPECT_CALL (*this, hide ()).Times (AtLeast (1));
00053   }
00054 
00055   MOCK_METHOD0 (show, void ());
00056   MOCK_METHOD0 (hide, void ());
00057   MOCK_CONST_METHOD3 (buttonPress, void (int, int, unsigned int));
00058   MOCK_METHOD3 (lockPosition, void (int, int, unsigned int));
00059   MOCK_METHOD1 (damage, void (const nux::Geometry &));
00060 };
00061 
00062 class MockAnimationGrabHandleImplFactory : public unity::MT::GrabHandle::ImplFactory
00063 {
00064 public:
00065   MockAnimationGrabHandleImplFactory () : ImplFactory () {};
00066 
00067   GrabHandle::Impl * create (const GrabHandle::Ptr &h);
00068 };
00069 
00070 class MockShowHideGrabHandleImpl : public unity::MT::GrabHandle::Impl
00071 {
00072 public:
00073   MockShowHideGrabHandleImpl () : unity::MT::GrabHandle::Impl ()
00074   {
00075     EXPECT_CALL (*this, damage (_)).Times (AtLeast (1));
00076     EXPECT_CALL (*this, show ()).Times (AtLeast (2));
00077     EXPECT_CALL (*this, hide ()).Times (AtLeast (2));
00078   }
00079 
00080   MOCK_METHOD0 (show, void ());
00081   MOCK_METHOD0 (hide, void ());
00082   MOCK_CONST_METHOD3 (buttonPress, void (int, int, unsigned int));
00083   MOCK_METHOD3 (lockPosition, void (int, int, unsigned int));
00084   MOCK_METHOD1 (damage, void (const nux::Geometry &));
00085 };
00086 
00087 class MockShowHideGrabHandleImplFactory : public unity::MT::GrabHandle::ImplFactory
00088 {
00089 public:
00090   MockShowHideGrabHandleImplFactory () : ImplFactory () {};
00091 
00092   GrabHandle::Impl * create (const GrabHandle::Ptr &h);
00093 };
00094 
00095 class MockGrabHandleTexture : public unity::MT::Texture
00096 {
00097 public:
00098   typedef boost::shared_ptr <MockGrabHandleTexture> Ptr;
00099   MockGrabHandleTexture () : unity::MT::Texture () {};
00100 };
00101 
00102 class MockGrabHandleTextureFactory : public unity::MT::Texture::Factory
00103 {
00104 public:
00105   MockGrabHandleTextureFactory () : Factory () {};
00106 
00107   unity::MT::Texture::Ptr create ();
00108 };
00109 
00110 class MockGrabHandleWindow : public unity::MT::GrabHandleWindow
00111 {
00112 public:
00113   MockGrabHandleWindow () : GrabHandleWindow () {};
00114   MOCK_METHOD4 (requestMovement, void (int, int, unsigned int, unsigned int));
00115   MOCK_METHOD1 (raiseGrabHandle, void (const boost::shared_ptr <const unity::MT::GrabHandle> &));
00116 };
00117 
00118 Texture::Ptr
00119 MockGrabHandleTextureFactory::create ()
00120 {
00121   return boost::shared_static_cast <Texture> (MockGrabHandleTexture::Ptr (new MockGrabHandleTexture ()));
00122 }
00123 
00124 GrabHandle::Impl *
00125 MockLayoutGrabHandleImplFactory::create (const GrabHandle::Ptr &h)
00126 {
00127   return new MockLayoutGrabHandleImpl ();
00128 }
00129 
00130 GrabHandle::Impl *
00131 MockShowHideGrabHandleImplFactory::create (const GrabHandle::Ptr &h)
00132 {
00133   return new MockShowHideGrabHandleImpl ();
00134 }
00135 
00136 GrabHandle::Impl *
00137 MockAnimationGrabHandleImplFactory::create (const GrabHandle::Ptr &h)
00138 {
00139   return new MockAnimationGrabHandleImpl ();
00140 }
00141 
00142 namespace {
00143 
00144 // The fixture for testing class UnityMTGrabHandleTest.
00145 class UnityMTGrabHandleTest : public ::testing::Test {
00146 protected:
00147 
00148   UnityMTGrabHandleTest() :
00149     handlesMask (0),
00150     window (new MockGrabHandleWindow)
00151   {
00152 
00153   }
00154 
00155   unsigned int handlesMask;
00156   MockGrabHandleWindow *window;
00157 };
00158 
00159 TEST_F(UnityMTGrabHandleTest, TestLayoutMasks) {
00160   unity::MT::GrabHandle::ImplFactory::SetDefault (new MockLayoutGrabHandleImplFactory ());
00161   unity::MT::Texture::Factory::SetDefault (new MockGrabHandleTextureFactory ());
00162 
00163   handlesMask = unity::MT::getLayoutForMask (MaximizedVertMask, MoveMask | ResizeMask);
00164   EXPECT_EQ (handlesMask, LeftHandle | RightHandle | MiddleHandle);
00165 
00166   handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, MoveMask | ResizeMask);
00167   EXPECT_EQ (handlesMask, BottomHandle | TopHandle | MiddleHandle);
00168 
00169   handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask | MaximizedVertMask, MoveMask | ResizeMask);
00170   EXPECT_EQ (handlesMask, MiddleHandle);
00171 
00172   handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask | MaximizedVertMask, ResizeMask);
00173   EXPECT_EQ (handlesMask, 0);
00174 
00175   handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, ResizeMask);
00176   EXPECT_EQ (handlesMask, BottomHandle | TopHandle);
00177 
00178   handlesMask = unity::MT::getLayoutForMask (MaximizedHorzMask, ResizeMask);
00179   EXPECT_EQ (handlesMask, BottomHandle | TopHandle);
00180 
00181   handlesMask = unity::MT::getLayoutForMask (MaximizedVertMask, ResizeMask);
00182   EXPECT_EQ (handlesMask, RightHandle | LeftHandle);
00183 
00184   handlesMask = unity::MT::getLayoutForMask (0, ResizeMask);
00185   EXPECT_EQ (handlesMask, TopLeftHandle | TopHandle | TopRightHandle |
00186                           LeftHandle | RightHandle |
00187                           BottomLeftHandle | BottomHandle | BottomRightHandle);
00188 
00189   handlesMask = unity::MT::getLayoutForMask (0, ResizeMask | MoveMask);
00190   EXPECT_EQ (handlesMask, TopLeftHandle | TopHandle | TopRightHandle |
00191                           LeftHandle | RightHandle | MiddleHandle |
00192                           BottomLeftHandle | BottomHandle | BottomRightHandle);
00193 
00194   handlesMask = unity::MT::getLayoutForMask (0, MoveMask);
00195   EXPECT_EQ (handlesMask, MiddleHandle);
00196 
00197   handlesMask = unity::MT::getLayoutForMask (0, 0);
00198   EXPECT_EQ (handlesMask, 0);
00199 }
00200 
00201 TEST_F(UnityMTGrabHandleTest, TestLayouts)
00202 {
00203   unity::MT::GrabHandle::ImplFactory::SetDefault (new MockLayoutGrabHandleImplFactory ());
00204   unity::MT::Texture::Factory::SetDefault (new MockGrabHandleTextureFactory ());
00205 
00206   std::vector <TextureSize> textures;
00207 
00208   for (unsigned int i = 0; i < unity::MT::NUM_HANDLES; i++)
00209     textures.push_back (TextureSize (MockGrabHandleTextureFactory::Default ()->create (), nux::Geometry (0, 0, 100, 100)));
00210 
00211   GrabHandleGroup::Ptr group = GrabHandleGroup::create (window, textures);
00212 
00213   group->relayout (nux::Geometry (250, 250, 1000, 1000), true);
00214 
00215   /* Offset by x = -50, y = -50
00216    * since that is size / 2 */
00217   struct expected_positions
00218   {
00219     float x;
00220     float y;
00221   } positions[9] =
00222   {
00223     { 200, 200 },
00224     { 700, 200 },
00225     { 1200, 200 },
00226     { 1200, 700 },
00227     { 1200, 1200 },
00228     { 700, 1200 }, 
00229     { 200, 1200 },
00230     { 200, 700 },
00231     { 700, 700 }
00232   };
00233 
00234   unsigned int count = 0;
00235 
00236   /* Check handle positions */
00237   group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h)
00238                         {
00239                           EXPECT_EQ (h->x (), positions[count].x);
00240                           EXPECT_EQ (h->y (), positions[count].y);
00241                           count++;
00242                         });
00243 }
00244 
00245 TEST_F(UnityMTGrabHandleTest, TestShowHide)
00246 {
00247   unity::MT::GrabHandle::ImplFactory::SetDefault (new MockShowHideGrabHandleImplFactory ());
00248   unity::MT::Texture::Factory::SetDefault (new MockGrabHandleTextureFactory ());
00249 
00250   std::vector <TextureSize> textures;
00251 
00252   for (unsigned int i = 0; i < unity::MT::NUM_HANDLES; i++)
00253     textures.push_back (TextureSize (MockGrabHandleTextureFactory::Default ()->create (), nux::Geometry (0, 0, 100, 100)));
00254 
00255   GrabHandleGroup::Ptr group = GrabHandleGroup::create (window, textures);
00256 
00257   group->show (0);
00258   group->show (TopLeftHandle | TopHandle | TopRightHandle);
00259   group->show (LeftHandle);
00260   group->show (MiddleHandle);
00261   group->show (RightHandle);
00262   group->show (BottomLeftHandle | BottomHandle | BottomRightHandle);
00263 
00264   group->hide ();
00265   group->show ();
00266   group->hide ();
00267 }
00268 
00269 TEST_F(UnityMTGrabHandleTest, TestAnimations)
00270 {
00271   int opacity = 0;
00272   unity::MT::GrabHandle::ImplFactory::SetDefault (new MockAnimationGrabHandleImplFactory ());
00273   unity::MT::Texture::Factory::SetDefault (new MockGrabHandleTextureFactory ());
00274 
00275   unity::MT::FADE_MSEC = 10;
00276 
00277   std::vector <TextureSize> textures;
00278 
00279   for (unsigned int i = 0; i < unity::MT::NUM_HANDLES; i++)
00280     textures.push_back (TextureSize (MockGrabHandleTextureFactory::Default ()->create (), nux::Geometry (0, 0, 100, 100)));
00281 
00282   GrabHandleGroup::Ptr group = GrabHandleGroup::create (window, textures);
00283 
00284   group->show ();
00285   for (unsigned int i = 0; i < unity::MT::FADE_MSEC; i++)
00286   {
00287     group->animate (1);
00288     EXPECT_TRUE (group->needsAnimate ());
00289     EXPECT_TRUE (group->visible ());
00290     opacity += ((1 /
00291                 static_cast <float> (unity::MT::FADE_MSEC)) *
00292                 std::numeric_limits <unsigned short>::max ());
00293     opacity = std::min (opacity, static_cast <int> (std::numeric_limits <unsigned short>::max ()));
00294     EXPECT_EQ (group->opacity (), opacity);
00295     group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h)
00296                           {
00297                             h->damage (nux::Geometry (h->x (),
00298                                                       h->y (),
00299                                                       h->width (),
00300                                                       h->height ()));
00301                           });
00302   }
00303 
00304   group->animate (1);
00305   group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h)
00306                         {
00307                           h->damage (nux::Geometry (h->x (),
00308                                                     h->y (),
00309                                                     h->width (),
00310                                                     h->height ()));
00311                         });
00312   EXPECT_FALSE (group->needsAnimate ());
00313   EXPECT_EQ (group->opacity (), std::numeric_limits <unsigned short>::max ());
00314 
00315   opacity = group->opacity ();
00316 
00317   group->hide ();
00318   for (unsigned int i = 0; i < unity::MT::FADE_MSEC - 1; i++)
00319   {
00320     group->animate (1);
00321     EXPECT_TRUE (group->needsAnimate ());
00322     EXPECT_TRUE (group->visible ());
00323     opacity -= ((1 /
00324                 static_cast <float> (unity::MT::FADE_MSEC)) *
00325                 std::numeric_limits <unsigned short>::max ());
00326     opacity = std::max (opacity, 0);
00327     EXPECT_EQ (group->opacity (), opacity);
00328     group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h)
00329                           {
00330                             h->damage (nux::Geometry (h->x (),
00331                                                       h->y (),
00332                                                       h->width (),
00333                                                       h->height ()));
00334                           });
00335   }
00336 
00337   group->animate (1);
00338   group->forEachHandle ([&](const unity::MT::GrabHandle::Ptr &h)
00339                         {
00340                           h->damage (nux::Geometry (h->x (),
00341                                                     h->y (),
00342                                                     h->width (),
00343                                                     h->height ()));
00344                         });
00345   EXPECT_FALSE (group->needsAnimate ());
00346   EXPECT_EQ (group->opacity (), 0);
00347 
00348 }
00349 
00350 }  // namespace