summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/platform/ax_platform_node_textchildprovider_win_unittest.cc
blob: 4e0c8742d09fa20d98b1f935634133aa840675b2 (plain)
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
// Copyright 2019 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 "ui/accessibility/platform/ax_platform_node_win_unittest.h"

#include "base/win/scoped_bstr.h"
#include "ui/accessibility/platform/ax_fragment_root_win.h"
#include "ui/accessibility/platform/ax_platform_node_textchildprovider_win.h"
#include "ui/accessibility/platform/ax_platform_node_textprovider_win.h"
#include "ui/accessibility/platform/ax_platform_node_textrangeprovider_win.h"

using Microsoft::WRL::ComPtr;

namespace ui {

class AXPlatformNodeTextChildProviderTest : public AXPlatformNodeWinTest {
 protected:
  // Construct an accessibility tree for testing ITextChildProvider resolution
  // from various positions in the tree. The following tree configuration
  // is constructed:
  //
  // root_________________________________________________
  // |                                                    |
  // nontext_child_of_root______                          text_child_of_root
  // |                          |                         |
  // nontext_child_of_nontext   text_child_of_nontext     text_child_of_text
  //
  // nontext leaf elements are considered as embedded objects and expose a
  // character to allow the text pattern navigation to work with them too.
  // Because of that, a nontext leaf element is treated as a text element.
  void SetUp() override {
    ui::AXNodeData root;
    root.id = 1;
    root.role = ax::mojom::Role::kRootWebArea;

    ui::AXNodeData nontext_child_of_root;
    nontext_child_of_root.id = 2;
    nontext_child_of_root.role = ax::mojom::Role::kGroup;
    nontext_child_of_root.SetName("non text child of root.");
    root.child_ids.push_back(nontext_child_of_root.id);

    ui::AXNodeData text_child_of_root;
    text_child_of_root.id = 3;
    text_child_of_root.role = ax::mojom::Role::kStaticText;
    text_child_of_root.SetName("text child of root.");
    root.child_ids.push_back(text_child_of_root.id);

    ui::AXNodeData nontext_child_of_nontext;
    nontext_child_of_nontext.id = 4;
    nontext_child_of_nontext.role = ax::mojom::Role::kGroup;
    nontext_child_of_nontext.SetName("nontext child of nontext.");
    nontext_child_of_root.child_ids.push_back(nontext_child_of_nontext.id);

    ui::AXNodeData text_child_of_nontext;
    text_child_of_nontext.id = 5;
    text_child_of_nontext.role = ax::mojom::Role::kStaticText;
    text_child_of_nontext.SetName("text child of nontext.");
    nontext_child_of_root.child_ids.push_back(text_child_of_nontext.id);

    ui::AXNodeData text_child_of_text;
    text_child_of_text.id = 6;
    text_child_of_text.role = ax::mojom::Role::kInlineTextBox;
    text_child_of_text.SetName("text child of text.");
    text_child_of_root.child_ids.push_back(text_child_of_text.id);

    ui::AXTreeUpdate update;
    ui::AXTreeData tree_data;
    tree_data.tree_id = ui::AXTreeID::CreateNewAXTreeID();
    update.tree_data = tree_data;
    update.has_tree_data = true;
    update.root_id = root.id;
    update.nodes = {root,
                    nontext_child_of_root,
                    text_child_of_root,
                    nontext_child_of_nontext,
                    text_child_of_nontext,
                    text_child_of_text};

    Init(update);

    AXNode* root_node = GetRootAsAXNode();
    AXNode* nontext_child_of_root_node = root_node->children()[0];
    AXNode* text_child_of_root_node = root_node->children()[1];
    AXNode* nontext_child_of_nontext_node =
        nontext_child_of_root_node->children()[0];
    AXNode* text_child_of_nontext_node =
        nontext_child_of_root_node->children()[1];
    AXNode* text_child_of_text_node = text_child_of_root_node->children()[0];

    InitITextChildProvider(root_node, root_provider_raw_,
                           root_text_child_provider_);
    InitITextChildProvider(nontext_child_of_root_node,
                           nontext_child_of_root_provider_raw_,
                           nontext_child_of_root_text_child_provider_);
    InitITextChildProvider(text_child_of_root_node,
                           text_child_of_root_text_provider_raw_,
                           text_child_of_root_text_child_provider_);
    InitITextChildProvider(nontext_child_of_nontext_node,
                           nontext_child_of_nontext_text_provider_raw_,
                           nontext_child_of_nontext_text_child_provider_);
    InitITextChildProvider(text_child_of_nontext_node,
                           text_child_of_nontext_text_provider_raw_,
                           text_child_of_nontext_text_child_provider_);
    InitITextChildProvider(text_child_of_text_node,
                           text_child_of_text_text_provider_raw_,
                           text_child_of_text_text_child_provider_);
  }

