blob: d39854e5fe4ebaca7ab0efdb2d6b69eaf1964fc0 [file] [log] [blame]
Adam Lesinskicc5609d2016-04-05 12:41:07 -07001/*
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
Adam Lesinskicc5609d2016-04-05 12:41:07 -070017#include "xml/XmlActionExecutor.h"
18
Adam Lesinskice5e56e22016-10-21 17:56:45 -070019#include "test/Test.h"
20
Adam Lesinskia45893a2017-05-30 15:19:02 -070021using ::testing::NotNull;
22
Adam Lesinskicc5609d2016-04-05 12:41:07 -070023namespace aapt {
24namespace xml {
25
26TEST(XmlActionExecutorTest, BuildsAccessibleNestedPattern) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027 XmlActionExecutor executor;
28 XmlNodeAction& manifest_action = executor["manifest"];
29 XmlNodeAction& application_action = manifest_action["application"];
Adam Lesinskicc5609d2016-04-05 12:41:07 -070030
Adam Lesinskice5e56e22016-10-21 17:56:45 -070031 Element* manifest_el = nullptr;
32 manifest_action.Action([&](Element* manifest) -> bool {
33 manifest_el = manifest;
34 return true;
35 });
Adam Lesinskicc5609d2016-04-05 12:41:07 -070036
Adam Lesinskice5e56e22016-10-21 17:56:45 -070037 Element* application_el = nullptr;
38 application_action.Action([&](Element* application) -> bool {
39 application_el = application;
40 return true;
41 });
Adam Lesinskicc5609d2016-04-05 12:41:07 -070042
Adam Lesinskice5e56e22016-10-21 17:56:45 -070043 std::unique_ptr<XmlResource> doc =
44 test::BuildXmlDom("<manifest><application /></manifest>");
Adam Lesinskicc5609d2016-04-05 12:41:07 -070045
Adam Lesinskice5e56e22016-10-21 17:56:45 -070046 StdErrDiagnostics diag;
Adam Lesinskia45893a2017-05-30 15:19:02 -070047 ASSERT_TRUE(executor.Execute(XmlActionExecutorPolicy::kNone, &diag, doc.get()));
48 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinskice5e56e22016-10-21 17:56:45 -070049 EXPECT_EQ(std::string("manifest"), manifest_el->name);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070050
Adam Lesinskia45893a2017-05-30 15:19:02 -070051 ASSERT_THAT(application_el, NotNull());
Adam Lesinskice5e56e22016-10-21 17:56:45 -070052 EXPECT_EQ(std::string("application"), application_el->name);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070053}
54
55TEST(XmlActionExecutorTest, FailsWhenUndefinedHierarchyExists) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070056 XmlActionExecutor executor;
57 executor["manifest"]["application"];
Adam Lesinskicc5609d2016-04-05 12:41:07 -070058
Adam Lesinskied37f482017-11-02 12:07:08 -070059 std::unique_ptr<XmlResource> doc;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070060 StdErrDiagnostics diag;
Adam Lesinskied37f482017-11-02 12:07:08 -070061
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>");
Adam Lesinskia45893a2017-05-30 15:19:02 -070066 ASSERT_FALSE(executor.Execute(XmlActionExecutorPolicy::kWhitelist, &diag, doc.get()));
Adam Lesinskicc5609d2016-04-05 12:41:07 -070067}
68
Adam Lesinskice5e56e22016-10-21 17:56:45 -070069} // namespace xml
70} // namespace aapt