summaryrefslogtreecommitdiffstats
path: root/components/mus/gesture_manager_unittest.cc
blob: 2f2ed62d22a38a56bd1dc3bf6346e37f5ed098a4 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mus/gesture_manager.h"

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "components/mus/gesture_manager_delegate.h"
#include "components/mus/public/cpp/keys.h"
#include "components/mus/server_view.h"
#include "components/mus/test_server_view_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/mojo/events/input_events.mojom.h"

namespace view_manager {
namespace {

const uint32_t kInvalidGestureId = GestureManager::kInvalidGestureId;

void MarkAsRespondsToTouch(ServerView* view) {
  std::vector<uint8_t> empty_vector;
  view->SetProperty(mojo::kViewManagerKeyWantsTouchEvents, &empty_vector);
}

std::set<uint32_t> SetWith(uint32_t v1) {
  std::set<uint32_t> result;
  result.insert(v1);
  return result;
}

std::set<uint32_t> SetWith(uint32_t v1, uint32_t v2) {
  std::set<uint32_t> result;
  result.insert(v1);
  result.insert(v2);
  return result;
}

std::set<uint32_t> SetWith(uint32_t v1, uint32_t v2, uint32_t v3) {
  std::set<uint32_t> result;
  result.insert(v1);
  result.insert(v2);
  result.insert(v3);
  return result;
}

std::string EventTypeToString(mojo::EventType event_type) {
  switch (event_type) {
    case mojo::EVENT_TYPE_POINTER_CANCEL:
      return "cancel";
    case mojo::EVENT_TYPE_POINTER_DOWN:
      return "down";
    case mojo::EVENT_TYPE_POINTER_MOVE:
      return "move";
    case mojo::EVENT_TYPE_POINTER_UP:
      return "up";
    default:
      break;
  }
  return std::string("unexpected event");
}

mojo::EventPtr CreateEvent(mojo::EventType type,
                           int32_t pointer_id,
                           int x,
                           int y) {
  mojo::EventPtr event(mojo::Event::New());
  event->action = type;
  event->pointer_data = mojo::PointerData::New();
  event->pointer_data->pointer_id = pointer_id;
  event->pointer_data->location->x = x;
  event->pointer_data->location->y = y;
  return event.Pass();
}

struct CompareViewByConnectionId {
  bool operator()(const ServerView* a, const ServerView* b) {
    return a->id().connection_id < b->id().connection_id;
  }
};

std::string IDsToString(const std::set<uint32_t>& ids) {
  std::string result;
  for (uint32_t id : ids) {
    if (!result.empty())
      result += ",";
    result += base::UintToString(id);
  }
  return result;
}

std::string GestureStateChangeToString(const ServerView* view,
                                       const GestureStateChange& change) {
  std::string result =
      "connection=" + base::IntToString(view->id().connection_id);
  if (change.chosen_gesture != GestureManager::kInvalidGestureId)
    result += " chosen=" + base::UintToString(change.chosen_gesture);
  if (!change.canceled_gestures.empty())
    result += " canceled=" + IDsToString(change.canceled_gestures);
  return result;
}

}  // namespace

class TestGestureManagerDelegate : public GestureManagerDelegate {
 public:
  TestGestureManagerDelegate() {}
  ~TestGestureManagerDelegate() override {}

  std::string GetAndClearDescriptions() {
    const std::string result(base::JoinString(descriptions_, "\n"));
    descriptions_.clear();
    return result;
  }

  std::vector<std::string>& descriptions() { return descriptions_; }

  void AppendDescriptionsFromResults(const ChangeMap& change_map) {
    std::set<const ServerView*, CompareViewByConnectionId> views_by_id;
    for (const auto& pair : change_map)
      views_by_id.insert(pair.first);

    for (auto* view : views_by_id) {
      descriptions_.push_back(
          GestureStateChangeToString(view, change_map.find(view)->second));
    }
  }

  // GestureManagerDelegate:
  void ProcessEvent(const ServerView* view,
                    mojo::EventPtr event,
                    bool has_chosen_gesture) override {
    descriptions_.push_back(
        EventTypeToString(event->action) + " pointer=" +
        base::IntToString(event->pointer_data->pointer_id) + " connection=" +
        base::UintToString(view->id().connection_id) + " chosen=" +
        (has_chosen_gesture ? "true" : "false"));
  }

 private:
  std::vector<std::string> descriptions_;

  DISALLOW_COPY_AND_ASSIGN(TestGestureManagerDelegate);
};

class GestureManagerTest : public testing::Test {
 public:
  GestureManagerTest()
      : root_(&view_delegate_, ViewId(1, 1)),
        child_(&view_delegate_, ViewId(2, 2)),
        gesture_manager_(&gesture_delegate_, &root_) {
    view_delegate_.set_root_view(&root_);
    root_.SetVisible(true);
    MarkAsRespondsToTouch(&root_);
    root_.SetBounds(gfx::Rect(0, 0, 100, 100));
  }
  ~GestureManagerTest() override {}

