File: quit_instruction_bubble_controller_unittest.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (144 lines) | stat: -rw-r--r-- 4,615 bytes parent folder | download
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
// Copyright 2018 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 "chrome/browser/ui/views/quit_instruction_bubble_controller.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/macros.h"
#include "base/timer/mock_timer.h"
#include "chrome/browser/ui/views/quit_instruction_bubble.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/slide_animation.h"

class TestQuitInstructionBubble : public QuitInstructionBubbleBase {
 public:
  TestQuitInstructionBubble() {}
  ~TestQuitInstructionBubble() override {}

  void Show() override {}
  void Hide() override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestQuitInstructionBubble);
};

class TestQuitInstructionBubbleController
    : public QuitInstructionBubbleController {
 public:
  TestQuitInstructionBubbleController(
      std::unique_ptr<QuitInstructionBubbleBase> bubble,
      std::unique_ptr<base::OneShotTimer> hide_timer,
      std::unique_ptr<gfx::SlideAnimation> animation)
      : QuitInstructionBubbleController(std::move(bubble),
                                        std::move(hide_timer)) {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestQuitInstructionBubbleController);
};

class TestSlideAnimation : public gfx::SlideAnimation {
 public:
  TestSlideAnimation() : gfx::SlideAnimation(nullptr) {}
  ~TestSlideAnimation() override {}

  void Reset() override {}
  void Reset(double value) override {}
  void Show() override {}
  void Hide() override {}
  void SetSlideDuration(int duration) override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestSlideAnimation);
};

class QuitInstructionBubbleControllerTest : public testing::Test {
 protected:
  void SetUp() override {
    std::unique_ptr<TestQuitInstructionBubble> bubble =
        std::make_unique<TestQuitInstructionBubble>();
    auto timer = std::make_unique<base::MockOneShotTimer>();
    bubble_ = bubble.get();
    timer_ = timer.get();
    controller_.reset(new TestQuitInstructionBubbleController(
        std::move(bubble), std::move(timer),
        std::make_unique<TestSlideAnimation>()));
  }

  void TearDown() override { controller_.reset(); }

  void SendKeyEvent(ui::KeyEvent* event) { controller_->OnKeyEvent(event); }

  void SendAccelerator(bool quit, bool press, bool repeat) {
    ui::KeyboardCode key = quit ? ui::VKEY_Q : ui::VKEY_P;
    int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
    if (repeat)
      modifiers |= ui::EF_IS_REPEAT;
    ui::EventType type = press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
    ui::KeyEvent event(type, key, modifiers);
    SendKeyEvent(&event);
  }

  void PressQuitAccelerator() { SendAccelerator(true, true, false); }

  void ReleaseQuitAccelerator() { SendAccelerator(true, false, false); }

  void RepeatQuitAccelerator() { SendAccelerator(true, true, true); }

  void PressOtherAccelerator() { SendAccelerator(false, true, false); }

  void ReleaseOtherAccelerator() { SendAccelerator(false, false, false); }

  std::unique_ptr<TestQuitInstructionBubbleController> controller_;

  // Owned by |controller_|.
  TestQuitInstructionBubble* bubble_;

  // Owned by |controller_|.
  base::MockOneShotTimer* timer_;
};

// Pressing the shortcut should show the bubble.
TEST_F(QuitInstructionBubbleControllerTest, PressAndHold) {
  PressQuitAccelerator();
  EXPECT_TRUE(timer_->IsRunning());
  ReleaseQuitAccelerator();
  EXPECT_TRUE(timer_->IsRunning());
}

// Repeated presses should keep showing the bubble.
TEST_F(QuitInstructionBubbleControllerTest, RepeatedPresses) {
  PressQuitAccelerator();
  RepeatQuitAccelerator();
  ReleaseQuitAccelerator();
  EXPECT_TRUE(timer_->IsRunning());
}

// Other keys shouldn't matter.
TEST_F(QuitInstructionBubbleControllerTest, OtherKeyPress) {
  PressQuitAccelerator();
  ReleaseQuitAccelerator();
  PressOtherAccelerator();
  ReleaseOtherAccelerator();
  EXPECT_TRUE(timer_->IsRunning());
  PressQuitAccelerator();
  EXPECT_TRUE(timer_->IsRunning());
}

// The controller should not consume keyup events on the 'Q' key
// (https://crbug.com/856868).
TEST_F(QuitInstructionBubbleControllerTest, ControllerDoesNotHandleQKeyUp) {
  ui::KeyEvent press_event(ui::ET_KEY_PRESSED, ui::VKEY_Q, 0);
  SendKeyEvent(&press_event);
  EXPECT_FALSE(press_event.handled());

  ui::KeyEvent release_event(ui::ET_KEY_RELEASED, ui::VKEY_Q, 0);
  SendKeyEvent(&release_event);
  EXPECT_FALSE(release_event.handled());
}