11ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski/*
21ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
31ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
41ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
51ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * you may not use this file except in compliance with the License.
61ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * You may obtain a copy of the License at
71ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
81ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
91ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski *
101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Unless required by applicable law or agreed to in writing, software
111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * See the License for the specific language governing permissions and
141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * limitations under the License.
151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski */
161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#ifndef AAPT_TEST_COMMON_H
181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#define AAPT_TEST_COMMON_H
191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "ConfigDescription.h"
219ba47d813075fcb05c5e1532c137c93b394631cbAdam Lesinski#include "Debug.h"
221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "ResourceTable.h"
231ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "ResourceUtils.h"
241ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "ValueVisitor.h"
25a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski#include "io/File.h"
261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "process/IResourceTableConsumer.h"
271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "util/StringPiece.h"
281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
291ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include <gtest/gtest.h>
301ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include <iostream>
311ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
321ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski//
331ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to fail to compile.
341ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski//
351ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
361ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
371ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
381ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
391ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
401ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace aapt {
411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace test {
421ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskistruct DummyDiagnosticsImpl : public IDiagnostics {
44cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski    void log(Level level, DiagMessageActual& actualMsg) override {
45cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski        switch (level) {
46cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski        case Level::Note:
47cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski            return;
48cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski
49cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski        case Level::Warn:
50cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski            std::cerr << actualMsg.source << ": warn: " << actualMsg.message << "." << std::endl;
51cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski            break;
52cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski
53cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski        case Level::Error:
54cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski            std::cerr << actualMsg.source << ": error: " << actualMsg.message << "." << std::endl;
55cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski            break;
56cc5609d8e484ec82ef1dced793af7f674f059b1cAdam Lesinski        }
571ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    }
581ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski};
591ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
60e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinskiinline IDiagnostics* getDiagnostics() {
61e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski    static DummyDiagnosticsImpl diag;
62e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski    return &diag;
63e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski}
64e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski
651ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiinline ResourceName parseNameOrDie(const StringPiece16& str) {
661ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ResourceNameRef ref;
671ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    bool result = ResourceUtils::tryParseReference(str, &ref);
681ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    assert(result && "invalid resource name");
691ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    return ref.toResourceName();
701ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
711ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
721ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiinline ConfigDescription parseConfigOrDie(const StringPiece& str) {
731ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    ConfigDescription config;
741ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    bool result = ConfigDescription::parse(str, &config);
751ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    assert(result && "invalid configuration");
761ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    return config;
771ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
781ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
79e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinskitemplate <typename T> T* getValueForConfigAndProduct(ResourceTable* table,
80e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski                                                     const StringPiece16& resName,
81e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski                                                     const ConfigDescription& config,
82e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski                                                     const StringPiece& product) {
831ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName));
841ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    if (result) {
85e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski        ResourceConfigValue* configValue = result.value().entry->findValue(config, product);
86e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski        if (configValue) {
87e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski            return valueCast<T>(configValue->value.get());
881ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski        }
891ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    }
901ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    return nullptr;
911ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
921ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
93e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinskitemplate <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName,
94e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski                                           const ConfigDescription& config) {
95e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski    return getValueForConfigAndProduct<T>(table, resName, config, {});
96e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski}
97e4bb9eb5af5b0899dc0921d5580220b20e15bd5aAdam Lesinski
981ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskitemplate <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) {
991ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski    return getValueForConfig<T>(table, resName, {});
1001ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}
1011ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
102a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinskiclass TestFile : public io::IFile {
103a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinskiprivate:
104a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    Source mSource;
105a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski
106a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinskipublic:
107a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    TestFile(const StringPiece& path) : mSource(path) {}
108a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski
109a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    std::unique_ptr<io::IData> openAsData() override {
110a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski        return {};
111a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    }
112a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski
113a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    const Source& getSource() const override {
114a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski        return mSource;
115a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski    }
116a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski};
117a6fe345be955368a13aea76aefb4db821aad11dfAdam Lesinski
1181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} // namespace test
1191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} // namespace aapt
1201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
1211ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#endif /* AAPT_TEST_COMMON_H */
122