summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/accessibility/testing/accessibility_test.cc
blob: ac7e538321697bfc232a669b319946c4b6a2d38a (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
// Copyright 2017 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 "third_party/blink/renderer/modules/accessibility/testing/accessibility_test.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"

namespace blink {

void AccessibilityTest::SetUp() {
  RenderingTest::SetUp();
  DCHECK(GetDocument().GetSettings());
  GetDocument().GetSettings()->SetAccessibilityEnabled(true);
}

void AccessibilityTest::TearDown() {
  GetDocument().ClearAXObjectCache();
  DCHECK(GetDocument().GetSettings());
  GetDocument().GetSettings()->SetAccessibilityEnabled(false);
  RenderingTest::TearDown();
}

AXObjectCacheImpl& AccessibilityTest::GetAXObjectCache() const {
  auto* ax_object_cache =
      ToAXObjectCacheImpl(GetDocument().GetOrCreateAXObjectCache());
  DCHECK(ax_object_cache);
  return *ax_object_cache;
}

AXObject* AccessibilityTest::GetAXRootObject() const {
  return GetAXObjectCache().GetOrCreate(&GetLayoutView());
}

AXObject* AccessibilityTest::GetAXFocusedObject() const {
  return GetAXObjectCache().FocusedObject();
}

AXObject* AccessibilityTest::GetAXObjectByElementId(const char* id) const {
  const auto* element = GetElementById(id);
  return element ? GetAXObjectCache().GetOrCreate(element) : nullptr;
}

std::string AccessibilityTest::PrintAXTree() const {
  std::ostringstream stream;
  PrintAXTreeHelper(stream, GetAXRootObject(), 0);
  return stream.str();
}

std::ostringstream& AccessibilityTest::PrintAXTreeHelper(
    std::ostringstream& stream,
    const AXObject* root,
    size_t level) const {
  if (!root)
    return stream;

  stream << std::string(level * 2, '+');
  stream << *root << std::endl;
  for (const Member<AXObject> child : root->Children()) {
    DCHECK(child);
    PrintAXTreeHelper(stream, child.Get(), level + 1);
  }
  return stream;
}

}  // namespace blink