blob: 681d9d48173f0dd4777fbea8b44f0aa2e8c08269 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinski467f1712015-11-16 17:35:44 -080017#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinskid5083f62017-01-16 15:07:21 -080019#include "androidfw/StringPiece.h"
20
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070021#include "io/StringInputStream.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "test/Test.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080023
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024using ::aapt::io::StringInputStream;
25using ::android::StringPiece;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027namespace aapt {
28
29TEST(XmlPullParserTest, NextChildNodeTraversesCorrectly) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070030 std::string str =
31 R"(<?xml version="1.0" encoding="utf-8"?>
32 <a><b><c xmlns:a="http://schema.org"><d/></c><e/></b></a>)";
33 StringInputStream input(str);
34 xml::XmlPullParser parser(&input);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 const size_t depth_outer = parser.depth();
37 ASSERT_TRUE(xml::XmlPullParser::NextChildNode(&parser, depth_outer));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 EXPECT_EQ(xml::XmlPullParser::Event::kStartElement, parser.event());
40 EXPECT_EQ(StringPiece("a"), StringPiece(parser.element_name()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 const size_t depth_a = parser.depth();
43 ASSERT_TRUE(xml::XmlPullParser::NextChildNode(&parser, depth_a));
44 EXPECT_EQ(xml::XmlPullParser::Event::kStartElement, parser.event());
45 EXPECT_EQ(StringPiece("b"), StringPiece(parser.element_name()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 const size_t depth_b = parser.depth();
48 ASSERT_TRUE(xml::XmlPullParser::NextChildNode(&parser, depth_b));
49 EXPECT_EQ(xml::XmlPullParser::Event::kStartElement, parser.event());
50 EXPECT_EQ(StringPiece("c"), StringPiece(parser.element_name()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 ASSERT_TRUE(xml::XmlPullParser::NextChildNode(&parser, depth_b));
53 EXPECT_EQ(xml::XmlPullParser::Event::kStartElement, parser.event());
54 EXPECT_EQ(StringPiece("e"), StringPiece(parser.element_name()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 ASSERT_FALSE(xml::XmlPullParser::NextChildNode(&parser, depth_outer));
57 EXPECT_EQ(xml::XmlPullParser::Event::kEndDocument, parser.event());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060} // namespace aapt