blob: 5aea77d67950614f42c455d1410d904c2b820efb [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 "ValueVisitor.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <string>
20
Adam Lesinskicacb28f2016-10-19 12:18:14 -070021#include "ResourceValues.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070022#include "test/Test.h"
23#include "util/Util.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025namespace aapt {
26
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070027struct SingleReferenceVisitor : public DescendingValueVisitor {
28 using DescendingValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 Reference* visited = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 void Visit(Reference* ref) override { visited = ref; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033};
34
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070035struct StyleVisitor : public DescendingValueVisitor {
36 using DescendingValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::list<Reference*> visited_refs;
39 Style* visited_style = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 void Visit(Reference* ref) override { visited_refs.push_back(ref); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 void Visit(Style* style) override {
44 visited_style = style;
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070045 DescendingValueVisitor::Visit(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047};
48
49TEST(ValueVisitorTest, VisitsReference) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 Reference ref(ResourceName{"android", ResourceType::kAttr, "foo"});
51 SingleReferenceVisitor visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 ref.Accept(&visitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 EXPECT_EQ(visitor.visited, &ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055}
56
57TEST(ValueVisitorTest, VisitsReferencesInStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 std::unique_ptr<Style> style =
59 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 .SetParent("android:style/foo")
61 .AddItem("android:attr/one", test::BuildReference("android:id/foo"))
62 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 StyleVisitor visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 style->Accept(&visitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 ASSERT_EQ(style.get(), visitor.visited_style);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 // Entry attribute references, plus the parent reference, plus one value
70 // reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 ASSERT_EQ(style->entries.size() + 2, visitor.visited_refs.size());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072}
73
74TEST(ValueVisitorTest, ValueCast) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::unique_ptr<Reference> ref = test::BuildReference("android:color/white");
76 EXPECT_NE(ValueCast<Reference>(ref.get()), nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 std::unique_ptr<Style> style =
79 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 .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);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085}
86
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087} // namespace aapt