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 19 #include <algorithm> 20 21 #include "test/Test.h" 22 23 namespace aapt { 24 25 TEST(XmlIdCollectorTest, CollectsIds) { 26 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); 27 28 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF( 29 <View xmlns:android="http://schemas.android.com/apk/res/android" 30 android:id="@+id/foo" 31 text="@+id/bar"> 32 <SubView android:id="@+id/car" 33 class="@+id/bar"/> 34 </View>)EOF"); 35 36 XmlIdCollector collector; 37 ASSERT_TRUE(collector.Consume(context.get(), doc.get())); 38 39 EXPECT_EQ( 40 1, std::count(doc->file.exported_symbols.begin(), 41 doc->file.exported_symbols.end(), 42 SourcedResourceName{test::ParseNameOrDie("id/foo"), 3u})); 43 44 EXPECT_EQ( 45 1, std::count(doc->file.exported_symbols.begin(), 46 doc->file.exported_symbols.end(), 47 SourcedResourceName{test::ParseNameOrDie("id/bar"), 3u})); 48 49 EXPECT_EQ( 50 1, std::count(doc->file.exported_symbols.begin(), 51 doc->file.exported_symbols.end(), 52 SourcedResourceName{test::ParseNameOrDie("id/car"), 6u})); 53 } 54 55 TEST(XmlIdCollectorTest, DontCollectNonIds) { 56 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); 57 58 std::unique_ptr<xml::XmlResource> doc = 59 test::BuildXmlDom("<View foo=\"@+string/foo\"/>"); 60 61 XmlIdCollector collector; 62 ASSERT_TRUE(collector.Consume(context.get(), doc.get())); 63 64 EXPECT_TRUE(doc->file.exported_symbols.empty()); 65 } 66 67 TEST(XmlIdCollectorTest, ErrorOnInvalidIds) { 68 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); 69 70 std::unique_ptr<xml::XmlResource> doc = 71 test::BuildXmlDom("<View foo=\"@+id/foo$bar\"/>"); 72 73 XmlIdCollector collector; 74 ASSERT_FALSE(collector.Consume(context.get(), doc.get())); 75 } 76 77 } // namespace aapt 78