blob: b6b9f5f17a514975a273edbe6fbb482aa7132047 [file] [log] [blame]
fmalita7445e862016-07-14 19:14:06 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkStream.h"
9#include "tests/Test.h"
fmalita7445e862016-07-14 19:14:06 -070010
fmalita718df0a2016-07-15 10:33:29 -070011#if defined(SK_XML)
12
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/xml/SkDOM.h"
fmalita718df0a2016-07-15 10:33:29 -070014
fmalita12584552016-07-18 14:47:29 -070015static 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
fmalita7445e862016-07-14 19:14:06 -070026DEF_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'>"
fmalita12584552016-07-18 14:47:29 -070032 "<subelem1>Some text.</subelem1>"
fmalita7445e862016-07-14 19:14:06 -070033 "<subelem2 f='6' g='7'/>"
fmalita12584552016-07-18 14:47:29 -070034 "<subelem3>Some more text.</subelem3>"
fmalita7445e862016-07-14 19:14:06 -070035 "</elem3>"
36 "<elem4 h='8'/>"
37 "</root>"
38 ;
39
fmalitaa427cc42016-07-18 10:56:43 -070040 SkMemoryStream docStream(gDoc, sizeof(gDoc) - 1);
41
fmalita7445e862016-07-14 19:14:06 -070042 SkDOM dom;
43 REPORTER_ASSERT(r, !dom.getRootNode());
44
fmalitaa427cc42016-07-18 10:56:43 -070045 const SkDOM::Node* root = dom.build(docStream);
fmalita7445e862016-07-14 19:14:06 -070046 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"));
fmalita12584552016-07-18 14:47:29 -070057
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 }
fmalita7445e862016-07-14 19:14:06 -070084}
fmalita718df0a2016-07-15 10:33:29 -070085
86#endif // SK_XML