1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "xml/XmlPullParser.h"
18
19#include "androidfw/StringPiece.h"
20
21#include "io/StringStream.h"
22#include "test/Test.h"
23
24using ::aapt::io::StringInputStream;
25using ::android::StringPiece;
26using ::testing::Eq;
27using ::testing::StrEq;
28
29using Event = ::aapt::xml::XmlPullParser::Event;
30
31namespace aapt {
32namespace xml {
33
34TEST(XmlPullParserTest, NextChildNodeTraversesCorrectly) {
35  std::string str =
36      R"(<?xml version="1.0" encoding="utf-8"?>
37         <a><b><c xmlns:a="http://schema.org"><d/></c><e/></b></a>)";
38  StringInputStream input(str);
39  XmlPullParser parser(&input);
40
41  const size_t depth_outer = parser.depth();
42  ASSERT_TRUE(XmlPullParser::NextChildNode(&parser, depth_outer));
43
44  EXPECT_THAT(parser.event(), Eq(XmlPullParser::Event::kStartElement));
45  EXPECT_THAT(parser.element_name(), StrEq("a"));
46
47  const size_t depth_a = parser.depth();
48  ASSERT_TRUE(XmlPullParser::NextChildNode(&parser, depth_a));
49  EXPECT_THAT(parser.event(), Eq(XmlPullParser::Event::kStartElement));
50  EXPECT_THAT(parser.element_name(), StrEq("b"));
51
52  const size_t depth_b = parser.depth();
53  ASSERT_TRUE(XmlPullParser::NextChildNode(&parser, depth_b));
54  EXPECT_THAT(parser.event(), Eq(XmlPullParser::Event::kStartElement));
55  EXPECT_THAT(parser.element_name(), StrEq("c"));
56
57  ASSERT_TRUE(XmlPullParser::NextChildNode(&parser, depth_b));
58  EXPECT_THAT(parser.event(), Eq(XmlPullParser::Event::kStartElement));
59  EXPECT_THAT(parser.element_name(), StrEq("e"));
60
61  ASSERT_FALSE(XmlPullParser::NextChildNode(&parser, depth_outer));
62  EXPECT_THAT(parser.event(), Eq(XmlPullParser::Event::kEndDocument));
63}
64
65}  // namespace xml
66}  // namespace aapt
67