File: atk_util_auralinux_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <atk/atk.h>

#include <string>

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/platform/atk_util_auralinux.h"
#include "ui/accessibility/platform/ax_platform_for_test.h"
#include "ui/accessibility/platform/ax_platform_node_auralinux.h"
#include "ui/accessibility/platform/ax_platform_node_unittest.h"
#include "ui/accessibility/platform/test_ax_node_wrapper.h"

namespace ui {

class AtkUtilAuraLinuxTest : public AXPlatformNodeTest {
 public:
  AtkUtilAuraLinuxTest() : ax_mode_setter_(kAXModeComplete) {
    // We need to create a platform node in order to install it as the root
    // ATK node. The ATK bridge will complain if we try to use it without a
    // root node installed.
    AXNodeData root;
    root.id = 1;
    Init(root);

    TestAXNodeWrapper* wrapper =
        TestAXNodeWrapper::GetOrCreate(GetTree(), GetRoot());
    if (!wrapper) {
      NOTREACHED();
    }
    AXPlatformNodeAuraLinux::SetApplication(wrapper->ax_platform_node());

    AtkUtilAuraLinux::GetInstance()->InitializeForTesting();
  }

  ~AtkUtilAuraLinuxTest() override = default;

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

  void TearDown() override {
    TestAXNodeWrapper* wrapper =
        TestAXNodeWrapper::GetOrCreate(GetTree(), GetRoot());
    if (!wrapper) {
      NOTREACHED();
    }
    g_object_unref(wrapper->ax_platform_node()->GetNativeViewAccessible());

    AXPlatformNodeTest::TearDown();
    // These tests set AtSpiReady to true. Reset to initial state.
    AtkUtilAuraLinux::GetInstance()->SetAtSpiReady(false);
  }

 private:
  ScopedAXModeSetter ax_mode_setter_;
};

TEST_F(AtkUtilAuraLinuxTest, KeySnooping) {
  AtkKeySnoopFunc key_snoop_func = [](AtkKeyEventStruct* key_event,
                                      void* keyval_seen) -> int {
    *static_cast<int*>(keyval_seen) = key_event->keyval;
    return FALSE;
  };

  int keyval_seen = 0;
  guint listener_id = atk_add_key_event_listener(key_snoop_func, &keyval_seen);

  AtkKeyEventStruct atk_key_event;
  atk_key_event.type = ATK_KEY_EVENT_PRESS;
  atk_key_event.state = 0;
  atk_key_event.keyval = 55;
  atk_key_event.keycode = 10;
  atk_key_event.timestamp = 10;
  atk_key_event.string = nullptr;
  atk_key_event.length = 0;

  AtkUtilAuraLinux* atk_util = AtkUtilAuraLinux::GetInstance();
  atk_util->HandleAtkKeyEvent(&atk_key_event);
  // AX mode is enabled and Key snooping works.
  EXPECT_EQ(keyval_seen, 55);

  TestAXNodeWrapper* wrapper =
      TestAXNodeWrapper::GetOrCreate(GetTree(), GetRoot());
  DCHECK(wrapper);
  {
    ScopedAXModeSetter disable_accessibility(AXMode::kNone);
    keyval_seen = 0;
    atk_util->HandleAtkKeyEvent(&atk_key_event);
    // When AX mode is not enabled, Key snooping doesn't work.
    EXPECT_EQ(keyval_seen, 0);
  }  // Restores the previous AX mode.
  keyval_seen = 0;
  atk_util->HandleAtkKeyEvent(&atk_key_event);
  // AX mode is set again, Key snooping works.
  EXPECT_EQ(keyval_seen, 55);

  atk_remove_key_event_listener(listener_id);

  keyval_seen = 0;
  atk_util->HandleAtkKeyEvent(&atk_key_event);

  EXPECT_EQ(keyval_seen, 0);
}

TEST_F(AtkUtilAuraLinuxTest, AtSpiReady) {
  AtkUtilAuraLinux* atk_util = AtkUtilAuraLinux::GetInstance();

  EXPECT_FALSE(atk_util->IsAtSpiReady());

  // In a normal browser execution, when a key event listener is added it means
  // the AT-SPI bridge has done it as part of its initialization, so it is set
  // as enabled.
  AtkKeySnoopFunc key_snoop_func = [](AtkKeyEventStruct* key_event,
                                      void* user_data) -> int { return FALSE; };
  atk_add_key_event_listener(key_snoop_func, nullptr);

  EXPECT_TRUE(atk_util->IsAtSpiReady());
}

}  // namespace ui