Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Ramin Halavati | 8423107 | 2022-08-17 08:02:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/services/screen_ai/screen_ai_ax_tree_serializer.h" |
| 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "ui/accessibility/ax_enums.mojom.h" |
| 9 | #include "ui/accessibility/ax_node_data.h" |
| 10 | #include "ui/accessibility/ax_tree_id.h" |
| 11 | #include "ui/accessibility/ax_tree_update.h" |
| 12 | |
| 13 | namespace screen_ai { |
| 14 | |
| 15 | TEST(ScreenAIAXTreeSerializerTest, Serialize) { |
| 16 | ui::AXNodeData root_data; |
| 17 | ui::AXNodeData page_1_data; |
| 18 | ui::AXNodeData page_1_text_data; |
| 19 | ui::AXNodeData page_2_data; |
| 20 | ui::AXNodeData page_2_text_data; |
| 21 | |
| 22 | root_data.id = 1; |
| 23 | root_data.role = ax::mojom::Role::kPdfRoot; |
| 24 | |
| 25 | page_1_data.id = 2; |
| 26 | page_1_data.role = ax::mojom::Role::kRegion; |
| 27 | page_1_data.AddBoolAttribute(ax::mojom::BoolAttribute::kIsPageBreakingObject, |
| 28 | true); |
| 29 | |
| 30 | page_1_text_data.id = 3; |
| 31 | page_1_text_data.role = ax::mojom::Role::kStaticText; |
| 32 | page_1_text_data.SetName("some text on page 1"); |
| 33 | page_1_text_data.AddBoolAttribute( |
| 34 | ax::mojom::BoolAttribute::kIsLineBreakingObject, true); |
| 35 | page_1_data.child_ids = {page_1_text_data.id}; |
| 36 | |
| 37 | page_2_data.id = 4; |
| 38 | page_2_data.role = ax::mojom::Role::kRegion; |
| 39 | page_2_data.AddBoolAttribute(ax::mojom::BoolAttribute::kIsPageBreakingObject, |
| 40 | true); |
| 41 | |
| 42 | page_2_text_data.id = 5; |
| 43 | page_2_text_data.role = ax::mojom::Role::kStaticText; |
| 44 | page_2_text_data.SetName("some text on page 2"); |
| 45 | page_2_text_data.AddIntAttribute( |
| 46 | ax::mojom::IntAttribute::kTextStyle, |
| 47 | static_cast<int32_t>(ax::mojom::TextStyle::kBold)); |
| 48 | page_2_data.child_ids = {page_2_text_data.id}; |
| 49 | |
| 50 | root_data.child_ids = {page_1_data.id, page_2_data.id}; |
| 51 | |
| 52 | ScreenAIAXTreeSerializer serializer( |
| 53 | /* parent_tree_id */ ui::AXTreeID::CreateNewAXTreeID(), |
| 54 | {root_data, page_1_data, page_1_text_data, page_2_data, |
| 55 | page_2_text_data}); |
| 56 | ui::AXTreeUpdate update = serializer.Serialize(); |
| 57 | |
Nektarios Paisios | 0deaa27 | 2022-10-25 18:02:20 | [diff] [blame] | 58 | EXPECT_NE(update.tree_data.tree_id, ui::AXTreeIDUnknown()); |
| 59 | EXPECT_NE(update.tree_data.parent_tree_id, ui::AXTreeIDUnknown()); |
| 60 | // After checking the tree IDs, reset them to allow testing using a constant |
| 61 | // expectation string, since tree IDs are dynamically generated. |
| 62 | update.tree_data.tree_id = ui::AXTreeIDUnknown(); |
| 63 | update.tree_data.parent_tree_id = ui::AXTreeIDUnknown(); |
| 64 | |
Ramin Halavati | 8423107 | 2022-08-17 08:02:20 | [diff] [blame] | 65 | const std::string expected_update( |
Nektarios Paisios | 0deaa27 | 2022-10-25 18:02:20 | [diff] [blame] | 66 | "AXTreeUpdate tree data: title=Screen AI\n" |
Aaron Leventhal | 763d46a | 2022-12-21 19:16:22 | [diff] [blame] | 67 | "AXTreeUpdate: root id 1\nid=1 pdfRoot child_ids=2,4 (0, 0)-(0, 0)\n " |
| 68 | "id=2 region child_ids=3 (0, 0)-(0, 0) is_page_breaking_object=true\n " |
| 69 | "id=3 staticText name=some text on page 1 (0, 0)-(0, 0) " |
| 70 | "is_line_breaking_object=true\n id=4 region child_ids=5 (0, 0)-(0, 0) " |
| 71 | "is_page_breaking_object=true\n id=5 staticText name=some " |
| 72 | "text on page 2 (0, 0)-(0, 0)\n"); |
Ramin Halavati | 8423107 | 2022-08-17 08:02:20 | [diff] [blame] | 73 | EXPECT_EQ(expected_update, update.ToString()); |
| 74 | } |
| 75 | |
| 76 | } // namespace screen_ai |