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