Common.h revision a6fe345be955368a13aea76aefb4db821aad11df
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_TEST_COMMON_H
18#define AAPT_TEST_COMMON_H
19
20#include "ConfigDescription.h"
21#include "Debug.h"
22#include "ResourceTable.h"
23#include "ResourceUtils.h"
24#include "ValueVisitor.h"
25#include "io/File.h"
26#include "process/IResourceTableConsumer.h"
27#include "util/StringPiece.h"
28
29#include <gtest/gtest.h>
30#include <iostream>
31
32//
33// GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to fail to compile.
34//
35#define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
36#define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
37#define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
38#define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
39
40namespace aapt {
41namespace test {
42
43struct DummyDiagnosticsImpl : public IDiagnostics {
44    void error(const DiagMessage& message) override {
45        DiagMessageActual actual = message.build();
46        std::cerr << actual.source << ": error: " << actual.message << "." << std::endl;
47    }
48    void warn(const DiagMessage& message) override {
49        DiagMessageActual actual = message.build();
50        std::cerr << actual.source << ": warn: " << actual.message << "." << std::endl;
51    }
52    void note(const DiagMessage& message) override {}
53};
54
55inline ResourceName parseNameOrDie(const StringPiece16& str) {
56    ResourceNameRef ref;
57    bool result = ResourceUtils::tryParseReference(str, &ref);
58    assert(result && "invalid resource name");
59    return ref.toResourceName();
60}
61
62inline ConfigDescription parseConfigOrDie(const StringPiece& str) {
63    ConfigDescription config;
64    bool result = ConfigDescription::parse(str, &config);
65    assert(result && "invalid configuration");
66    return config;
67}
68
69template <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName,
70                                           const ConfigDescription& config) {
71    Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName));
72    if (result) {
73        ResourceEntry* entry = result.value().entry;
74        auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), config,
75                                     [](const ResourceConfigValue& a, const ConfigDescription& b)
76                                             -> bool {
77                                         return a.config < b;
78                                     });
79        if (iter != entry->values.end() && iter->config == config) {
80            return valueCast<T>(iter->value.get());
81        }
82    }
83    return nullptr;
84}
85
86template <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) {
87    return getValueForConfig<T>(table, resName, {});
88}
89
90class TestFile : public io::IFile {
91private:
92    Source mSource;
93
94public:
95    TestFile(const StringPiece& path) : mSource(path) {}
96
97    std::unique_ptr<io::IData> openAsData() override {
98        return {};
99    }
100
101    const Source& getSource() const override {
102        return mSource;
103    }
104};
105
106} // namespace test
107} // namespace aapt
108
109#endif /* AAPT_TEST_COMMON_H */
110