// Copyright 2014 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif #include <memory> #include <numeric> #include <optional> #include <vector> #include "base/strings/string_number_conversions.h" #include "build/build_config.h" #include "testing/gtest/include/gtest/gtest-param-test.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/accessibility/ax_event_generator.h" #include "ui/accessibility/ax_node.h" #include "ui/accessibility/ax_serializable_tree.h" #include "ui/accessibility/ax_tree.h" #include "ui/accessibility/ax_tree_serializer.h" #include "ui/accessibility/ax_tree_update.h" #include "ui/accessibility/tree_generator.h" namespace ui { namespace { // Do a more exhaustive test in release mode. If you're modifying // the algorithm you may want to try even larger tree sizes if you // can afford the time. #ifdef NDEBUG constexpr int kMax_tree_size = 4; #else constexpr int kMax_tree_size = …; #endif // We split the test into four by splitting the two nested loops that builds the // trees. To do so, we need to know the maximum number of (permuted) trees. constexpr int kMax_number_of_trees0 = …; constexpr int kMax_number_of_trees1 = …; // A function to turn a tree into a string, capturing only the node ids // and their relationship to one another. // // The string format is kind of like an S-expression, with each expression // being either a node id, or a node id followed by a subexpression // representing its children. // // Examples: // // (1) is a tree with a single node with id 1. // (1 (2 3)) is a tree with 1 as the root, and 2 and 3 as its children. // (1 (2 (3))) has 1 as the root, 2 as its child, and then 3 as the child of 2. // (1 (2 (3x))) is the same with node 3 ignored. std::string TreeToStringHelper(const AXNode* node) { … } std::string TreeToString(const AXTree& tree) { … } AXTreeUpdate SerializeEntireTree(AXSerializableTree& tree) { … } // Create an AXTreeUpdate consisting of only those nodes from // |tree0| that changed their ignored status in |tree1|. AXTreeUpdate MakeTreeUpdateFromIgnoredChanges(AXSerializableTree& tree0, AXSerializableTree& tree1) { … } void SerializeUnignoredNodes(AXNode* node, AXTreeUpdate* update) { … } void MakeTreeOfUnignoredNodesOnly(AXSerializableTree& src, AXSerializableTree* dst) { … } } // namespace // Test the TreeGenerator class by building all possible trees with // 3 nodes and the ids [1...3], with no permutations of ids. TEST(AXGeneratedTreeTest, TestTreeGeneratorNoPermutations) { … } // Test generating trees with permutations of ignored nodes. TEST(AXGeneratedTreeTest, TestGeneratingTreesWithIgnoredNodes) { … } // Test the TreeGenerator class by building all possible trees with // 3 nodes and the ids [1...3] permuted in any order. TEST(AXGeneratedTreeTest, TestTreeGeneratorWithPermutations) { … } struct PermutationBlock { … }; class SerializeGeneratedTreesTest : public testing::TestWithParam<PermutationBlock> { … }; TEST_P(SerializeGeneratedTreesTest, SerializeGeneratedTrees) { … } // Test mutating every possible tree with <n> nodes to every other possible // tree with <n> nodes, where <n> is 4 in release mode and 3 in debug mode // (for speed). For each possible combination of trees, we also vary which // node we serialize first. // // For every possible scenario, we check that the AXTreeUpdate is valid, // that the destination tree can unserialize it and create a valid tree, // and that after updating all nodes the resulting tree now matches the // intended tree. // // Sheriffs: this test is actually very stable and reliable, but it's // cpu-bound so under extremely heavy load it sometimes times out even // though it only takes 1 - 2 seconds to run under normal load. // Please don't disable unless it's actually flaking frequently (e.g., // every day). Check Flake Portal first. INSTANTIATE_TEST_SUITE_P(…); // Sheriffs: this test is actually very stable and reliable, but it's // cpu-bound so under extremely heavy load it sometimes times out even // though it only takes 1 - 2 seconds to run under normal load. // Please don't disable unless it's actually flaking frequently (e.g., // every day). Check Flake Portal first. TEST(AXGeneratedTreeTest, GeneratedTreesWithIgnoredNodes) { … } } // namespace ui