🦁 Fix voiceover behaviour for answers.

The new cells were using the legacy cell accessibility behaviour which
relied on one of the details labels be hidden when an answer is shown.
In new cells, both cells are not hidden, so this cannot be used to
determine the correct a11y value anymore. Instead use isAnswer directly.

Also adds unit tests to prevent regression.

(cherry picked from commit 107720462fde8e7e87ff2ed3286c1a3de348cca4)

Bug: 992847
Change-Id: I47fb9ab7ce548c192e2089c72c091a94fc8b1b8d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1748749
Auto-Submit: Stepan Khapugin <[email protected]>
Reviewed-by: Robbie Gibson <[email protected]>
Commit-Queue: Stepan Khapugin <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#685954}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1752296
Reviewed-by: Kariah Davis <[email protected]>
Cr-Commit-Position: refs/branch-heads/3865@{#364}
Cr-Branched-From: 0cdcc6158160790658d1f033d3db873603250124-refs/heads/master@{#681094}
diff --git a/ios/chrome/browser/ui/omnibox/popup/BUILD.gn b/ios/chrome/browser/ui/omnibox/popup/BUILD.gn
index 3bef1351..4e1e259d 100644
--- a/ios/chrome/browser/ui/omnibox/popup/BUILD.gn
+++ b/ios/chrome/browser/ui/omnibox/popup/BUILD.gn
@@ -132,6 +132,7 @@
   configs += [ "//build/config/compiler:enable_arc" ]
   testonly = true
   sources = [
+    "omnibox_popup_row_cell_unittest.mm",
     "omnibox_popup_view_controller_unittest.mm",
   ]
   deps = [
diff --git a/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell.mm b/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell.mm
index 912a41b..4ab03e9 100644
--- a/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell.mm
+++ b/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell.mm
@@ -373,7 +373,7 @@
 }
 
 - (NSString*)accessibilityValue {
-  return self.detailTruncatingLabel.hidden
+  return self.suggestion.hasAnswer
              ? self.detailAnswerLabel.attributedText.string
              : self.detailTruncatingLabel.attributedText.string;
 }
diff --git a/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell_unittest.mm b/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell_unittest.mm
new file mode 100644
index 0000000..43fbde8
--- /dev/null
+++ b/ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell_unittest.mm
@@ -0,0 +1,82 @@
+// 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.
+
+#import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row_cell.h"
+
+#import "ios/chrome/browser/ui/omnibox/popup/autocomplete_suggestion.h"
+#import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_row.h"
+#include "testing/gtest_mac.h"
+#include "testing/platform_test.h"
+#include "url/gurl.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+// Implements AutocompleteSuggestion protocol for use in tests. Can be populated
+// directly into properties.
+@interface FakeAutocompleteMatch : NSObject <AutocompleteSuggestion>
+@property(nonatomic, assign) BOOL supportsDeletion;
+@property(nonatomic, assign) BOOL hasAnswer;
+@property(nonatomic, assign) BOOL isURL;
+@property(nonatomic, assign, getter=isAppendable) BOOL appendable;
+@property(nonatomic, strong) UIImage* suggestionTypeIcon;
+@property(nonatomic, assign) BOOL isTabMatch;
+@property(nonatomic, strong) NSAttributedString* text;
+@property(nonatomic, strong) NSAttributedString* detailText;
+@property(nonatomic, assign) NSInteger numberOfLines;
+@property(nonatomic, assign) BOOL hasImage;
+@property(nonatomic, assign) GURL imageURL;
+@property(nonatomic, assign) GURL faviconPageURL;
+@property(nonatomic, strong) id<OmniboxIcon> icon;
+
+@end
+
+@implementation FakeAutocompleteMatch
+@end
+
+namespace {
+
+class OmniboxPopupRowCellTest : public PlatformTest {
+ protected:
+  void SetUp() override {
+    PlatformTest::SetUp();
+    cell_ = [[OmniboxPopupRowCell alloc] init];
+  }
+
+  OmniboxPopupRowCell* cell_;
+};
+
+TEST_F(OmniboxPopupRowCellTest, ReadsAnswersInVoiceover) {
+  FakeAutocompleteMatch* fakeAnswerMatch = [[FakeAutocompleteMatch alloc] init];
+  fakeAnswerMatch.hasAnswer = YES;
+
+  // The detail for answers is the suggested question, for example if user types
+  // "how tall is" the detail text might be "how tall is the Eiffel tower".
+  fakeAnswerMatch.detailText =
+      [[NSAttributedString alloc] initWithString:@"question"];
+  fakeAnswerMatch.text = [[NSAttributedString alloc] initWithString:@"answer"];
+
+  [cell_ setupWithAutocompleteSuggestion:fakeAnswerMatch incognito:NO];
+
+  EXPECT_NSEQ([cell_ accessibilityValue], @"question");
+  EXPECT_NSEQ([cell_ accessibilityLabel], @"answer");
+}
+
+TEST_F(OmniboxPopupRowCellTest, ReadsNonAnswersInVoiceover) {
+  FakeAutocompleteMatch* fakeNonAnswerMatch =
+      [[FakeAutocompleteMatch alloc] init];
+  fakeNonAnswerMatch.hasAnswer = NO;
+
+  fakeNonAnswerMatch.detailText =
+      [[NSAttributedString alloc] initWithString:@"detail"];
+  fakeNonAnswerMatch.text = [[NSAttributedString alloc] initWithString:@"body"];
+
+  [cell_ setupWithAutocompleteSuggestion:fakeNonAnswerMatch incognito:NO];
+
+  EXPECT_NSEQ([cell_ accessibilityValue], @"detail");
+  EXPECT_NSEQ([cell_ accessibilityLabel], @"body");
+}
+
+}  // namespace