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 "ScopedXmlPullParser.h"
18 #include "SourceXmlPullParser.h"
19
20 #include <gtest/gtest.h>
21 #include <sstream>
22 #include <string>
23
24 namespace aapt {
25
TEST(ScopedXmlPullParserTest,StopIteratingAtNoNZeroDepth)26 TEST(ScopedXmlPullParserTest, StopIteratingAtNoNZeroDepth) {
27 std::stringstream input;
28 input << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl
29 << "<resources><string></string></resources>" << std::endl;
30
31 SourceXmlPullParser sourceParser(input);
32 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
33 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
34
35 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
36 EXPECT_EQ(std::u16string(u"string"), sourceParser.getElementName());
37
38 {
39 ScopedXmlPullParser scopedParser(&sourceParser);
40 EXPECT_EQ(XmlPullParser::Event::kEndElement, scopedParser.next());
41 EXPECT_EQ(std::u16string(u"string"), sourceParser.getElementName());
42
43 EXPECT_EQ(XmlPullParser::Event::kEndDocument, scopedParser.next());
44 }
45
46 EXPECT_EQ(XmlPullParser::Event::kEndElement, sourceParser.next());
47 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
48
49 EXPECT_EQ(XmlPullParser::Event::kEndDocument, sourceParser.next());
50 }
51
TEST(ScopedXmlPullParserTest,FinishCurrentElementOnDestruction)52 TEST(ScopedXmlPullParserTest, FinishCurrentElementOnDestruction) {
53 std::stringstream input;
54 input << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl
55 << "<resources><string></string></resources>" << std::endl;
56
57 SourceXmlPullParser sourceParser(input);
58 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
59 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
60
61 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
62 EXPECT_EQ(std::u16string(u"string"), sourceParser.getElementName());
63
64 {
65 ScopedXmlPullParser scopedParser(&sourceParser);
66 EXPECT_EQ(std::u16string(u"string"), sourceParser.getElementName());
67 }
68
69 EXPECT_EQ(XmlPullParser::Event::kEndElement, sourceParser.next());
70 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
71
72 EXPECT_EQ(XmlPullParser::Event::kEndDocument, sourceParser.next());
73 }
74
TEST(ScopedXmlPullParserTest,NestedParsersOperateCorrectly)75 TEST(ScopedXmlPullParserTest, NestedParsersOperateCorrectly) {
76 std::stringstream input;
77 input << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl
78 << "<resources><string><foo></foo></string></resources>" << std::endl;
79
80 SourceXmlPullParser sourceParser(input);
81 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
82 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
83
84 EXPECT_EQ(XmlPullParser::Event::kStartElement, sourceParser.next());
85 EXPECT_EQ(std::u16string(u"string"), sourceParser.getElementName());
86
87 {
88 ScopedXmlPullParser scopedParser(&sourceParser);
89 EXPECT_EQ(std::u16string(u"string"), scopedParser.getElementName());
90 while (XmlPullParser::isGoodEvent(scopedParser.next())) {
91 if (scopedParser.getEvent() != XmlPullParser::Event::kStartElement) {
92 continue;
93 }
94
95 ScopedXmlPullParser subScopedParser(&scopedParser);
96 EXPECT_EQ(std::u16string(u"foo"), subScopedParser.getElementName());
97 }
98 }
99
100 EXPECT_EQ(XmlPullParser::Event::kEndElement, sourceParser.next());
101 EXPECT_EQ(std::u16string(u"resources"), sourceParser.getElementName());
102
103 EXPECT_EQ(XmlPullParser::Event::kEndDocument, sourceParser.next());
104 }
105
106 } // namespace aapt
107