~brandontschaefer/nux/remove-gconf-ibus-1.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2012 Inalogic Inc.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3, as
* published by the  Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
* PURPOSE.  See the applicable version of the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of both the GNU Lesser General Public
* License version 3 along with this program.  If not, see
* <http://www.gnu.org/licenses/>
*
* Authored by: Brandon Schaefer <brandon.schaefer@canonical.com>
*
*/


#include <gmock/gmock.h>
using namespace testing;

#include "Nux/Nux.h"
#include "Nux/InputAreaProximity.h"
#include "Nux/WindowCompositor.h"
#include "Nux/ProgramFramework/TestView.h"

#include <stack>

namespace
{

const int VIEW_WIDTH = 500;
const int VIEW_HEIGHT = 500;

class TestInputAreaProximity : public Test
{
public:
  TestInputAreaProximity()
    : top_proximity_(0)
    , near_signal_recived_(false)
    , beyond_signal_recived_(false)
    , approaching_signal_recived_(false)
  {
    nux::NuxInitialize(0);
    wnd_thread.reset(nux::CreateNuxWindow("View Test", 300, 200, nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL));
    MoveMouse(VIEW_WIDTH+200, VIEW_HEIGHT+200);
  }

  ~TestInputAreaProximity()
  {
    while (!prox_areas_.empty())
      RemoveInputAreaProximity();
  }

  void AddNInputAreaProximities(int n, int proximity = 10)
  {
    for (int i = 0; i < n; i++)
      AddInputAreaProximity(proximity);
  }

  void AddInputAreaProximity(int proximity = 10)
  {
    nux::TestView* test_view = new nux::TestView("");
    test_view->SetGeometry(nux::Geometry(0, 0, VIEW_WIDTH, VIEW_HEIGHT));
    top_proximity_ = proximity;

    auto prox_area = std::make_shared<nux::InputAreaProximity>(test_view, proximity);

    prox_area->mouse_near.connect([&] (const nux::Point&) {
      near_signal_recived_ = true;
    });

    prox_area->mouse_beyond.connect([&] (const nux::Point&) {
      beyond_signal_recived_ = true;
    });

    prox_area->mouse_approaching.connect([&] (const nux::Point&, const nux::Point& difference) {
      approaching_signal_recived_ = true;
      last_approaching_point_ = difference;
    });

    prox_areas_.push(std::make_pair(prox_area, test_view));
  }

  void RemoveInputAreaProximity()
  {
    if (!prox_areas_.empty())
    {
      auto prox_area = prox_areas_.top();
      prox_areas_.pop();

      prox_area.second->UnReference();
    }
  }

  void ResetSignalRecivedValues()
  {
    near_signal_recived_ = false;
    beyond_signal_recived_ = false;
    approaching_signal_recived_ = false;
  }

  bool NearSignalRecived() const
  {
    return near_signal_recived_;
  }

  bool BeyondSignalRecived() const
  {
    return beyond_signal_recived_;
  }

  bool ApproachingSignalRecived() const
  {
    return approaching_signal_recived_;
  }

  nux::Point GetLastApprochingPoint()
  {
    return last_approaching_point_;
  }

  void MoveMouseNear()
  {
    MoveMouse(VIEW_WIDTH+(top_proximity_-1), VIEW_HEIGHT+(top_proximity_-1));
  }

  void MoveMouseBeyond()
  {
    MoveMouse(VIEW_WIDTH+(top_proximity_+1), VIEW_HEIGHT+(top_proximity_+1));
  }

  void MoveMouse(int x, int y)
  {
    nux::Event event;
    event.type = nux::NUX_MOUSE_MOVE;
    event.x = x;
    event.y = y;
    nux::GetWindowCompositor().ProcessEvent(event);
  }

  void MoveMouseBeyondWindow()
  {
    nux::Event event;
    event.type = nux::NUX_WINDOW_MOUSELEAVE;
    event.x = VIEW_WIDTH + top_proximity_ + 10;
    event.y = VIEW_HEIGHT + top_proximity_ + 10;
    nux::GetWindowCompositor().ProcessEvent(event);
  }

  int GetProxListSize()
  {
    return nux::GetWindowCompositor().GetProximityListSize();
  }

private:
  std::unique_ptr<nux::WindowThread> wnd_thread;
  std::stack<std::pair<std::shared_ptr<nux::InputAreaProximity>, nux::TestView*> > prox_areas_;

  nux::Point last_approaching_point_;

