/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AAPT_TEST_COMMON_H #define AAPT_TEST_COMMON_H #include #include "android-base/logging.h" #include "android-base/macros.h" #include "androidfw/ConfigDescription.h" #include "androidfw/StringPiece.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include "Debug.h" #include "ResourceTable.h" #include "ResourceUtils.h" #include "ResourceValues.h" #include "ValueVisitor.h" #include "io/File.h" #include "process/IResourceTableConsumer.h" namespace aapt { namespace test { IDiagnostics* GetDiagnostics(); inline ResourceName ParseNameOrDie(const android::StringPiece& str) { ResourceNameRef ref; CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str; return ref.ToResourceName(); } inline android::ConfigDescription ParseConfigOrDie(const android::StringPiece& str) { android::ConfigDescription config; CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str; return config; } template T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name, const android::ConfigDescription& config, const android::StringPiece& product) { Maybe result = table->FindResource(ParseNameOrDie(res_name)); if (result) { ResourceConfigValue* config_value = result.value().entry->FindValue(config, product); if (config_value) { return ValueCast(config_value->value.get()); } } return nullptr; } template <> Value* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name, const android::ConfigDescription& config, const android::StringPiece& product); template T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name, const android::ConfigDescription& config) { return GetValueForConfigAndProduct(table, res_name, config, {}); } template T* GetValue(ResourceTable* table, const android::StringPiece& res_name) { return GetValueForConfig(table, res_name, {}); } class TestFile : public io::IFile { public: explicit TestFile(const android::StringPiece& path) : source_(path) {} std::unique_ptr OpenAsData() override { return {}; } std::unique_ptr OpenInputStream() override { return OpenAsData(); } const Source& GetSource() const override { return source_; } private: DISALLOW_COPY_AND_ASSIGN(TestFile); Source source_; }; } // namespace test // Workaround gtest bug (https://github.com/google/googletest/issues/443) // that does not select base class operator<< for derived class T. template typename std::enable_if::value, std::ostream&>::type operator<<( std::ostream& out, const T& value) { value.Print(&out); return out; } template std::ostream& operator<<(std::ostream&, const Item&); template std::ostream& operator<<(std::ostream&, const Reference&); template std::ostream& operator<<(std::ostream&, const Id&); template std::ostream& operator<<(std::ostream&, const RawString&); template std::ostream& operator<<(std::ostream&, const String&); template std::ostream& operator<<(std::ostream&, const StyledString&); template std::ostream& operator<<(std::ostream&, const FileReference&); template std::ostream& operator<<(std::ostream&, const BinaryPrimitive&); template std::ostream& operator<<(std::ostream&, const Attribute&); template std::ostream& operator<<