blob: 11eab3323576ee57f3afd621880373637deafcd8 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070017#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "ValueVisitor.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070019#include "test/Test.h"
20#include "util/Util.h"
21
22#include <string>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023
24namespace aapt {
25
26struct 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
36struct 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
52TEST(ValueVisitorTest, VisitsReference) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070053 Reference ref(ResourceName{"android", ResourceType::kAttr, "foo"});
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 SingleReferenceVisitor visitor;
55 ref.accept(&visitor);
56
57 EXPECT_EQ(visitor.visited, &ref);
58}
59
60TEST(ValueVisitorTest, VisitsReferencesInStyle) {
61 std::unique_ptr<Style> style = test::StyleBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070062 .setParent("@android:style/foo")
63 .addItem("@android:attr/one", test::buildReference("@android:id/foo"))
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 .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
75TEST(ValueVisitorTest, ValueCast) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076 std::unique_ptr<Reference> ref = test::buildReference("@android:color/white");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 EXPECT_NE(valueCast<Reference>(ref.get()), nullptr);
78
79 std::unique_ptr<Style> style = test::StyleBuilder()
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080 .addItem("@android:attr/foo", test::buildReference("@android:color/black"))
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 .build();
82 EXPECT_NE(valueCast<Style>(style.get()), nullptr);
83 EXPECT_EQ(valueCast<Reference>(style.get()), nullptr);
84}
85
86} // namespace aapt