  void InitITextChildProvider(
      AXNode* node,
      ComPtr<IRawElementProviderSimple>& raw_element_provider,
      ComPtr<ITextChildProvider>& text_child_provider) {
    raw_element_provider =
        QueryInterfaceFromNode<IRawElementProviderSimple>(node);

    EXPECT_HRESULT_SUCCEEDED(raw_element_provider->GetPatternProvider(
        UIA_TextChildPatternId, &text_child_provider));

    // If the element does not support ITextChildProvider, create one anyways
    // for testing purposes.
    if (!text_child_provider) {
      ui::AXPlatformNodeWin* platform_node =
          (ui::AXPlatformNodeWin*)raw_element_provider.Get();

      ComPtr<ITextChildProvider> new_child_provider =
          ui::AXPlatformNodeTextChildProviderWin::Create(platform_node);
      new_child_provider->QueryInterface(IID_PPV_ARGS(&text_child_provider));
    }
  }

  ComPtr<IRawElementProviderSimple> root_provider_raw_;
  ComPtr<IRawElementProviderSimple> nontext_child_of_root_provider_raw_;
  ComPtr<IRawElementProviderSimple> text_child_of_root_text_provider_raw_;
  ComPtr<IRawElementProviderSimple> nontext_child_of_nontext_text_provider_raw_;
  ComPtr<IRawElementProviderSimple> text_child_of_nontext_text_provider_raw_;
  ComPtr<IRawElementProviderSimple> text_child_of_text_text_provider_raw_;

