Back to index

unity  6.0.0
test_im_text_entry.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 Lesser General Public License version 3, as
00006  * published 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 applicable version of the GNU Lesser General Public
00012  * License for more details.
00013  *
00014  * You should have received a copy of both the GNU Lesser General Public
00015  * License version 3 along with this program.  If not, see
00016  * <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00019  *
00020  */
00021 
00022 #include <gmock/gmock.h>
00023 #include "unity-shared/IMTextEntry.h"
00024 
00025 using namespace testing;
00026 using namespace unity;
00027 using namespace nux;
00028 
00029 namespace
00030 {
00031 
00032 class TestEvent : public nux::Event
00033 {
00034 public:
00035   TestEvent(nux::KeyModifier keymod, unsigned long keysym)
00036   {
00037     type = NUX_KEYDOWN;
00038     key_modifiers = keymod;
00039     x11_keysym = keysym;
00040   }
00041 
00042   TestEvent(unsigned long keysym)
00043   {
00044     type = NUX_KEYDOWN;
00045     x11_keysym = keysym;
00046   }
00047 };
00048 
00049 class MockTextEntry : public IMTextEntry
00050 {
00051 public:
00052   MOCK_METHOD0(CutClipboard, void());
00053   MOCK_METHOD0(CopyClipboard, void());
00054   MOCK_METHOD0(PasteClipboard, void());
00055   MOCK_METHOD0(PastePrimaryClipboard, void());
00056 
00057   bool InspectKeyEvent(nux::Event const& event)
00058   {
00059     key_down.emit(event.type, event.GetKeySym(), event.GetKeyState(), nullptr, 0);
00060     return IMTextEntry::InspectKeyEvent(event);
00061   }
00062 };
00063 
00064 
00065 TEST(TestIMTextEntry, CopyCtrlC)
00066 {
00067   MockTextEntry text_entry;
00068 
00069   TestEvent event(KEY_MODIFIER_CTRL, NUX_VK_c);
00070 
00071   EXPECT_CALL(text_entry, CopyClipboard());
00072   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00073 }
00074 
00075 TEST(TestIMTextEntry, CopyCtrlIns)
00076 {
00077   MockTextEntry text_entry;
00078 
00079   TestEvent event(KEY_MODIFIER_CTRL, NUX_VK_INSERT);
00080 
00081   EXPECT_CALL(text_entry, CopyClipboard());
00082   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00083 }
00084 
00085 TEST(TestIMTextEntry, PasteCtrlV)
00086 {
00087   MockTextEntry text_entry;
00088 
00089   TestEvent event(KEY_MODIFIER_CTRL, NUX_VK_v);
00090 
00091   EXPECT_CALL(text_entry, PasteClipboard());
00092   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00093 }
00094 
00095 TEST(TestIMTextEntry, PasteShiftIns)
00096 {
00097   MockTextEntry text_entry;
00098 
00099   TestEvent event(KEY_MODIFIER_SHIFT, NUX_VK_INSERT);
00100 
00101   EXPECT_CALL(text_entry, PasteClipboard());
00102   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00103 }
00104 
00105 TEST(TestIMTextEntry, CutCtrlX)
00106 {
00107   MockTextEntry text_entry;
00108 
00109   TestEvent event(KEY_MODIFIER_CTRL, NUX_VK_x);
00110 
00111   EXPECT_CALL(text_entry, CutClipboard());
00112   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00113 }
00114 
00115 TEST(TestIMTextEntry, CutShiftDel)
00116 {
00117   MockTextEntry text_entry;
00118 
00119   TestEvent event(KEY_MODIFIER_SHIFT, NUX_VK_DELETE);
00120 
00121   EXPECT_CALL(text_entry, CutClipboard());
00122   EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00123 }
00124 
00125 TEST(TestIMTextEntry, CtrlMoveKeys)
00126 {
00127   MockTextEntry text_entry;
00128 
00129   TestEvent left(KEY_MODIFIER_CTRL, NUX_VK_LEFT);
00130   EXPECT_TRUE(text_entry.InspectKeyEvent(left));
00131 
00132   TestEvent right(KEY_MODIFIER_CTRL, NUX_VK_RIGHT);
00133   EXPECT_TRUE(text_entry.InspectKeyEvent(right));
00134 
00135   TestEvent home(KEY_MODIFIER_CTRL, NUX_VK_HOME);
00136   EXPECT_TRUE(text_entry.InspectKeyEvent(home));
00137 
00138   TestEvent end(KEY_MODIFIER_CTRL, NUX_VK_END);
00139   EXPECT_TRUE(text_entry.InspectKeyEvent(end));
00140 }
00141 
00142 TEST(TestIMTextEntry, CtrlDeleteKeys)
00143 {
00144   MockTextEntry text_entry;
00145 
00146   TestEvent del(KEY_MODIFIER_CTRL, NUX_VK_DELETE);
00147   EXPECT_TRUE(text_entry.InspectKeyEvent(del));
00148 
00149   TestEvent backspace(KEY_MODIFIER_CTRL, NUX_VK_BACKSPACE);
00150   EXPECT_TRUE(text_entry.InspectKeyEvent(backspace));
00151 }
00152 
00153 TEST(TestIMTextEntry, CtrlA)
00154 {
00155   MockTextEntry text_entry;
00156 
00157   TestEvent selectall(KEY_MODIFIER_CTRL, NUX_VK_a);
00158   EXPECT_TRUE(text_entry.InspectKeyEvent(selectall));
00159 }
00160 
00161 TEST(TestIMTextEntry, CtrlKeybindings)
00162 {
00163   MockTextEntry text_entry;
00164 
00165   std::vector<unsigned long> allowed_keys { NUX_VK_a, NUX_VK_BACKSPACE,
00166                                             NUX_VK_LEFT, NUX_VK_RIGHT,
00167                                             NUX_VK_HOME, NUX_VK_END,
00168                                             NUX_VK_BACKSPACE, NUX_VK_DELETE };
00169 
00170   for (auto keysym : allowed_keys)
00171   {
00172     TestEvent event(KEY_MODIFIER_CTRL, keysym);
00173     EXPECT_TRUE(text_entry.InspectKeyEvent(event));
00174   }
00175 }
00176 
00177 TEST(TestIMTextEntry, AltKeybindings)
00178 {
00179   MockTextEntry text_entry;
00180 
00181   for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
00182   {
00183     TestEvent event(KEY_MODIFIER_ALT, keysym);
00184     EXPECT_FALSE(text_entry.InspectKeyEvent(event));
00185   }
00186 }
00187 
00188 TEST(TestIMTextEntry, SuperKeybindings)
00189 {
00190   MockTextEntry text_entry;
00191 
00192   for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
00193   {
00194     TestEvent event(KEY_MODIFIER_SUPER, keysym);
00195     EXPECT_FALSE(text_entry.InspectKeyEvent(event));
00196   }
00197 }
00198 
00199 }