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