Skip to content

Commit

Permalink
Add an option to not prune the Android AX tree, to preserve more styl…
Browse files Browse the repository at this point in the history
…e info.

BUG=501039

Review URL: https://codereview.chromium.org/1182783008

Cr-Commit-Position: refs/heads/master@{#334740}
  • Loading branch information
minorninth authored and Commit bot committed Jun 16, 2015
1 parent 4beca47 commit 38de562
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.chromium.android_webview.test.TestAwContentsClient.OnDownloadStartHelper;
import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.content.browser.test.util.CallbackHelper;
Expand Down Expand Up @@ -650,7 +649,6 @@ public void testRequestAccessibilitySnapshotStyles() throws Throwable {
assertFalse(child.underline);
}

@DisabledTest
@Feature({"AndroidWebView"})
public void testRequestAccessibilitySnapshotStrongStyle() throws Throwable {
final String data = "<html><body><p>foo</p><p><strong>bar</strong></p></body></html>";
Expand All @@ -662,34 +660,36 @@ public void testRequestAccessibilitySnapshotStrongStyle() throws Throwable {
assertTrue(child1.hasStyle);
assertFalse(child1.bold);
AccessibilitySnapshotNode child2 = root.children.get(1);
assertEquals("bar", child2.text);
assertTrue(child1.textSize < child2.textSize);
AccessibilitySnapshotNode child2child = child2.children.get(0);
assertEquals("bar", child2child.text);
assertEquals(child1.textSize, child2child.textSize);
assertTrue(child2child.bold);
}

@DisabledTest
@Feature({"AndroidWebView"})
public void testRequestAccessibilitySnapshotItalicStyle() throws Throwable {
final String data = "<html><body><i>foo</i></body></html>";
AccessibilitySnapshotNode root = receiveAccessibilitySnapshot(data);
assertEquals(1, root.children.size());
assertEquals("", root.text);
AccessibilitySnapshotNode child = root.children.get(0);
assertEquals("foo", child.text);
assertTrue(child.hasStyle);
assertTrue(child.italic);
AccessibilitySnapshotNode grandchild = child.children.get(0);
assertEquals("foo", grandchild.text);
assertTrue(grandchild.hasStyle);
assertTrue(grandchild.italic);
}

@DisabledTest
@Feature({"AndroidWebView"})
public void testRequestAccessibilitySnapshotBoldStyle() throws Throwable {
final String data = "<html><body><b>foo</b></body></html>";
AccessibilitySnapshotNode root = receiveAccessibilitySnapshot(data);
assertEquals(1, root.children.size());
assertEquals("", root.text);
AccessibilitySnapshotNode child = root.children.get(0);
assertEquals("foo", child.text);
assertTrue(child.hasStyle);
assertTrue(child.bold);
AccessibilitySnapshotNode grandchild = child.children.get(0);
assertEquals("foo", grandchild.text);
assertTrue(grandchild.hasStyle);
assertTrue(grandchild.bold);
}

@Feature({"AndroidWebView"})
Expand Down
24 changes: 14 additions & 10 deletions content/browser/accessibility/browser_accessibility_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
if (GetRole() == ui::AX_ROLE_DATE || GetRole() == ui::AX_ROLE_INPUT_TIME)
return true;

// Headings with text can drop their children.
base::string16 name = GetText();
if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
return true;
BrowserAccessibilityManagerAndroid* manager_android =
static_cast<BrowserAccessibilityManagerAndroid*>(manager());
if (manager_android->prune_tree_for_screen_reader()) {
// Headings with text can drop their children.
base::string16 name = GetText();
if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
return true;

// Focusable nodes with text can drop their children.
if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
return true;
// Focusable nodes with text can drop their children.
if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
return true;

// Nodes with only static text as children can drop their children.
if (HasOnlyStaticTextChildren())
return true;
// Nodes with only static text as children can drop their children.
if (HasOnlyStaticTextChildren())
return true;
}

return BrowserAccessibility::PlatformIsLeaf();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ BrowserAccessibilityManagerAndroid::BrowserAccessibilityManagerAndroid(
const ui::AXTreeUpdate& initial_tree,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory)
: BrowserAccessibilityManager(delegate, factory) {
: BrowserAccessibilityManager(delegate, factory),
prune_tree_for_screen_reader_(true) {
Initialize(initial_tree);
SetContentViewCore(content_view_core);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ class CONTENT_EXPORT BrowserAccessibilityManagerAndroid
void SetContentViewCore(
base::android::ScopedJavaLocalRef<jobject> content_view_core);

// By default, the tree is pruned for a better screen reading experience,
// including:
// * If the node has only static text children
// * If the node is focusable and has no focusable children
// * If the node is a heading
// This can be turned off to generate a tree that more accurately reflects
// the DOM and includes style changes within these nodes.
void set_prune_tree_for_screen_reader(bool prune) {
prune_tree_for_screen_reader_ = prune;
}
bool prune_tree_for_screen_reader() { return prune_tree_for_screen_reader_; }

// Implementation of BrowserAccessibilityManager.
void NotifyAccessibilityEvent(ui::AXEvent event_type,
BrowserAccessibility* node) override;
Expand Down Expand Up @@ -145,6 +157,9 @@ class CONTENT_EXPORT BrowserAccessibilityManagerAndroid
// Handle a hover event from the renderer process.
void HandleHoverEvent(BrowserAccessibility* node);

// See docs for set_prune_tree_for_screen_reader, above.
bool prune_tree_for_screen_reader_;

DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManagerAndroid);
};

Expand Down
8 changes: 5 additions & 3 deletions content/browser/web_contents/web_contents_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "content/browser/accessibility/browser_accessibility_android.h"
#include "content/browser/accessibility/browser_accessibility_manager.h"
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#include "content/browser/android/interstitial_page_delegate_android.h"
#include "content/browser/frame_host/interstitial_page_impl.h"
#include "content/browser/media/android/browser_media_player_manager.h"
Expand Down Expand Up @@ -105,8 +105,10 @@ void AXTreeSnapshotCallback(const ScopedJavaGlobalRef<jobject>& callback,
Java_WebContentsImpl_onAccessibilitySnapshot(env, nullptr, callback.obj());
return;
}
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(result, nullptr));
scoped_ptr<BrowserAccessibilityManagerAndroid> manager(
static_cast<BrowserAccessibilityManagerAndroid*>(
BrowserAccessibilityManager::Create(result, nullptr)));
manager->set_prune_tree_for_screen_reader(false);
BrowserAccessibilityAndroid* root =
static_cast<BrowserAccessibilityAndroid*>(manager->GetRoot());
ScopedJavaLocalRef<jobject> j_root = WalkAXTreeDepthFirst(env, root);
Expand Down

0 comments on commit 38de562

Please sign in to comment.