Flutter Engine
The Flutter Engine
SkDOMTest.cpp
Go to the documentation of this file.
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
9
10#if defined(SK_XML)
12#include "src/xml/SkDOM.h"
13#include "tests/Test.h"
14
15static 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
26DEF_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
#define DEF_TEST(name, reporter)
Definition: Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
Definition: SkDOM.h:24
Type
Definition: SkDOM.h:42
@ kText_Type
Definition: SkDOM.h:44
@ kElement_Type
Definition: SkDOM.h:43
Definition: dom.py:1
string root
Definition: scale_cpu.py:20