  ComPtr<ITextChildProvider> root_text_child_provider_;
  ComPtr<ITextChildProvider> nontext_child_of_root_text_child_provider_;
  ComPtr<ITextChildProvider> text_child_of_root_text_child_provider_;
  ComPtr<ITextChildProvider> nontext_child_of_nontext_text_child_provider_;
  ComPtr<ITextChildProvider> text_child_of_nontext_text_child_provider_;
  ComPtr<ITextChildProvider> text_child_of_text_text_child_provider_;
};

// ITextChildProvider::TextContainer Tests
//
// For each possible position in the tree verify:
// 1) A text container can/cannot be retrieved if an ancestor does/doesn't
//    support the UIA Text control pattern.
// 2) Any retrieved text container is the nearest ancestor text container.
// 3) A Text control can in fact be retrieved from any retrieved text
//    container.

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromRoot) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      root_text_child_provider_->get_TextContainer(&text_container));
  ASSERT_EQ(nullptr, text_container.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromNontextChildOfRoot) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      nontext_child_of_root_text_child_provider_->get_TextContainer(
          &text_container));
  ASSERT_NE(nullptr, text_container.Get());

  EXPECT_EQ(root_provider_raw_.Get(), text_container.Get());

  ComPtr<IUnknown> pattern_provider;
  ComPtr<ITextProvider> text_container_text_provider;
  text_container->GetPatternProvider(UIA_TextPatternId, &pattern_provider);
  ASSERT_NE(nullptr, pattern_provider.Get());
  EXPECT_HRESULT_SUCCEEDED(pattern_provider.As(&text_container_text_provider));
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromTextChildOfRoot) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_root_text_child_provider_->get_TextContainer(
          &text_container));
  ASSERT_NE(nullptr, text_container.Get());

  EXPECT_EQ(root_provider_raw_.Get(), text_container.Get());

  ComPtr<IUnknown> pattern_provider;
  ComPtr<ITextProvider> text_container_text_provider;
  text_container->GetPatternProvider(UIA_TextPatternId, &pattern_provider);
  ASSERT_NE(nullptr, pattern_provider.Get());
  EXPECT_HRESULT_SUCCEEDED(pattern_provider.As(&text_container_text_provider));
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromNontextChildOfNontext) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      nontext_child_of_nontext_text_child_provider_->get_TextContainer(
          &text_container));
  ASSERT_NE(nullptr, text_container.Get());

  EXPECT_EQ(root_provider_raw_.Get(), text_container.Get());

  ComPtr<IUnknown> pattern_provider;
  ComPtr<ITextProvider> text_container_text_provider;
  text_container->GetPatternProvider(UIA_TextPatternId, &pattern_provider);
  ASSERT_NE(nullptr, pattern_provider.Get());
  EXPECT_HRESULT_SUCCEEDED(pattern_provider.As(&text_container_text_provider));
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromTextChildOfNontext) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_nontext_text_child_provider_->get_TextContainer(
          &text_container));
  ASSERT_NE(nullptr, text_container.Get());

  EXPECT_EQ(root_provider_raw_.Get(), text_container.Get());

  ComPtr<IUnknown> pattern_provider;
  ComPtr<ITextProvider> text_container_text_provider;
  text_container->GetPatternProvider(UIA_TextPatternId, &pattern_provider);
  ASSERT_NE(nullptr, pattern_provider.Get());
  EXPECT_HRESULT_SUCCEEDED(pattern_provider.As(&text_container_text_provider));
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextContainerFromTextChildOfText) {
  ComPtr<IRawElementProviderSimple> text_container;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_text_text_child_provider_->get_TextContainer(
          &text_container));
  ASSERT_NE(nullptr, text_container.Get());

  EXPECT_EQ(text_child_of_root_text_provider_raw_.Get(), text_container.Get());

  ComPtr<IUnknown> pattern_provider;
  ComPtr<ITextProvider> text_container_text_provider;
  text_container->GetPatternProvider(UIA_TextPatternId, &pattern_provider);
  ASSERT_NE(nullptr, pattern_provider.Get());
  EXPECT_HRESULT_SUCCEEDED(pattern_provider.As(&text_container_text_provider));
}

