fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkStream.h" |
| 9 | #include "tests/Test.h" |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 10 | |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 11 | #if defined(SK_XML) |
| 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/xml/SkDOM.h" |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 14 | |
fmalita | 1258455 | 2016-07-18 14:47:29 -0700 | [diff] [blame] | 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 | |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 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'>" |
fmalita | 1258455 | 2016-07-18 14:47:29 -0700 | [diff] [blame] | 32 | "<subelem1>Some text.</subelem1>" |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 33 | "<subelem2 f='6' g='7'/>" |
fmalita | 1258455 | 2016-07-18 14:47:29 -0700 | [diff] [blame] | 34 | "<subelem3>Some more text.</subelem3>" |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 35 | "</elem3>" |
| 36 | "<elem4 h='8'/>" |
| 37 | "</root>" |
| 38 | ; |
| 39 | |
fmalita | a427cc4 | 2016-07-18 10:56:43 -0700 | [diff] [blame] | 40 | SkMemoryStream docStream(gDoc, sizeof(gDoc) - 1); |
| 41 | |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 42 | SkDOM dom; |
| 43 | REPORTER_ASSERT(r, !dom.getRootNode()); |
| 44 | |
fmalita | a427cc4 | 2016-07-18 10:56:43 -0700 | [diff] [blame] | 45 | const SkDOM::Node* root = dom.build(docStream); |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 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")); |
fmalita | 1258455 | 2016-07-18 14:47:29 -0700 | [diff] [blame] | 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 | } |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 84 | } |
fmalita | 718df0a | 2016-07-15 10:33:29 -0700 | [diff] [blame] | 85 | |
| 86 | #endif // SK_XML |