File: menu_test_base.h

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 (104 lines) | stat: -rw-r--r-- 3,365 bytes parent folder | download | duplicates (3)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_
#define CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_

#include <array>
#include <memory>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/views/test/view_event_test_base.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/accessibility/ax_update_observer.h"
#include "ui/views/controls/menu/menu_delegate.h"

namespace views {
class MenuItemView;
class MenuRunner;
}  // namespace views

// This is a convenience base class for menu related tests to provide some
// common functionality.
//
// Subclasses should implement:
//  BuildMenu()            populate the menu
//  DoTestWithMenuOpen()   initiate the test
//
// Subclasses can call:
//  Click()       to post a mouse click on a View
//  KeyPress()    to post a key press
//
// Although it should be possible to post a menu multiple times,
// MenuItemView prevents repeated activation of a menu by clicks too
// close in time.
class MenuTestBase : public ViewEventTestBase,
                     public views::AXUpdateObserver,
                     public views::MenuDelegate {
 public:
  MenuTestBase();

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

  ~MenuTestBase() override;

  // AXUpdateObserver overrides.
  void OnViewEvent(views::View*, ax::mojom::Event event_type) override;

  // Generate a mouse click and run |next| once the event has been processed.
  virtual void Click(views::View* view, base::OnceClosure next);

  // Generate a keypress and run |next| once the event has been processed.
  void KeyPress(ui::KeyboardCode keycode, base::OnceClosure next);

  views::MenuItemView* menu() { return menu_; }

  int last_command() const { return last_command_; }

 protected:
  views::MenuRunner* menu_runner() { return menu_runner_.get(); }

  // Called to populate the menu.
  virtual void BuildMenu(views::MenuItemView* menu) = 0;

  // Called once the menu is open.
  virtual void DoTestWithMenuOpen() = 0;

  // Returns a bitmask of flags to use when creating the |menu_runner_|. By
  // default, this is only views::MenuRunner::HAS_MNEMONICS.
  virtual int GetMenuRunnerFlags();

  // ViewEventTestBase implementation.
  void SetUp() override;
  void TearDown() override;
  std::unique_ptr<views::View> CreateContentsView() override;
  void DoTestOnMessageLoop() override;
  gfx::Size GetPreferredSizeForContents() const override;

  // views::MenuDelegate implementation
  void ExecuteCommand(int id) override;

  int GetAXEventCount(ax::mojom::Event event_type) const;

 private:
  void ButtonPressed();

  raw_ptr<views::MenuButton> button_ = nullptr;
  std::unique_ptr<views::MenuRunner> menu_runner_;
  // Owned by `menu_runner_`.
  raw_ptr<views::MenuItemView> menu_ = nullptr;

  // The command id of the last pressed menu item since the menu was opened.
  int last_command_;

  // The number of AX events fired by type.
  static constexpr int kNumEvents =
      static_cast<size_t>(ax::mojom::Event::kMaxValue) + 1;
  std::array<int, kNumEvents> ax_event_counts_;
};

#endif  // CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_