blob: 3d1d5f5209f2a5a28249d78acd7f2c2b5224cdb4 [file] [log] [blame]
Adam Lesinski60293192014-10-21 18:36:42 -07001/*
Adam Lesinski7a37b742016-10-12 14:05:55 -07002 * Copyright (C) 2016 The Android Open Source Project
Adam Lesinski60293192014-10-21 18:36:42 -07003 *
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#include "TestHelpers.h"
18
19#include <androidfw/ResourceTypes.h>
Adam Lesinski60293192014-10-21 18:36:42 -070020#include <gtest/gtest.h>
Adam Lesinski7a37b742016-10-12 14:05:55 -070021#include <unistd.h>
22#include <utils/String8.h>
23
24std::string TestSourceDir() {
25 const char* dir = getenv("ANDROID_BUILD_TOP");
26 LOG_ALWAYS_FATAL_IF(dir == nullptr, "Environment variable ANDROID_BUILD_TOP must be set");
27 std::string testdir = std::string(dir) + "/frameworks/base/libs/androidfw/tests/data";
28
29 // Check that the directory exists.
30 struct stat filestat;
31 LOG_ALWAYS_FATAL_IF(stat(testdir.c_str(), &filestat) != 0, "test data path '%s' does not exist",
32 testdir.c_str());
33 return testdir;
34}
Adam Lesinski60293192014-10-21 18:36:42 -070035
36namespace android {
37
Adam Lesinski7a37b742016-10-12 14:05:55 -070038::testing::AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
39 const char* expected_str) {
40 Res_value val;
41 ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
42 if (block < 0) {
43 return ::testing::AssertionFailure() << "could not find resource";
44 }
Adam Lesinski60293192014-10-21 18:36:42 -070045
Adam Lesinski7a37b742016-10-12 14:05:55 -070046 if (val.dataType != Res_value::TYPE_STRING) {
47 return ::testing::AssertionFailure() << "resource is not a string";
48 }
Adam Lesinski60293192014-10-21 18:36:42 -070049
Adam Lesinski7a37b742016-10-12 14:05:55 -070050 const ResStringPool* pool = table.getTableStringBlock(block);
51 if (pool == NULL) {
52 return ::testing::AssertionFailure() << "table has no string pool for block " << block;
53 }
Adam Lesinski60293192014-10-21 18:36:42 -070054
Adam Lesinski7a37b742016-10-12 14:05:55 -070055 const String8 actual_str = pool->string8ObjectAt(val.data);
56 if (String8(expected_str) != actual_str) {
57 return ::testing::AssertionFailure() << actual_str.string();
58 }
59 return ::testing::AssertionSuccess() << actual_str.string();
Adam Lesinski60293192014-10-21 18:36:42 -070060}
61
Adam Lesinski7a37b742016-10-12 14:05:55 -070062} // namespace android