  void SetGestures(const ServerView* view,
                   int32_t pointer_id,
                   uint32_t chosen_gesture_id,
                   const std::set<uint32_t>& possible_gesture_ids,
                   const std::set<uint32_t>& canceled_ids) {
    scoped_ptr<ChangeMap> result(
        gesture_manager_.SetGestures(view, pointer_id, chosen_gesture_id,
                                     possible_gesture_ids, canceled_ids));
    gesture_delegate_.AppendDescriptionsFromResults(*result);
  }

  void AddChildView() {
    MarkAsRespondsToTouch(&child_);
    child_.SetVisible(true);
    root_.Add(&child_);
    child_.SetBounds(gfx::Rect(0, 0, 100, 100));
  }

 protected:
  TestServerViewDelegate view_delegate_;
  ServerView root_;
  ServerView child_;
  TestGestureManagerDelegate gesture_delegate_;
  GestureManager gesture_manager_;

 private:
  DISALLOW_COPY_AND_ASSIGN(GestureManagerTest);
};

TEST_F(GestureManagerTest, SingleViewAndSingleGesture) {
  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Choose this pointer.
  SetGestures(&root_, pointer_id, 10u, SetWith(10u), std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=10",
            gesture_delegate_.GetAndClearDescriptions());
}

TEST_F(GestureManagerTest, SingleViewAndTwoGestures) {
  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());

  // Delegate should have got nothing.
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Cancel 10, 5 should become active.
  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              SetWith(10u));

  EXPECT_EQ("connection=1 chosen=5 canceled=10",
            gesture_delegate_.GetAndClearDescriptions());
}

TEST_F(GestureManagerTest, TwoViewsSingleGesture) {
  AddChildView();

  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  // Deepest child should be queried first.
  EXPECT_EQ("down pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond from the first view, which triggers the second to be queried.
  SetGestures(&child_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond with 5,10 for the second view. Should get nothing.
  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Cancel 10 in the child.
  SetGestures(&child_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              SetWith(10u));
  EXPECT_EQ("connection=2 canceled=10",
            gesture_delegate_.GetAndClearDescriptions());

  // Choose 5 in the root. This should choose 5 in the root and cancel 5 in
  // the child.
  SetGestures(&root_, pointer_id, 5u, SetWith(5u, 10u), SetWith(10u));
  ASSERT_EQ(2u, gesture_delegate_.descriptions().size());
  EXPECT_EQ("connection=1 chosen=5 canceled=10",
            gesture_delegate_.descriptions()[0]);
  EXPECT_EQ("connection=2 canceled=5", gesture_delegate_.descriptions()[1]);
}

TEST_F(GestureManagerTest, TwoViewsWaitForMoveToChoose) {
  AddChildView();

  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  // Deepest child should be queried first.
  EXPECT_EQ("down pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Send a move. The move should not be processed as GestureManager is
  // still waiting for responses.
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_MOVE, pointer_id, 6, 6));
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Respond from the first view, which triggers the second to be queried.
  SetGestures(&child_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond with 1,2 for the second view.
  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(1u, 2u),
              std::set<uint32_t>());
  // Now that we've responded to the down requests we should get a move for the
  // child.
  EXPECT_EQ("move pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond for the child, root should now get move.
  SetGestures(&child_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());
  EXPECT_EQ("move pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond with nothing chosen for the root. Nothing should come in as no
  // pending moves.
  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(1u, 2u),
              std::set<uint32_t>());
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Send another move event and respond with a chosen id.
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_MOVE, pointer_id, 7, 7));
  EXPECT_EQ("move pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());
  SetGestures(&child_, pointer_id, 5u, SetWith(5u, 10u), std::set<uint32_t>());
  ASSERT_EQ(3u, gesture_delegate_.descriptions().size());
  // Now that a gesture is chosen the move event is generated.
  EXPECT_EQ("move pointer=1 connection=1 chosen=true",
            gesture_delegate_.descriptions()[0]);
  EXPECT_EQ("connection=1 canceled=1,2", gesture_delegate_.descriptions()[1]);
  EXPECT_EQ("connection=2 chosen=5 canceled=10",
            gesture_delegate_.descriptions()[2]);
}

TEST_F(GestureManagerTest, SingleViewNewPointerAfterChoose) {
  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Choose 5.
  SetGestures(&root_, pointer_id, 5u, SetWith(5u, 10u), std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=5 canceled=10",
            gesture_delegate_.GetAndClearDescriptions());

  // Start another down event with a different pointer.
  const int32_t pointer_id2 = 2;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id2, 5, 5));
  EXPECT_EQ("down pointer=2 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // For the new pointer supply the id of a gesture that has been chosen.
  // Even though we didn't explicitly supply 5 as chosen, 5 is chosen because
  // it's already in the chosen state for pointer 1.
  SetGestures(&root_, pointer_id2, kInvalidGestureId, SetWith(5u, 11u),
              std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=5 canceled=11",
            gesture_delegate_.GetAndClearDescriptions());
}

