File: keyboard_test_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (6)
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
// Copyright 2017 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/keyboard/ui/test/keyboard_test_util.h"

#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/public/cpp/keyboard/keyboard_controller_observer.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "ui/display/screen.h"

namespace keyboard {

namespace {

class KeyboardVisibilityChangeWaiter : public ash::KeyboardControllerObserver {
 public:
  explicit KeyboardVisibilityChangeWaiter(bool wait_until)
      : wait_until_(wait_until) {
    KeyboardUIController::Get()->AddObserver(this);
  }

  KeyboardVisibilityChangeWaiter(const KeyboardVisibilityChangeWaiter&) =
      delete;
  KeyboardVisibilityChangeWaiter& operator=(
      const KeyboardVisibilityChangeWaiter&) = delete;

  ~KeyboardVisibilityChangeWaiter() override {
    KeyboardUIController::Get()->RemoveObserver(this);
  }

  void Wait() { run_loop_.Run(); }

 private:
  void OnKeyboardVisibilityChanged(const bool is_visible) override {
    if (is_visible == wait_until_)
      run_loop_.QuitWhenIdle();
  }

  base::RunLoop run_loop_;
  const bool wait_until_;
};

bool WaitVisibilityChangesTo(bool wait_until) {
  if (KeyboardUIController::Get()->IsKeyboardVisible() == wait_until)
    return true;
  KeyboardVisibilityChangeWaiter waiter(wait_until);
  waiter.Wait();
  return true;
}

}  // namespace

namespace test {

bool WaitUntilLoaded() {
  // In tests, the keyboard window is mocked out so it usually "loads" within a
  // single RunUntilIdle call.
  base::RunLoop run_loop;
  while (KeyboardUIController::Get()->GetStateForTest() ==
         KeyboardUIState::kLoading) {
    run_loop.RunUntilIdle();
  }
  return true;
}

bool WaitUntilShown() {
  // KeyboardController send a visibility update once the show animation
  // finishes.
  return WaitVisibilityChangesTo(true /* wait_until */);
}

bool WaitUntilHidden() {
  // Unlike |WaitUntilShown|, KeyboardController updates its visibility
  // at the beginning of the hide animation. There's currently no way to
  // actually detect when the hide animation finishes.
  // TODO(crbug.com/41392988): Find a proper solution to this.
  return WaitVisibilityChangesTo(false /* wait_until */);
}

bool IsKeyboardShowing() {
  auto* keyboard_controller = KeyboardUIController::Get();
  DCHECK(keyboard_controller->IsEnabled());

  // KeyboardController sets its state to SHOWN when it is about to show.
  return keyboard_controller->GetStateForTest() == KeyboardUIState::kShown;
}

bool IsKeyboardHiding() {
  auto* keyboard_controller = KeyboardUIController::Get();
  DCHECK(keyboard_controller->IsEnabled());

  return keyboard_controller->GetStateForTest() == KeyboardUIState::kWillHide ||
         keyboard_controller->GetStateForTest() == KeyboardUIState::kHidden;
}

gfx::Rect KeyboardBoundsFromRootBounds(const gfx::Rect& root_bounds,
                                       int keyboard_height) {
  return gfx::Rect(root_bounds.x(), root_bounds.bottom() - keyboard_height,
                   root_bounds.width(), keyboard_height);
}

}  // namespace test

}  // namespace keyboard