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

ash / fast_ink / laser / laser_pointer_controller_unittest.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/fast_ink/laser/laser_pointer_controller.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/fast_ink/laser/laser_pointer_controller_test_api.h"
#include "ash/fast_ink/laser/laser_pointer_view.h"
#include "ash/public/cpp/stylus_utils.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "ui/events/test/event_generator.h"

namespace ash {
namespace {

class TestLaserPointerObserver : public LaserPointerObserver {
 public:
  TestLaserPointerObserver() = default;
  ~TestLaserPointerObserver() override = default;

  // LaserPointerObserver:
  void OnLaserPointerStateChanged(bool enabled) override {
    laser_pointer_enabled_ = enabled;
  }

  bool laser_pointer_enabled() { return laser_pointer_enabled_; }

 private:
  bool laser_pointer_enabled_ = false;
};

class LaserPointerControllerTest : public AshTestBase {
 public:
  LaserPointerControllerTest() = default;
  LaserPointerControllerTest(const LaserPointerControllerTest&) = delete;
  LaserPointerControllerTest& operator=(const LaserPointerControllerTest&) =
      delete;
  ~LaserPointerControllerTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();
    observer_ = std::make_unique<TestLaserPointerObserver>();
    controller_ = std::make_unique<LaserPointerController>();
    controller_test_api_ =
        std::make_unique<LaserPointerControllerTestApi>(controller_.get());
  }

  void TearDown() override {
    controller_test_api_.reset();
    // This needs to be called first to remove the event handler before the
    // shell instance gets torn down.
    controller_.reset();
    AshTestBase::TearDown();
  }

 protected:
  void VerifyLaserPointerRendererTouchEvent() {
    ui::test::EventGenerator* event_generator = GetEventGenerator();

    // When disabled the laser pointer should not be showing.
    event_generator->MoveTouch(gfx::Point(1, 1));
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify that by enabling the mode, the laser pointer should still not be
    // showing.
    controller_test_api_->SetEnabled(true);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify moving the finger 4 times will not display the laser pointer.
    event_generator->MoveTouch(gfx::Point(2, 2));
    event_generator->MoveTouch(gfx::Point(3, 3));
    event_generator->MoveTouch(gfx::Point(4, 4));
    event_generator->MoveTouch(gfx::Point(5, 5));
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify pressing the finger will show the laser pointer and add a point
    // but will not activate fading out.
    event_generator->PressTouch();
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
    EXPECT_FALSE(controller_test_api_->IsFadingAway());
    EXPECT_EQ(1, controller_test_api_->laser_points().GetNumberOfPoints());

    // Verify dragging the finger 2 times will add 2 more points.
    event_generator->MoveTouch(gfx::Point(6, 6));
    event_generator->MoveTouch(gfx::Point(7, 7));
    EXPECT_EQ(3, controller_test_api_->laser_points().GetNumberOfPoints());

    // Verify releasing the finger still shows the laser pointer, which is
    // fading away.
    event_generator->ReleaseTouch();
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
    EXPECT_TRUE(controller_test_api_->IsFadingAway());

    // Verify that disabling the mode does not display the laser pointer.
    controller_test_api_->SetEnabled(false);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
    EXPECT_FALSE(controller_test_api_->IsFadingAway());

    // Verify that disabling the mode while laser pointer is displayed does not
    // display the laser pointer.
    controller_test_api_->SetEnabled(true);
    event_generator->PressTouch();
    event_generator->MoveTouch(gfx::Point(6, 6));
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
    controller_test_api_->SetEnabled(false);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify that the laser pointer does not add points while disabled.
    event_generator->PressTouch();
    event_generator->MoveTouch(gfx::Point(8, 8));
    event_generator->ReleaseTouch();
    event_generator->MoveTouch(gfx::Point(9, 9));
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
  }

