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 "xml/XmlActionExecutor.h"
18
19#include "test/Test.h"
20
21using ::testing::NotNull;
22
23namespace aapt {
24namespace xml {
25
26TEST(XmlActionExecutorTest, BuildsAccessibleNestedPattern) {
27  XmlActionExecutor executor;
28  XmlNodeAction& manifest_action = executor["manifest"];
29  XmlNodeAction& application_action = manifest_action["application"];
30
31  Element* manifest_el = nullptr;
32  manifest_action.Action([&](Element* manifest) -> bool {
33    manifest_el = manifest;
34    return true;
35  });
36
37  Element* application_el = nullptr;
38  application_action.Action([&](Element* application) -> bool {
39    application_el = application;
40    return true;
41  });
42
43  std::unique_ptr<XmlResource> doc =
44      test::BuildXmlDom("<manifest><application /></manifest>");
45
46  StdErrDiagnostics diag;
47  ASSERT_TRUE(executor.Execute(XmlActionExecutorPolicy::kNone, &diag, doc.get()));
48  ASSERT_THAT(manifest_el, NotNull());
49  EXPECT_EQ(std::string("manifest"), manifest_el->name);
50
51  ASSERT_THAT(application_el, NotNull());
52  EXPECT_EQ(std::string("application"), application_el->name);
53}
54
55TEST(XmlActionExecutorTest, FailsWhenUndefinedHierarchyExists) {
56  XmlActionExecutor executor;
57  executor["manifest"]["application"];
58
59  std::unique_ptr<XmlResource> doc;
60  StdErrDiagnostics diag;
61
62  doc = test::BuildXmlDom("<manifest><application /><activity /></manifest>");
63  ASSERT_FALSE(executor.Execute(XmlActionExecutorPolicy::kWhitelist, &diag, doc.get()));
64
65  doc = test::BuildXmlDom("<manifest><application><activity /></application></manifest>");
66  ASSERT_FALSE(executor.Execute(XmlActionExecutorPolicy::kWhitelist, &diag, doc.get()));
67}
68
69}  // namespace xml
70}  // namespace aapt
71