XmlIdCollector_test.cpp revision 58a20a6482a56a262fd83a617482641e3a981db1
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 "compile/XmlIdCollector.h"
18#include "test/Builders.h"
19#include "test/Context.h"
20
21#include <algorithm>
22#include <gtest/gtest.h>
23
24namespace aapt {
25
26TEST(XmlIdCollectorTest, CollectsIds) {
27    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
28
29    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
30            <View xmlns:android="http://schemas.android.com/apk/res/android"
31                  android:id="@+id/foo"
32                  text="@+id/bar">
33              <SubView android:id="@+id/car"
34                       class="@+id/bar"/>
35            </View>)EOF");
36
37    XmlIdCollector collector;
38    ASSERT_TRUE(collector.consume(context.get(), doc.get()));
39
40    EXPECT_EQ(1, std::count(doc->file.exportedSymbols.begin(), doc->file.exportedSymbols.end(),
41                             SourcedResourceName{ test::parseNameOrDie("id/foo"), 3u }));
42
43    EXPECT_EQ(1, std::count(doc->file.exportedSymbols.begin(), doc->file.exportedSymbols.end(),
44                             SourcedResourceName{ test::parseNameOrDie("id/bar"), 3u }));
45
46    EXPECT_EQ(1, std::count(doc->file.exportedSymbols.begin(), doc->file.exportedSymbols.end(),
47                             SourcedResourceName{ test::parseNameOrDie("id/car"), 6u }));
48}
49
50TEST(XmlIdCollectorTest, DontCollectNonIds) {
51    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
52
53    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom("<View foo=\"@+string/foo\"/>");
54
55    XmlIdCollector collector;
56    ASSERT_TRUE(collector.consume(context.get(), doc.get()));
57
58    EXPECT_TRUE(doc->file.exportedSymbols.empty());
59}
60
61} // namespace aapt
62