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