• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkStream.h"
9 #include "tests/Test.h"
10 
11 #if defined(SK_XML)
12 
13 #include "src/xml/SkDOM.h"
14 
check_node(skiatest::Reporter * r,const SkDOM & dom,const SkDOM::Node * node,const char * expectedName,SkDOM::Type expectedType)15 static const SkDOM::Node* check_node(skiatest::Reporter* r, const SkDOM& dom,
16                                      const SkDOM::Node* node, const char* expectedName,
17                                      SkDOM::Type expectedType) {
18     REPORTER_ASSERT(r, node);
19     if (node) {
20         REPORTER_ASSERT(r, !strcmp(dom.getName(node), expectedName));
21         REPORTER_ASSERT(r, dom.getType(node) == expectedType);
22     }
23     return node;
24 }
25 
DEF_TEST(SkDOM_test,r)26 DEF_TEST(SkDOM_test, r) {
27     static const char gDoc[] =
28         "<root a='1' b='2'>"
29             "<elem1 c='3' />"
30             "<elem2 d='4' />"
31             "<elem3 e='5'>"
32                 "<subelem1>Some text.</subelem1>"
33                 "<subelem2 f='6' g='7'/>"
34                 "<subelem3>Some more text.</subelem3>"
35             "</elem3>"
36             "<elem4 h='8'/>"
37         "</root>"
38         ;
39 
40     SkMemoryStream docStream(gDoc, sizeof(gDoc) - 1);
41 
42     SkDOM   dom;
43     REPORTER_ASSERT(r, !dom.getRootNode());
44 
45     const SkDOM::Node* root = dom.build(docStream);
46     REPORTER_ASSERT(r, root && dom.getRootNode() == root);
47 
48     const char* v = dom.findAttr(root, "a");
49     REPORTER_ASSERT(r, v && !strcmp(v, "1"));
50     v = dom.findAttr(root, "b");
51     REPORTER_ASSERT(r, v && !strcmp(v, "2"));
52     v = dom.findAttr(root, "c");
53     REPORTER_ASSERT(r, v == nullptr);
54 
55     REPORTER_ASSERT(r, dom.getFirstChild(root, "elem1"));
56     REPORTER_ASSERT(r, !dom.getFirstChild(root, "subelem1"));
57 
58     {
59         const auto* elem1 = check_node(r, dom, dom.getFirstChild(root),
60                                        "elem1", SkDOM::kElement_Type);
61         const auto* elem2 = check_node(r, dom, dom.getNextSibling(elem1),
62                                        "elem2", SkDOM::kElement_Type);
63         const auto* elem3 = check_node(r, dom, dom.getNextSibling(elem2),
64                                        "elem3", SkDOM::kElement_Type);
65         {
66             const auto* subelem1 = check_node(r, dom, dom.getFirstChild(elem3),
67                                               "subelem1", SkDOM::kElement_Type);
68             {
69                 check_node(r, dom, dom.getFirstChild(subelem1),
70                            "Some text.", SkDOM::kText_Type);
71             }
72             const auto* subelem2 = check_node(r, dom, dom.getNextSibling(subelem1),
73                                               "subelem2", SkDOM::kElement_Type);
74             const auto* subelem3 = check_node(r, dom, dom.getNextSibling(subelem2),
75                                               "subelem3", SkDOM::kElement_Type);
76             {
77                 check_node(r, dom, dom.getFirstChild(subelem3),
78                            "Some more text.", SkDOM::kText_Type);
79             }
80         }
81         check_node(r, dom, dom.getNextSibling(elem3),
82                    "elem4", SkDOM::kElement_Type);
83     }
84 }
85 
86 #endif // SK_XML
87