TEST_F(GestureManagerTest, SingleViewChoosingConflictingGestures) {
  // For pointer1 choose 1 with 1,2,3 as possibilities.
  const int32_t pointer1 = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer1, 5, 5));
  gesture_delegate_.GetAndClearDescriptions();
  SetGestures(&root_, pointer1, 1u, SetWith(1u, 2u, 3u), std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=1 canceled=2,3",
            gesture_delegate_.GetAndClearDescriptions());

  // For pointer2 choose 11 with 11,12 as possibilities.
  const int32_t pointer2 = 2;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer2, 5, 5));
  gesture_delegate_.GetAndClearDescriptions();
  SetGestures(&root_, pointer2, 11u, SetWith(11u, 12u), std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=11 canceled=12",
            gesture_delegate_.GetAndClearDescriptions());

  // For pointer3 choose 21 with 1,11,21 as possibilties.
  const int32_t pointer3 = 3;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer3, 5, 5));
  gesture_delegate_.GetAndClearDescriptions();
  SetGestures(&root_, pointer3, 21u, SetWith(1u, 11u, 21u),
              std::set<uint32_t>());
  EXPECT_EQ("connection=1 chosen=21 canceled=1,11",
            gesture_delegate_.GetAndClearDescriptions());
}

TEST_F(GestureManagerTest,
       TwoViewsRespondingWithChosenGestureSendsRemainingEvents) {
  AddChildView();

  // Start two pointer downs, don't respond to either.
  const int32_t pointer1 = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer1, 5, 5));
  EXPECT_EQ("down pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  const int32_t pointer2 = 2;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer2, 5, 5));
  EXPECT_EQ("down pointer=2 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Queue up a move event for pointer1. The event should not be forwarded
  // as we're still waiting.
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_MOVE, pointer1, 5, 5));
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Respond with 1,2 for pointer1 (nothing chosen yet).
  SetGestures(&child_, pointer1, kInvalidGestureId, SetWith(1u, 2u),
              std::set<uint32_t>());
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Respond with 1,2 and choose 1 for pointer2. This results in the following:
  // down for pointer 1 (because we chose a gesture in common with pointer1),
  // move for pointer 1 in both connections (because a gesture was chosen queued
  // up events are sent), down for pointer2 for the root and finally
  // notification of what was chosen.
  SetGestures(&child_, pointer2, 1u, SetWith(1u, 2u), std::set<uint32_t>());
  ASSERT_EQ(5u, gesture_delegate_.descriptions().size());
  EXPECT_EQ("down pointer=1 connection=1 chosen=true",
            gesture_delegate_.descriptions()[0]);
  EXPECT_EQ("move pointer=1 connection=2 chosen=true",
            gesture_delegate_.descriptions()[1]);
  EXPECT_EQ("move pointer=1 connection=1 chosen=true",
            gesture_delegate_.descriptions()[2]);
  EXPECT_EQ("down pointer=2 connection=1 chosen=true",
            gesture_delegate_.descriptions()[3]);
  EXPECT_EQ("connection=2 chosen=1 canceled=2",
            gesture_delegate_.descriptions()[4]);
}

TEST_F(GestureManagerTest, TwoViewsSingleGestureUp) {
  AddChildView();

  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  // Deepest child should be queried first.
  EXPECT_EQ("down pointer=1 connection=2 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Send an up, shouldn't result in anything.
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_UP, pointer_id, 5, 5));
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Respond from the first view, with a chosen gesture.
  SetGestures(&child_, pointer_id, 5u, SetWith(5u, 10u), std::set<uint32_t>());
  ASSERT_EQ(4u, gesture_delegate_.descriptions().size());
  EXPECT_EQ("down pointer=1 connection=1 chosen=true",
            gesture_delegate_.descriptions()[0]);
  EXPECT_EQ("up pointer=1 connection=2 chosen=true",
            gesture_delegate_.descriptions()[1]);
  EXPECT_EQ("up pointer=1 connection=1 chosen=true",
            gesture_delegate_.descriptions()[2]);
  EXPECT_EQ("connection=2 chosen=5 canceled=10",
            gesture_delegate_.descriptions()[3]);
}

TEST_F(GestureManagerTest, SingleViewSingleGestureCancel) {
  const int32_t pointer_id = 1;
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_DOWN, pointer_id, 5, 5));
  EXPECT_EQ("down pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());

  // Send a cancel, shouldn't result in anything.
  gesture_manager_.ProcessEvent(
      *CreateEvent(mojo::EVENT_TYPE_POINTER_CANCEL, pointer_id, 5, 5));
  EXPECT_EQ(std::string(), gesture_delegate_.GetAndClearDescriptions());

  // Respond from the first view, with no gesture, should unblock cancel.
  SetGestures(&root_, pointer_id, kInvalidGestureId, SetWith(5u, 10u),
              std::set<uint32_t>());
  EXPECT_EQ("cancel pointer=1 connection=1 chosen=false",
            gesture_delegate_.GetAndClearDescriptions());
}

}  // namespace view_manager