Commit bac35afd authored by Regan Hsu's avatar Regan Hsu Committed by Chromium LUCI CQ

[CrOS PhoneHub] Update A11y string for battery percent label.

ChromeVox will now verbalize "Phone Battery [displayed percent] percent"
instead of "[displayed percent]" without any context about what the
percent means.

Screenshot:
https://storage.cloud.google.com/chromium-translation-screenshots/72b4b1039e35fdd7f6d42bc25d509c1227e94d16

Fixed: 1152779
Change-Id: Icf6ccaf130989b8c73e12e6e47f760a987293bcf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2577756Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Regan Hsu <hsuregan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834509}
parent 1d4a17ad
......@@ -1066,6 +1066,9 @@ This file contains the strings for ash.
<message name="IDS_ASH_PHONE_HUB_BATTERY_STATUS_CHARGING_USB" desc="The tooltip text indicating the phone is currently charging via a USB connection.">
Charging via USB
</message>
<message name="IDS_ASH_PHONE_HUB_BATTERY_PERCENTAGE_ACCESSIBLE_TEXT" desc="The accessible text of the phone battery percentage displayed in the Phone Hub tray.">
Phone battery <ph name="BATTERY_PERCENTAGE">$1<ex>100</ex></ph>%
</message>
<message name="IDS_ASH_PHONE_HUB_BATTERY_SAVER_ON" desc="The tooltip text indicating the phone is currently in battery saver mode.">
Battery saver mode is on
</message>
......
72b4b1039e35fdd7f6d42bc25d509c1227e94d16
\ No newline at end of file
......@@ -265,6 +265,9 @@ void PhoneStatusView::UpdateBatteryStatus() {
SetBatteryTooltipText();
battery_label_->SetText(
base::FormatPercent(phone_status.battery_percentage()));
battery_label_->SetAccessibleName(l10n_util::GetStringFUTF16(
IDS_ASH_PHONE_HUB_BATTERY_PERCENTAGE_ACCESSIBLE_TEXT,
base::NumberToString16(phone_status.battery_percentage())));
}
PowerStatus::BatteryImageInfo PhoneStatusView::CalculateBatteryInfo() {
......
......@@ -108,6 +108,19 @@ void Label::SetText(const base::string16& new_text) {
stored_selection_range_ = gfx::Range::InvalidRange();
}
void Label::SetAccessibleName(const base::string16& name) {
if (name == accessible_name_)
return;
accessible_name_ = name;
OnPropertyChanged(&accessible_name_, kPropertyEffectsNone);
NotifyAccessibilityEvent(ax::mojom::Event::kTextChanged, true);
}
const base::string16& Label::GetAccessibleName() const {
return accessible_name_.empty() ? full_text_->GetDisplayText()
: accessible_name_;
}
int Label::GetTextContext() const {
return text_context_;
}
......@@ -619,7 +632,7 @@ void Label::GetAccessibleNodeData(ui::AXNodeData* node_data) {
else
node_data->role = ax::mojom::Role::kStaticText;
node_data->SetName(full_text_->GetDisplayText());
node_data->SetName(GetAccessibleName());
}
base::string16 Label::GetTooltipText(const gfx::Point& p) const {
......@@ -1210,6 +1223,7 @@ ADD_PROPERTY_METADATA(base::string16, TooltipText)
ADD_PROPERTY_METADATA(bool, HandlesTooltips)
ADD_PROPERTY_METADATA(bool, CollapseWhenHidden)
ADD_PROPERTY_METADATA(int, MaximumWidth)
ADD_PROPERTY_METADATA(base::string16, AccessibleName)
END_METADATA
} // namespace views
......@@ -90,6 +90,12 @@ class VIEWS_EXPORT Label : public View,
const base::string16& GetText() const;
virtual void SetText(const base::string16& text);
// Set the accessibility name that will be announced by the screen reader.
// If this function is not called, the screen reader defaults to verbalizing
// the text value.
void SetAccessibleName(const base::string16& name);
const base::string16& GetAccessibleName() const;
// Where the label appears in the UI. Passed in from the constructor. This is
// a value from views::style::TextContext or an enum that extends it.
int GetTextContext() const;
......@@ -454,6 +460,9 @@ class VIEWS_EXPORT Label : public View,
std::unique_ptr<SelectionController> selection_controller_;
// Accessibility data.
base::string16 accessible_name_;
// Context menu related members.
ui::SimpleMenuModel context_menu_contents_;
std::unique_ptr<views::MenuRunner> context_menu_runner_;
......
......@@ -605,7 +605,9 @@ TEST_F(LabelTest, TooltipProperty) {
}
TEST_F(LabelTest, Accessibility) {
label()->SetText(ASCIIToUTF16("My special text."));
const base::string16 accessible_name = ASCIIToUTF16("A11y text.");
label()->SetText(ASCIIToUTF16("Displayed text."));
ui::AXNodeData node_data;
label()->GetAccessibleNodeData(&node_data);
......@@ -614,6 +616,33 @@ TEST_F(LabelTest, Accessibility) {
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
EXPECT_FALSE(
node_data.HasIntAttribute(ax::mojom::IntAttribute::kRestriction));
// Setting a custom accessible name overrides the displayed text in
// screen reader announcements.
label()->SetAccessibleName(accessible_name);
label()->GetAccessibleNodeData(&node_data);
EXPECT_EQ(accessible_name,
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
EXPECT_NE(label()->GetText(),
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
// Changing the displayed text will not impact the non-empty accessible name.
label()->SetText(ASCIIToUTF16("Different displayed Text."));
label()->GetAccessibleNodeData(&node_data);
EXPECT_EQ(accessible_name,
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
EXPECT_NE(label()->GetText(),
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
// Clearing the accessible name will cause the screen reader to default to
// verbalizing the displayed text.
label()->SetAccessibleName(ASCIIToUTF16(""));
label()->GetAccessibleNodeData(&node_data);
EXPECT_EQ(label()->GetText(),
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
}
TEST_F(LabelTest, TextChangeWithoutLayout) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment