Adding language detection unittest case.

This test directly calls 'GetLanguage' on the nodes, rather than relying on
the browsertests code.

Test will need to be updated once we actually detect languages.

R=dmazzoni,aboxhall
BUG=889370

Change-Id: I1c280453bb8d03005fd5d0f0b87fe300f156277a
Reviewed-on: https://chromium-review.googlesource.com/c/1377484
Commit-Queue: Chris Hall <chrishall@chromium.org>
Reviewed-by: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#617037}
diff --git a/ui/accessibility/ax_language_info_unittest.cc b/ui/accessibility/ax_language_info_unittest.cc
index c15e9b7..464aa59 100644
--- a/ui/accessibility/ax_language_info_unittest.cc
+++ b/ui/accessibility/ax_language_info_unittest.cc
@@ -17,12 +17,11 @@
 
 // Tests that AXNode::GetLanguage() terminates when there is no lang attribute.
 TEST(AXLanguageInfoTest, TestGetLanguageNoLangAttr) {
-  /* build tree including parenting, this is to exercise the code paths within
-   * AXNode::GetLanguage() which scan up the tree to handle lang inheritance.
-   *      1
-   *    2   3
-   *  4
-   */
+  // build tree including parenting, this is to exercise the code paths within
+  // AXNode::GetLanguage() which scan up the tree to handle lang inheritance.
+  //      1
+  //    2   3
+  //  4
   AXTreeUpdate initial_state;
   initial_state.root_id = 1;
   initial_state.nodes.resize(4);
@@ -68,17 +67,16 @@
 }
 
 TEST(AXLanguageInfoTest, TestGetLanguageLangAttrInheritance) {
-  /* build tree including parenting, this is to exercise the code paths within
-   * AXNode::GetLanguage() which scan up the tree to handle lang inheritance.
-   *        1
-   *      2   3
-   *    4
-   *  5
-   *
-   *  1 - English
-   *  2 - French
-   *  all other nodes are unspecified
-   */
+  // build tree including parenting, this is to exercise the code paths within
+  // AXNode::GetLanguage() which scan up the tree to handle lang inheritance.
+  //        1
+  //      2   3
+  //    4
+  //  5
+  //
+  //  1 - English
+  //  2 - French
+  //  all other nodes are unspecified
   AXTreeUpdate initial_state;
   initial_state.root_id = 1;
   initial_state.nodes.resize(5);
@@ -121,4 +119,98 @@
   EXPECT_EQ(item5_lang, "fr");
 }
 
+TEST(AXLanguageInfoTest, TestGetLanguageLangDetection) {
+  // build tree including parenting, this is to exercise the code paths within
+  // AXNode::GetLanguage() which scan up the tree to handle lang inheritance.
+  //        1
+  //      2   3
+  //    4
+  //  5
+  //
+  //  1 - English lang attribute, no text
+  //  2 - French lang attribute,  no text
+  //  3 - no attribute,           French text
+  //  4 - no attribute,           English text
+  //  5 - no attribute,           German text
+  AXTreeUpdate initial_state;
+  initial_state.root_id = 1;
+  initial_state.nodes.resize(5);
+
+  {
+    AXNodeData& node1 = initial_state.nodes[0];
+    node1.id = 1;
+    node1.child_ids.resize(2);
+    node1.child_ids[0] = 2;
+    node1.child_ids[1] = 3;
+    node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "en");
+  }
+
+  {
+    AXNodeData& node2 = initial_state.nodes[1];
+    node2.id = 2;
+    node2.child_ids.resize(1);
+    node2.child_ids[0] = 4;
+    node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
+  }
+
+  {
+    AXNodeData& node3 = initial_state.nodes[2];
+    node3.id = 3;
+    node3.role = ax::mojom::Role::kStaticText;
+    std::string node3_text =
+        "Ce texte a été créé avec Google Translate, il est peu probable qu'il "
+        "soit idiomatique dans la langue cible indiquée Ce texte est "
+        "uniquement utilisé pour tester la détection de la langue.";
+    node3.AddStringAttribute(ax::mojom::StringAttribute::kName, node3_text);
+  }
+
+  {
+    AXNodeData& node4 = initial_state.nodes[3];
+    node4.id = 4;
+    node4.child_ids.resize(1);
+    node4.child_ids[0] = 5;
+    node4.role = ax::mojom::Role::kStaticText;
+    std::string node4_text =
+        "This is text created using Google Translate, it is unlikely to be "
+        "idomatic in the given target langauge. This text is only used to test "
+        "language detection";
+    node4.AddStringAttribute(ax::mojom::StringAttribute::kName, node4_text);
+  }
+
+  {
+    AXNodeData& node5 = initial_state.nodes[4];
+    node5.id = 5;
+    node5.role = ax::mojom::Role::kStaticText;
+    std::string node5_text =
+        "Dies ist ein mit Google Translate erstellter Text. Es ist "
+        "unwahrscheinlich, dass er in der angegebenen Zielsprache idiomatisch "
+        "ist. Dieser Text wird nur zum Testen der Spracherkennung verwendet.";
+    node5.AddStringAttribute(ax::mojom::StringAttribute::kName, node5_text);
+  }
+
+  AXTree tree(initial_state);
+
+  // TODO(crbug/889370): these expectations will be wrong once we detect
+  // language from the text.
+  AXNode* item1 = tree.GetFromId(1);
+  std::string item1_lang = item1->GetLanguage();
+  EXPECT_EQ(item1_lang, "en");
+
+  AXNode* item2 = tree.GetFromId(2);
+  std::string item2_lang = item2->GetLanguage();
+  EXPECT_EQ(item2_lang, "fr");
+
+  AXNode* item3 = tree.GetFromId(3);
+  std::string item3_lang = item3->GetLanguage();
+  EXPECT_EQ(item3_lang, "en");
+
+  AXNode* item4 = tree.GetFromId(4);
+  std::string item4_lang = item4->GetLanguage();
+  EXPECT_EQ(item4_lang, "fr");
+
+  AXNode* item5 = tree.GetFromId(5);
+  std::string item5_lang = item5->GetLanguage();
+  EXPECT_EQ(item5_lang, "fr");
+}
+
 }  // namespace ui