// ITextChildProvider::TextRange Tests
//
// For each possible position in the tree verify:
// 1) A text range can/cannot be retrieved if an ancestor does/doesn't
//    support the UIA Text control pattern.
// 2) Any retrieved text range encloses the child element.
TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromRoot) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      root_text_child_provider_->get_TextRange(&text_range_provider));
  EXPECT_EQ(nullptr, text_range_provider.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromNontextChildOfRoot) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      nontext_child_of_root_text_child_provider_->get_TextRange(
          &text_range_provider));
  ASSERT_NE(nullptr, text_range_provider.Get());

  base::win::ScopedBstr text_content;
  EXPECT_HRESULT_SUCCEEDED(
      text_range_provider->GetText(-1, text_content.Receive()));
  EXPECT_EQ(base::WideToUTF16(text_content.Get()),
            kEmbeddedCharacterAsString + u"text child of nontext.");

  ComPtr<IRawElementProviderSimple> enclosing_element;
  text_range_provider->GetEnclosingElement(&enclosing_element);
  EXPECT_EQ(nontext_child_of_root_provider_raw_.Get(), enclosing_element.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromTextChildOfRoot) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_root_text_child_provider_->get_TextRange(
          &text_range_provider));
  ASSERT_NE(nullptr, text_range_provider.Get());

  base::win::ScopedBstr text_content;
  EXPECT_HRESULT_SUCCEEDED(
      text_range_provider->GetText(-1, text_content.Receive()));
  EXPECT_EQ(0, wcscmp(text_content.Get(), L"text child of text."));

  ComPtr<IRawElementProviderSimple> enclosing_element;
  text_range_provider->GetEnclosingElement(&enclosing_element);
  EXPECT_EQ(text_child_of_root_text_provider_raw_.Get(),
            enclosing_element.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromNontextChildOfNontext) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      nontext_child_of_nontext_text_child_provider_->get_TextRange(
          &text_range_provider));
  ASSERT_NE(nullptr, text_range_provider.Get());

  base::win::ScopedBstr text_content;
  EXPECT_HRESULT_SUCCEEDED(
      text_range_provider->GetText(-1, text_content.Receive()));
  EXPECT_EQ(base::WideToUTF16(text_content.Get()), kEmbeddedCharacterAsString);

  ComPtr<IRawElementProviderSimple> enclosing_element;
  text_range_provider->GetEnclosingElement(&enclosing_element);
  EXPECT_EQ(nontext_child_of_nontext_text_provider_raw_.Get(),
            enclosing_element.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromTextChildOfNontext) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_nontext_text_child_provider_->get_TextRange(
          &text_range_provider));
  ASSERT_NE(nullptr, text_range_provider.Get());

  base::win::ScopedBstr text_content;
  EXPECT_HRESULT_SUCCEEDED(
      text_range_provider->GetText(-1, text_content.Receive()));
  EXPECT_EQ(0, wcscmp(text_content.Get(), L"text child of nontext."));

  ComPtr<IRawElementProviderSimple> enclosing_element;
  text_range_provider->GetEnclosingElement(&enclosing_element);
  EXPECT_EQ(text_child_of_nontext_text_provider_raw_.Get(),
            enclosing_element.Get());
}

TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderTextRangeFromTextChildOfText) {
  ComPtr<ITextRangeProvider> text_range_provider;
  EXPECT_HRESULT_SUCCEEDED(
      text_child_of_text_text_child_provider_->get_TextRange(
          &text_range_provider));
  ASSERT_NE(nullptr, text_range_provider.Get());

  base::win::ScopedBstr text_content;
  EXPECT_HRESULT_SUCCEEDED(
      text_range_provider->GetText(-1, text_content.Receive()));
  EXPECT_EQ(0, wcscmp(text_content.Get(), L"text child of text."));

  ComPtr<IRawElementProviderSimple> enclosing_element;
  text_range_provider->GetEnclosingElement(&enclosing_element);
  EXPECT_EQ(text_child_of_root_text_provider_raw_.Get(),
            enclosing_element.Get());
}

// ITextChildProvider Tests - Inactive AX Tree
//
// Test that both ITextChildProvider::GetTextContainer and
// ITextChildProvider::GetTextContainer fail under an inactive AX tree.
TEST_F(AXPlatformNodeTextChildProviderTest,
       ITextChildProviderInactiveAccessibilityTree) {
  DestroyTree();

  // Test that GetTextContainer fails under an inactive tree.
  ComPtr<IRawElementProviderSimple> text_container;
  HRESULT hr = nontext_child_of_root_text_child_provider_->get_TextContainer(
      &text_container);
  EXPECT_EQ(static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE), hr);

  // Test that GetTextRange fails under an inactive tree.
  ComPtr<ITextRangeProvider> text_range_provider;
  hr = nontext_child_of_root_text_child_provider_->get_TextRange(
      &text_range_provider);
  EXPECT_EQ(static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE), hr);
}

}  // namespace ui