/* * 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/StringPiece.h" #include "gtest/gtest.h" #include "ConfigDescription.h" #include "Debug.h" #include "ResourceTable.h" #include "ResourceUtils.h" #include "ValueVisitor.h" #include "io/File.h" #include "process/IResourceTableConsumer.h" // // GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to // fail to compile. // #define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v)) #define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v)) #define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v)) #define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v)) namespace aapt { namespace test { struct DummyDiagnosticsImpl : public IDiagnostics { void Log(Level level, DiagMessageActual& actual_msg) override { switch (level) { case Level::Note: return; case Level::Warn: std::cerr << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl; break; case Level::Error: std::cerr << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl; break; } } }; inline IDiagnostics* GetDiagnostics() { static DummyDiagnosticsImpl diag; return &diag; } inline ResourceName ParseNameOrDie(const android::StringPiece& str) { ResourceNameRef ref; CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name"; return ref.ToResourceName(); } inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) { ConfigDescription config; CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration"; return config; } template T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name, const 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 T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name, const 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 {}; } const Source& GetSource() const override { return source_; } private: DISALLOW_COPY_AND_ASSIGN(TestFile); Source source_; }; } // namespace test } // namespace aapt #endif /* AAPT_TEST_COMMON_H */