Home | History | Annotate | Download | only in aapt2
      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 #ifndef AAPT_MOCK_RESOLVER_H
     18 #define AAPT_MOCK_RESOLVER_H
     19 
     20 #include "Maybe.h"
     21 #include "Resolver.h"
     22 #include "Resource.h"
     23 #include "ResourceTable.h"
     24 #include "ResourceTableResolver.h"
     25 #include "ResourceValues.h"
     26 #include "StringPiece.h"
     27 
     28 #include <map>
     29 #include <string>
     30 
     31 namespace aapt {
     32 
     33 struct MockResolver : public IResolver {
     34     MockResolver(const std::shared_ptr<ResourceTable>& table,
     35                  const std::map<ResourceName, ResourceId>& items) :
     36             mResolver(std::make_shared<ResourceTableResolver>(
     37                     table, std::vector<std::shared_ptr<const android::AssetManager>>())),
     38             mAttr(false, android::ResTable_map::TYPE_ANY), mItems(items) {
     39     }
     40 
     41     virtual Maybe<ResourceId> findId(const ResourceName& name) override {
     42         Maybe<ResourceId> result = mResolver->findId(name);
     43         if (result) {
     44             return result;
     45         }
     46 
     47         const auto iter = mItems.find(name);
     48         if (iter != mItems.end()) {
     49             return iter->second;
     50         }
     51         return {};
     52     }
     53 
     54     virtual Maybe<Entry> findAttribute(const ResourceName& name) override {
     55         Maybe<Entry> tableResult = mResolver->findAttribute(name);
     56         if (tableResult) {
     57             return tableResult;
     58         }
     59 
     60         Maybe<ResourceId> result = findId(name);
     61         if (result) {
     62             if (name.type == ResourceType::kAttr) {
     63                 return Entry{ result.value(), &mAttr };
     64             } else {
     65                 return Entry{ result.value() };
     66             }
     67         }
     68         return {};
     69     }
     70 
     71     virtual Maybe<ResourceName> findName(ResourceId resId) override {
     72         Maybe<ResourceName> result = mResolver->findName(resId);
     73         if (result) {
     74             return result;
     75         }
     76 
     77         for (auto& p : mItems) {
     78             if (p.second == resId) {
     79                 return p.first;
     80             }
     81         }
     82         return {};
     83     }
     84 
     85 private:
     86     std::shared_ptr<ResourceTableResolver> mResolver;
     87     Attribute mAttr;
     88     std::map<ResourceName, ResourceId> mItems;
     89 };
     90 
     91 } // namespace aapt
     92 
     93 #endif // AAPT_MOCK_RESOLVER_H
     94