1/*
2 * Copyright (C) 2016 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 "test/Test.h"
18#include "xml/XmlActionExecutor.h"
19
20namespace aapt {
21namespace xml {
22
23TEST(XmlActionExecutorTest, BuildsAccessibleNestedPattern) {
24    XmlActionExecutor executor;
25    XmlNodeAction& manifestAction = executor[u"manifest"];
26    XmlNodeAction& applicationAction = manifestAction[u"application"];
27
28    Element* manifestEl = nullptr;
29    manifestAction.action([&](Element* manifest) -> bool {
30        manifestEl = manifest;
31        return true;
32    });
33
34    Element* applicationEl = nullptr;
35    applicationAction.action([&](Element* application) -> bool {
36        applicationEl = application;
37        return true;
38    });
39
40    std::unique_ptr<XmlResource> doc = test::buildXmlDom("<manifest><application /></manifest>");
41
42    StdErrDiagnostics diag;
43    ASSERT_TRUE(executor.execute(XmlActionExecutorPolicy::None, &diag, doc.get()));
44    ASSERT_NE(nullptr, manifestEl);
45    EXPECT_EQ(std::u16string(u"manifest"), manifestEl->name);
46
47    ASSERT_NE(nullptr, applicationEl);
48    EXPECT_EQ(std::u16string(u"application"), applicationEl->name);
49}
50
51TEST(XmlActionExecutorTest, FailsWhenUndefinedHierarchyExists) {
52    XmlActionExecutor executor;
53    executor[u"manifest"][u"application"];
54
55    std::unique_ptr<XmlResource> doc = test::buildXmlDom(
56            "<manifest><application /><activity /></manifest>");
57    StdErrDiagnostics diag;
58    ASSERT_FALSE(executor.execute(XmlActionExecutorPolicy::Whitelist, &diag, doc.get()));
59}
60
61} // namespace xml
62} // namespace aapt
63