Back to index

unity  6.0.0
test_gesture_engine.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
00018  *
00019  */
00020 
00021 #include <gtest/gtest.h>
00022 #include <compiz_mock/core/core.h>
00023 #include "GestureEngine.h"
00024 
00025 CompScreenMock concrete_screen_mock;
00026 CompScreenMock *screen_mock = &concrete_screen_mock;
00027 int pointerX_mock = 0;
00028 int pointerY_mock = 0;
00029 
00030 class GestureEngineTest : public ::testing::Test {
00031  protected:
00032   virtual void SetUp() {
00033     screen_mock->_width = 1280;
00034     screen_mock->_height = 1024;
00035 
00036     GenerateWindows();
00037   }
00038 
00039   void PerformPinch(GestureEngine &gesture_engine, float peak_radius) {
00040     CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00041 
00042     GeisAdapterMock::GeisTouchData touch_data;
00043     touch_data.id = 1;
00044     touch_data.touches = 3;
00045     touch_data.window = 123;
00046     touch_data.focus_x = 100; /* hits the middle window */
00047     touch_data.focus_y = 100;
00048     gesture_engine.OnTouchStart(&touch_data);
00049 
00050     GeisAdapterMock::GeisPinchData pinch_data;
00051     pinch_data.id = 1;
00052     pinch_data.touches = 3;
00053     pinch_data.window = 123;
00054     pinch_data.focus_x = 100; /* hits the middle window */
00055     pinch_data.focus_y = 100;
00056     pinch_data.radius = 1.0;
00057     gesture_engine.OnPinchStart(&pinch_data);
00058 
00059     touch_data.focus_x += 10;
00060     touch_data.focus_y += 20;
00061     gesture_engine.OnTouchUpdate(&touch_data);
00062 
00063     pinch_data.focus_x += 10;
00064     pinch_data.focus_y += 20;
00065     pinch_data.radius = peak_radius;
00066     gesture_engine.OnPinchUpdate(&pinch_data);
00067 
00068     gesture_engine.OnTouchFinish(&touch_data);
00069     gesture_engine.OnPinchFinish(&pinch_data);
00070   }
00071 
00072  private:
00073   void GenerateWindows() {
00074     /* remove windows from previous test */
00075     for (auto window : screen_mock->_client_list_stacking) {
00076       delete window;
00077     }
00078     screen_mock->_client_list_stacking.clear();
00079 
00080     /* and generate new ones */
00081     CompWindowMock *window;
00082 
00083     /* the root window */
00084     window = new CompWindowMock;
00085     /* x, y, width, height, border */
00086     window->_geometry.set(0, 0, screen_mock->width(), screen_mock->height(), 0);
00087     window->_serverGeometry = window->_geometry;
00088     window->_actions = 0;
00089     window->_state = 0;
00090     screen_mock->_client_list_stacking.push_back(window);
00091 
00092     /* middle window */
00093     window = new CompWindowMock;
00094     window->_geometry.set(10, 10, 400, 400, 0);
00095     window->_serverGeometry = window->_geometry;
00096     window->_actions = CompWindowActionMoveMask;
00097     window->_state = 0;
00098     screen_mock->_client_list_stacking.push_back(window);
00099 
00100     /* top-level window */
00101     window = new CompWindowMock;
00102     window->_geometry.set(500, 500, 410, 410, 0);
00103     window->_serverGeometry = window->_geometry;
00104     window->_actions = CompWindowActionMoveMask;
00105     window->_state = 0;
00106     screen_mock->_client_list_stacking.push_back(window);
00107 
00108     screen_mock->_client_list = screen_mock->_client_list_stacking;
00109     std::reverse(screen_mock->_client_list.begin(),
00110                  screen_mock->_client_list.end());
00111   }
00112 };
00113 
00114 TEST_F(GestureEngineTest, ThreeFingersDragMovesWindow)
00115 {
00116   GestureEngine gestureEngine(screen_mock);
00117   CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00118 
00119   GeisAdapterMock::GeisTouchData touch_data;
00120   touch_data.id = 1;
00121   touch_data.touches = 3;
00122   touch_data.window = 123;
00123   touch_data.focus_x = 100; /* hits the middle window */
00124   touch_data.focus_y = 100;
00125   gestureEngine.OnTouchStart(&touch_data);
00126 
00127   GeisAdapterMock::GeisDragData drag_data;
00128   drag_data.id = 1;
00129   drag_data.touches = 3;
00130   drag_data.window = 123;
00131   drag_data.focus_x = 100; /* hits the middle window */
00132   drag_data.focus_y = 100;
00133   gestureEngine.OnDragStart(&drag_data);
00134 
00135   ASSERT_FALSE(middle_window->_moved);
00136 
00137   touch_data.focus_x += 10;
00138   touch_data.focus_y += 20;
00139   gestureEngine.OnTouchUpdate(&touch_data);
00140 
00141   drag_data.delta_x = 10;
00142   drag_data.delta_y = 20;
00143   drag_data.focus_x += drag_data.delta_x;
00144   drag_data.focus_y += drag_data.delta_x;
00145   gestureEngine.OnDragUpdate(&drag_data);
00146 
00147   ASSERT_TRUE(middle_window->_moved);
00148   ASSERT_EQ(drag_data.delta_x, middle_window->_movement_x);
00149   ASSERT_EQ(drag_data.delta_y, middle_window->_movement_y);
00150 }
00151 
00152 TEST_F(GestureEngineTest, ThreeFingersDragDoesntMoveStaticWindow)
00153 {
00154   GestureEngine gestureEngine(screen_mock);
00155   CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00156 
00157   /* can't be moved */
00158   middle_window->_actions = 0;
00159 
00160   GeisAdapterMock::GeisTouchData touch_data;
00161   touch_data.id = 1;
00162   touch_data.touches = 3;
00163   touch_data.window = 123;
00164   touch_data.focus_x = 100; /* hits the middle window */
00165   touch_data.focus_y = 100;
00166   gestureEngine.OnTouchStart(&touch_data);
00167 
00168   GeisAdapterMock::GeisDragData drag_data;
00169   drag_data.id = 1;
00170   drag_data.touches = 3;
00171   drag_data.window = 123;
00172   drag_data.focus_x = 100; /* hits the middle window */
00173   drag_data.focus_y = 100;
00174   gestureEngine.OnDragStart(&drag_data);
00175 
00176   ASSERT_FALSE(middle_window->_moved);
00177 
00178   touch_data.focus_x += 10;
00179   touch_data.focus_y += 20;
00180   gestureEngine.OnTouchUpdate(&touch_data);
00181 
00182   drag_data.delta_x = 10;
00183   drag_data.delta_y = 20;
00184   drag_data.focus_x += drag_data.delta_x;
00185   drag_data.focus_y += drag_data.delta_x;
00186   gestureEngine.OnDragUpdate(&drag_data);
00187 
00188   ASSERT_FALSE(middle_window->_moved);
00189 }
00190 
00191 TEST_F(GestureEngineTest, ThreeFingersPinchMaximizesWindow)
00192 {
00193   GestureEngine gesture_engine(screen_mock);
00194   CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00195 
00196   PerformPinch(gesture_engine, 2.0);
00197 
00198   ASSERT_EQ(1, middle_window->_maximize_count);
00199   ASSERT_EQ(MAXIMIZE_STATE, middle_window->_maximize_state);
00200 }
00201 
00202 TEST_F(GestureEngineTest, ThreeFingersPinchRestoresWindow)
00203 {
00204   GestureEngine gesture_engine(screen_mock);
00205   CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00206 
00207   PerformPinch(gesture_engine, 0.3);
00208 
00209   ASSERT_EQ(1, middle_window->_maximize_count);
00210   ASSERT_EQ(0, middle_window->_maximize_state);
00211 }
00212 
00213 TEST_F(GestureEngineTest, MinimalThreeFingersPinchDoesNothing)
00214 {
00215   GestureEngine gesture_engine(screen_mock);
00216   CompWindowMock *middle_window = screen_mock->_client_list_stacking[1];
00217 
00218   PerformPinch(gesture_engine, 1.1);
00219 
00220   ASSERT_EQ(0, middle_window->_maximize_count);
00221 }
00222 
00223 /* Regression test for lp:979418, where the grab is not removed if the gesture
00224  * id is 0. */
00225 TEST_F(GestureEngineTest, DragGrabCheck)
00226 {
00227   screen_mock->_grab_count = 0;
00228 
00229   GestureEngine gesture_engine(screen_mock);
00230 
00231   GeisAdapterMock::GeisDragData drag_data;
00232   drag_data.id = 0;
00233   drag_data.touches = 3;
00234   drag_data.window = 123;
00235   drag_data.focus_x = 100; /* hits the middle window */
00236   drag_data.focus_y = 100;
00237   gesture_engine.OnDragStart(&drag_data);
00238 
00239   gesture_engine.OnDragFinish(&drag_data);
00240 
00241   ASSERT_EQ(0, screen_mock->_grab_count);
00242 }
00243 
00244 int main(int argc, char** argv)
00245 {
00246   ::testing::InitGoogleTest(&argc, argv);
00247 
00248   int ret = RUN_ALL_TESTS();
00249 
00250   return ret;
00251 }