Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "ValueVisitor.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
| 20 | #include "util/Util.h" |
| 21 | |
| 22 | #include <string> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | struct SingleReferenceVisitor : public ValueVisitor { |
| 27 | using ValueVisitor::visit; |
| 28 | |
| 29 | Reference* visited = nullptr; |
| 30 | |
| 31 | void visit(Reference* ref) override { |
| 32 | visited = ref; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | struct StyleVisitor : public ValueVisitor { |
| 37 | using ValueVisitor::visit; |
| 38 | |
| 39 | std::list<Reference*> visitedRefs; |
| 40 | Style* visitedStyle = nullptr; |
| 41 | |
| 42 | void visit(Reference* ref) override { |
| 43 | visitedRefs.push_back(ref); |
| 44 | } |
| 45 | |
| 46 | void visit(Style* style) override { |
| 47 | visitedStyle = style; |
| 48 | ValueVisitor::visit(style); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | TEST(ValueVisitorTest, VisitsReference) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 53 | Reference ref(ResourceName{"android", ResourceType::kAttr, "foo"}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | SingleReferenceVisitor visitor; |
| 55 | ref.accept(&visitor); |
| 56 | |
| 57 | EXPECT_EQ(visitor.visited, &ref); |
| 58 | } |
| 59 | |
| 60 | TEST(ValueVisitorTest, VisitsReferencesInStyle) { |
| 61 | std::unique_ptr<Style> style = test::StyleBuilder() |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 62 | .setParent("@android:style/foo") |
| 63 | .addItem("@android:attr/one", test::buildReference("@android:id/foo")) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | .build(); |
| 65 | |
| 66 | StyleVisitor visitor; |
| 67 | style->accept(&visitor); |
| 68 | |
| 69 | ASSERT_EQ(style.get(), visitor.visitedStyle); |
| 70 | |
| 71 | // Entry attribute references, plus the parent reference, plus one value reference. |
| 72 | ASSERT_EQ(style->entries.size() + 2, visitor.visitedRefs.size()); |
| 73 | } |
| 74 | |
| 75 | TEST(ValueVisitorTest, ValueCast) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 76 | std::unique_ptr<Reference> ref = test::buildReference("@android:color/white"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | EXPECT_NE(valueCast<Reference>(ref.get()), nullptr); |
| 78 | |
| 79 | std::unique_ptr<Style> style = test::StyleBuilder() |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 80 | .addItem("@android:attr/foo", test::buildReference("@android:color/black")) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | .build(); |
| 82 | EXPECT_NE(valueCast<Style>(style.get()), nullptr); |
| 83 | EXPECT_EQ(valueCast<Reference>(style.get()), nullptr); |
| 84 | } |
| 85 | |
| 86 | } // namespace aapt |