  int top_proximity_;
  bool near_signal_recived_;
  bool beyond_signal_recived_;
  bool approaching_signal_recived_;
};


TEST_F(TestInputAreaProximity, TestAtemptToAddNullToList)
{
  const int prox_size = GetProxListSize();
  nux::InputAreaProximity* prox_area = new nux::InputAreaProximity(NULL, 10);

  MoveMouseNear();
  MoveMouseBeyond();

  delete prox_area;
  ASSERT_EQ(GetProxListSize(), prox_size);
}

TEST_F(TestInputAreaProximity, TestHandlesDeletedInputArea)
{
  const int prox_size = GetProxListSize();

  nux::TestView* test_view = new nux::TestView("");
  test_view->SetGeometry(nux::Geometry(0, 0, VIEW_WIDTH, VIEW_HEIGHT));

  nux::InputAreaProximity* prox_area = new nux::InputAreaProximity(test_view, 10);
  test_view->UnReference();

  MoveMouseNear();
  MoveMouseBeyond();

  delete prox_area;
  ASSERT_EQ(GetProxListSize(), prox_size);
}

TEST_F(TestInputAreaProximity, TestAddsNullProximity)
{
  const int prox_size = nux::GetWindowCompositor().GetProximityListSize();
  nux::GetWindowThread()->GetWindowCompositor().AddAreaInProximityList(NULL);

  MoveMouseNear();
  MoveMouseBeyond();

  nux::GetWindowThread()->GetWindowCompositor().RemoveAreaInProximityList(NULL);
  ASSERT_EQ(nux::GetWindowCompositor().GetProximityListSize(), prox_size);
}

TEST_F(TestInputAreaProximity, TestProximityAreaAddsToList)
{
  ASSERT_EQ(nux::GetWindowCompositor().GetProximityListSize(), 0);
  AddNInputAreaProximities(2);
  ASSERT_EQ(nux::GetWindowCompositor().GetProximityListSize(), 2);
}

TEST_F(TestInputAreaProximity, TestRemovesFromProximityList)
{
  int num_prox_areas = 10;
  AddNInputAreaProximities(num_prox_areas);
  ASSERT_EQ(GetProxListSize(), num_prox_areas);

  for (int i = 0; i < num_prox_areas; i++)
  {
    RemoveInputAreaProximity();
    ASSERT_EQ(GetProxListSize(), num_prox_areas - (i + 1));
  }
}

TEST_F(TestInputAreaProximity, TestNearSignal)
{
  AddInputAreaProximity();

  MoveMouseBeyond();
  ASSERT_FALSE(NearSignalRecived());

  MoveMouseNear();
  ASSERT_TRUE(NearSignalRecived());
}

TEST_F(TestInputAreaProximity, TestApproachingSignal)
{
  AddInputAreaProximity();

  MoveMouseBeyond();
  ASSERT_FALSE(ApproachingSignalRecived());

  MoveMouseNear();
  ASSERT_TRUE(ApproachingSignalRecived());
}

TEST_F(TestInputAreaProximity, TestApproachingDoesNotEmitInsideArea)
{
  AddInputAreaProximity();

  MoveMouse(VIEW_WIDTH/2, VIEW_HEIGHT/2);
  ResetSignalRecivedValues();

  MoveMouse(VIEW_WIDTH/4, VIEW_HEIGHT/4);
  ASSERT_FALSE(ApproachingSignalRecived());

  MoveMouseNear();
  ASSERT_TRUE(ApproachingSignalRecived());
}

TEST_F(TestInputAreaProximity, TestApproachingSignalHorizontal)
{
  int proximity = 10;
  AddInputAreaProximity(proximity);

  nux::Point difference;
  int expected_result;
  for (int i = 1; i <= proximity; i++)
  {
    MoveMouse(VIEW_WIDTH+(proximity-i), VIEW_HEIGHT+(proximity-1));

    difference = GetLastApprochingPoint();
    expected_result = -(proximity - i);
    ASSERT_EQ(expected_result, difference.x);
    ASSERT_EQ(-(proximity-1), difference.y);
  }
}

TEST_F(TestInputAreaProximity, TestApproachingSignalVertical)
{
  int proximity = 10;
  AddInputAreaProximity(proximity);

  nux::Point difference;
  int expected_result;
  for (int i = 1; i <= proximity; i++)
  {
    MoveMouse(VIEW_WIDTH+(proximity-1), VIEW_HEIGHT+(proximity-i));
    difference = GetLastApprochingPoint();
    expected_result = -(proximity - i);
    ASSERT_EQ(-(proximity-1), difference.x);
    ASSERT_EQ(expected_result, difference.y);
  }
}

TEST_F(TestInputAreaProximity, TestApproachingSignalDiagonal)
{
  int proximity = 10;
  AddInputAreaProximity(proximity);

  nux::Point difference;
  int expected_result;
  for (int i = 1; i <= proximity; i++)
  {
    MoveMouse(VIEW_WIDTH+(proximity-i), VIEW_HEIGHT+(proximity-i));
    difference = GetLastApprochingPoint();
    expected_result = -(proximity - i);
    ASSERT_EQ(expected_result, difference.x);
    ASSERT_EQ(expected_result, difference.y);
  }
}

TEST_F(TestInputAreaProximity, TestBeyondSignal)
{
  AddInputAreaProximity();

  MoveMouseNear();
  ASSERT_FALSE(BeyondSignalRecived());

  MoveMouseBeyond();
  ASSERT_TRUE(BeyondSignalRecived());
}

TEST_F(TestInputAreaProximity, TestBeyondSignalEmitWhenLeavingWindow)
{
  AddInputAreaProximity();

  MoveMouseNear();
  ASSERT_FALSE(BeyondSignalRecived());

  MoveMouseBeyondWindow();
  ASSERT_TRUE(BeyondSignalRecived());
}

}