| 1 | /* |
| 2 | * Copyright (C) 2015 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 "ValueVisitor.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "ResourceValues.h" |
| 22 | #include "test/Test.h" |
| 23 | #include "util/Util.h" |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | struct SingleReferenceVisitor : public DescendingValueVisitor { |
| 28 | using DescendingValueVisitor::Visit; |
| 29 | |
| 30 | Reference* visited = nullptr; |
| 31 | |
| 32 | void Visit(Reference* ref) override { visited = ref; } |
| 33 | }; |
| 34 | |
| 35 | struct StyleVisitor : public DescendingValueVisitor { |
| 36 | using DescendingValueVisitor::Visit; |
| 37 | |
| 38 | std::list<Reference*> visited_refs; |
| 39 | Style* visited_style = nullptr; |
| 40 | |
| 41 | void Visit(Reference* ref) override { visited_refs.push_back(ref); } |
| 42 | |
| 43 | void Visit(Style* style) override { |
| 44 | visited_style = style; |
| 45 | DescendingValueVisitor::Visit(style); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | TEST(ValueVisitorTest, VisitsReference) { |
| 50 | Reference ref(ResourceName{"android", ResourceType::kAttr, "foo"}); |
| 51 | SingleReferenceVisitor visitor; |
| 52 | ref.Accept(&visitor); |
| 53 | |
| 54 | EXPECT_EQ(visitor.visited, &ref); |
| 55 | } |
| 56 | |
| 57 | TEST(ValueVisitorTest, VisitsReferencesInStyle) { |
| 58 | std::unique_ptr<Style> style = |
| 59 | test::StyleBuilder() |
| 60 | .SetParent("android:style/foo") |
| 61 | .AddItem("android:attr/one", test::BuildReference("android:id/foo")) |
| 62 | .Build(); |
| 63 | |
| 64 | StyleVisitor visitor; |
| 65 | style->Accept(&visitor); |
| 66 | |
| 67 | ASSERT_EQ(style.get(), visitor.visited_style); |
| 68 | |
| 69 | // Entry attribute references, plus the parent reference, plus one value |
| 70 | // reference. |
| 71 | ASSERT_EQ(style->entries.size() + 2, visitor.visited_refs.size()); |
| 72 | } |
| 73 | |
| 74 | TEST(ValueVisitorTest, ValueCast) { |
| 75 | std::unique_ptr<Reference> ref = test::BuildReference("android:color/white"); |
| 76 | EXPECT_NE(ValueCast<Reference>(ref.get()), nullptr); |
| 77 | |
| 78 | std::unique_ptr<Style> style = |
| 79 | test::StyleBuilder() |
| 80 | .AddItem("android:attr/foo", |
| 81 | test::BuildReference("android:color/black")) |
| 82 | .Build(); |
| 83 | EXPECT_NE(ValueCast<Style>(style.get()), nullptr); |
| 84 | EXPECT_EQ(ValueCast<Reference>(style.get()), nullptr); |
| 85 | } |
| 86 | |
| 87 | } // namespace aapt |