  void VerifyLaserPointerRendererMouseEvent() {
    ui::test::EventGenerator* event_generator = GetEventGenerator();

    // When disabled the laser pointer should not be showing.
    event_generator->MoveMouseTo(gfx::Point(1, 1));
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify that by enabling the mode, the laser pointer should still not be
    // showing.
    controller_test_api_->SetEnabled(true);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify moving the cursor 4 times will display the laser pointer.
    event_generator->MoveMouseTo(gfx::Point(2, 2));
    event_generator->MoveMouseTo(gfx::Point(3, 3));
    event_generator->MoveMouseTo(gfx::Point(4, 4));
    event_generator->MoveMouseTo(gfx::Point(5, 5));
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
    EXPECT_FALSE(controller_test_api_->IsFadingAway());
    EXPECT_EQ(4, controller_test_api_->laser_points().GetNumberOfPoints());

    // Verify moving the cursor 2 times will add 2 more points.
    event_generator->MoveMouseTo(gfx::Point(6, 6));
    event_generator->MoveMouseTo(gfx::Point(7, 7));
    EXPECT_EQ(6, controller_test_api_->laser_points().GetNumberOfPoints());

    // Verify that disabling the mode does not display the laser pointer.
    controller_test_api_->SetEnabled(false);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
    EXPECT_FALSE(controller_test_api_->IsFadingAway());

    // Verify that disabling the mode while laser pointer is displayed does not
    // display the laser pointer.
    controller_test_api_->SetEnabled(true);
    event_generator->MoveMouseTo(gfx::Point(6, 6));
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
    controller_test_api_->SetEnabled(false);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());

    // Verify that the laser pointer does not add points while disabled.
    event_generator->MoveMouseTo(gfx::Point(8, 8));
    event_generator->MoveMouseTo(gfx::Point(9, 9));
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
  }

  TestLaserPointerObserver* observer() { return observer_.get(); }

  std::unique_ptr<LaserPointerController> controller_;
  std::unique_ptr<LaserPointerControllerTestApi> controller_test_api_;
  std::unique_ptr<TestLaserPointerObserver> observer_;
};

}  // namespace

// Test to ensure the class responsible for drawing the laser pointer receives
// points from stylus movements as expected.
TEST_F(LaserPointerControllerTest, LaserPointerRenderer) {
  stylus_utils::SetHasStylusInputForTesting();
  ui::test::EventGenerator* event_generator = GetEventGenerator();
  event_generator->EnterPenPointerMode();
  VerifyLaserPointerRendererTouchEvent();

  // Verify that the laser pointer does not get shown if points are not coming
  // from the stylus, even when enabled.
  event_generator->ExitPenPointerMode();
  controller_test_api_->SetEnabled(true);
  event_generator->PressTouch();
  event_generator->MoveTouch(gfx::Point(10, 10));
  event_generator->MoveTouch(gfx::Point(11, 11));
  EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
  event_generator->ReleaseTouch();

  // Make sure that event can be sent after the pointer widget is destroyed
  // by release. This can happen if the pen event causes the deletion of
  // the pointer event in an earlier event handler.
  ui::PointerDetails pointer_details;
  pointer_details.pointer_type = ui::EventPointerType::kPen;

  ui::TouchEvent touch(ui::EventType::kTouchMoved, gfx::PointF(), gfx::PointF(),
                       base::TimeTicks(), pointer_details, 0);
  ui::Event::DispatcherApi api(&touch);
  api.set_target(Shell::GetPrimaryRootWindow());
  static_cast<ui::EventHandler*>(controller_.get())->OnTouchEvent(&touch);
}

