Home | History | Annotate | Download | only in optimize
      1 /*
      2  * Copyright (C) 2018 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 "optimize/ResourcePathShortener.h"
     18 
     19 #include "ResourceTable.h"
     20 #include "test/Test.h"
     21 
     22 using ::aapt::test::GetValue;
     23 using ::testing::Not;
     24 using ::testing::NotNull;
     25 using ::testing::Eq;
     26 
     27 namespace aapt {
     28 
     29 TEST(ResourcePathShortenerTest, FileRefPathsChangedInResourceTable) {
     30   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
     31 
     32   std::unique_ptr<ResourceTable> table =
     33       test::ResourceTableBuilder()
     34           .AddFileReference("android:drawable/xmlfile", "res/drawables/xmlfile.xml")
     35           .AddFileReference("android:drawable/xmlfile2", "res/drawables/xmlfile2.xml")
     36           .AddString("android:string/string", "res/should/still/be/the/same.png")
     37           .Build();
     38 
     39   std::map<std::string, std::string> path_map;
     40   ASSERT_TRUE(ResourcePathShortener(path_map).Consume(context.get(), table.get()));
     41 
     42   // Expect that the path map is populated
     43   ASSERT_THAT(path_map.find("res/drawables/xmlfile.xml"), Not(Eq(path_map.end())));
     44   ASSERT_THAT(path_map.find("res/drawables/xmlfile2.xml"), Not(Eq(path_map.end())));
     45 
     46   // The file paths were changed
     47   EXPECT_THAT(path_map.at("res/drawables/xmlfile.xml"), Not(Eq("res/drawables/xmlfile.xml")));
     48   EXPECT_THAT(path_map.at("res/drawables/xmlfile2.xml"), Not(Eq("res/drawables/xmlfile2.xml")));
     49 
     50   // Different file paths should remain different
     51   EXPECT_THAT(path_map["res/drawables/xmlfile.xml"],
     52               Not(Eq(path_map["res/drawables/xmlfile2.xml"])));
     53 
     54   FileReference* ref =
     55       GetValue<FileReference>(table.get(), "android:drawable/xmlfile");
     56   ASSERT_THAT(ref, NotNull());
     57   // The map correctly points to the new location of the file
     58   EXPECT_THAT(path_map["res/drawables/xmlfile.xml"], Eq(*ref->path));
     59 
     60   // Strings should not be affected, only file paths
     61   EXPECT_THAT(
     62       *GetValue<String>(table.get(), "android:string/string")->value,
     63               Eq("res/should/still/be/the/same.png"));
     64   EXPECT_THAT(path_map.find("res/should/still/be/the/same.png"), Eq(path_map.end()));
     65 }
     66 
     67 }   // namespace aapt
     68