// Test to ensure the class responsible for drawing the laser pointer receives
// points from mouse movements as expected.
TEST_F(LaserPointerControllerTest, LaserPointerRendererTouchEvent) {
  stylus_utils::SetNoStylusInputForTesting();
  VerifyLaserPointerRendererTouchEvent();

  // Make sure that event can be sent after the pointer widget is destroyed
  // by release. This can happen if the touch event causes the deletion of
  // the pointer event in an earlier event handler.
  ui::PointerDetails pointer_details;

  ui::TouchEvent touch(ui::EventType::kTouchMoved, gfx::PointF(), gfx::PointF(),
                       base::TimeTicks(), pointer_details, 0);
  ui::Event::DispatcherApi api(&touch);
  api.set_target(Shell::GetPrimaryRootWindow());
  static_cast<ui::EventHandler*>(controller_.get())->OnTouchEvent(&touch);
}

// Test to ensure the class responsible for drawing the laser pointer receives
// points from mouse movements as expected when stylus input is not available.
TEST_F(LaserPointerControllerTest, LaserPointerRendererMouseEventNoStylus) {
  stylus_utils::SetNoStylusInputForTesting();

  VerifyLaserPointerRendererMouseEvent();
}

// Test to ensure the class responsible for drawing the laser pointer receives
// points from mouse movements as expected when stylus input is available but
// hasn't been seen before.
TEST_F(LaserPointerControllerTest, LaserPointerRendererMouseEventHasStylus) {
  stylus_utils::SetHasStylusInputForTesting();

  VerifyLaserPointerRendererMouseEvent();

  // Verify that the laser pointer does not get shown if points are coming from
  // mouse event if a stylus interaction has been seen.
  ui::test::EventGenerator* event_generator = GetEventGenerator();
  event_generator->EnterPenPointerMode();
  event_generator->PressTouch();
  event_generator->MoveMouseTo(gfx::Point(2, 2));
  event_generator->MoveMouseTo(gfx::Point(3, 3));
  event_generator->MoveMouseTo(gfx::Point(4, 4));
  event_generator->MoveMouseTo(gfx::Point(5, 5));
  EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
}

// Test to ensure the class responsible for drawing the laser pointer handles
// prediction as expected when it receives points from stylus movements.
TEST_F(LaserPointerControllerTest, LaserPointerPrediction) {
  controller_test_api_->SetEnabled(true);
  // The laser pointer mode only works with stylus.
  ui::test::EventGenerator* event_generator = GetEventGenerator();
  event_generator->EnterPenPointerMode();
  event_generator->PressTouch();
  EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());

  EXPECT_EQ(1, controller_test_api_->laser_points().GetNumberOfPoints());
  // Initial press event shouldn't generate any predicted points as there's no
  // history to use for prediction.
  EXPECT_EQ(0,
            controller_test_api_->predicted_laser_points().GetNumberOfPoints());

  // Verify dragging the stylus 3 times will add some predicted points.
  event_generator->MoveTouch(gfx::Point(10, 10));
  event_generator->MoveTouch(gfx::Point(20, 20));
  event_generator->MoveTouch(gfx::Point(30, 30));
  EXPECT_NE(0,
            controller_test_api_->predicted_laser_points().GetNumberOfPoints());
  // Verify predicted points are in the right direction.
  for (const auto& point :
       controller_test_api_->predicted_laser_points().points()) {
    EXPECT_LT(30, point.location.x());
    EXPECT_LT(30, point.location.y());
  }

  // Verify releasing the stylus removes predicted points.
  event_generator->ReleaseTouch();
  EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer());
  EXPECT_TRUE(controller_test_api_->IsFadingAway());
  EXPECT_EQ(0,
            controller_test_api_->predicted_laser_points().GetNumberOfPoints());
}

TEST_F(LaserPointerControllerTest, NotifyLaserPointerStateChanged) {
  controller_->AddObserver(observer());
  controller_test_api_->SetEnabled(true);
  EXPECT_TRUE(observer()->laser_pointer_enabled());
  controller_test_api_->SetEnabled(false);
  EXPECT_FALSE(observer()->laser_pointer_enabled());
  controller_->RemoveObserver(observer());
}

// Test to ensure the class responsible for update cursor visibility state when
// it handles mouse and touch events.
TEST_F(LaserPointerControllerTest, MouseCursorState) {
  ash::stylus_utils::SetNoStylusInputForTesting();

  ui::test::EventGenerator* event_generator = GetEventGenerator();
  auto* cursor_manager = Shell::Get()->cursor_manager();

  // Verify that when disabled the cursor should be visible.
  event_generator->MoveMouseTo(gfx::Point(1, 1));
  EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
  EXPECT_TRUE(cursor_manager->IsCursorVisible());

  // Verify that by enabling the mode, mouse cursor should be hidden.
  controller_test_api_->SetEnabled(true);
  event_generator->MoveMouseTo(gfx::Point(2, 2));
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsCursorLocked());

  // Verify that after moving with touch, mouse cursor should be still hidden
  // but unlocked.
  event_generator->PressTouch();
  event_generator->MoveTouch(gfx::Point(2, 2));
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsCursorLocked());
  EXPECT_EQ(1, controller_test_api_->laser_points().GetNumberOfPoints());

  // Verify that moving the mouse cursor shows the cursor.
  event_generator->MoveMouseTo(gfx::Point(6, 6));
  EXPECT_FALSE(cursor_manager->IsCursorVisible());
  EXPECT_TRUE(cursor_manager->IsCursorLocked());
  EXPECT_EQ(2, controller_test_api_->laser_points().GetNumberOfPoints());

  // Verify that by disabling the mode, mouse cursor should be visible.
  controller_test_api_->SetEnabled(false);
  EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer());
  event_generator->MoveMouseTo(gfx::Point(7, 7));
  EXPECT_TRUE(cursor_manager->IsCursorVisible());
  EXPECT_FALSE(cursor_manager->IsCursorLocked());
}

// Base class for tests that rely on palette.
class LaserPointerControllerTestWithPalette
    : public LaserPointerControllerTest {
 public:
  LaserPointerControllerTestWithPalette() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kAshEnablePaletteOnAllDisplays);
    stylus_utils::SetHasStylusInputForTesting();
  }
  LaserPointerControllerTestWithPalette(
      const LaserPointerControllerTestWithPalette&) = delete;
  LaserPointerControllerTestWithPalette& operator=(
      const LaserPointerControllerTestWithPalette&) = delete;
  ~LaserPointerControllerTestWithPalette() override = default;
};

// Verify that clicking a palette with the laser pointer does not
// cause the laser to show.
TEST_F(LaserPointerControllerTestWithPalette, LaserPointerPaletteDisable) {
  // Make the two displays different size to catch root coordinates
  // being used as screen coordinates.
  UpdateDisplay("800x600,1024x768");

  std::vector<display::Display> testcases{
      GetPrimaryDisplay(),
      GetSecondaryDisplay(),
  };

  for (std::size_t i = 0; i < testcases.size(); i++) {
    display::Display display = testcases[i];
    PaletteTray* palette =
        controller_test_api_->GetPaletteTrayOnDisplay(display.id());
    ASSERT_TRUE(palette) << "While processing testcase " << i;

    // The laser pointer mode only works with stylus.
    ui::test::EventGenerator* event_generator = GetEventGenerator();
    event_generator->EnterPenPointerMode();

    // Palette does not appear until a stylus is seen for the first time.
    event_generator->PressMoveAndReleaseTouchTo(display.bounds().CenterPoint());
    gfx::Point center = palette->GetBoundsInScreen().CenterPoint();

    // Tap the laser pointer both on and off of the palette.
    controller_test_api_->SetEnabled(true);
    event_generator->PressTouch(center);
    EXPECT_FALSE(controller_test_api_->IsShowingLaserPointer())
        << "While processing testcase " << i;
    event_generator->ReleaseTouch();
    event_generator->PressTouch(display.bounds().CenterPoint());
    EXPECT_TRUE(controller_test_api_->IsShowingLaserPointer())
        << "While processing testcase " << i;
    event_generator->ReleaseTouch();
  }
}

}  // namespace ash