Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 1 | /* |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 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 | #include "TestHelpers.h" |
| 18 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame^] | 21 | #include "android-base/logging.h" |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame^] | 25 | static std::string sTestDataPath; |
| 26 | |
| 27 | void SetTestDataPath(const std::string& path) { sTestDataPath = path; } |
| 28 | |
| 29 | const std::string& GetTestDataPath() { |
| 30 | CHECK(!sTestDataPath.empty()) << "no test data path set."; |
| 31 | return sTestDataPath; |
| 32 | } |
| 33 | |
| 34 | ::testing::AssertionResult IsStringEqual(const ResTable& table, |
| 35 | uint32_t resource_id, |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 36 | const char* expected_str) { |
| 37 | Res_value val; |
| 38 | ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG); |
| 39 | if (block < 0) { |
| 40 | return ::testing::AssertionFailure() << "could not find resource"; |
| 41 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 43 | if (val.dataType != Res_value::TYPE_STRING) { |
| 44 | return ::testing::AssertionFailure() << "resource is not a string"; |
| 45 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 47 | const ResStringPool* pool = table.getTableStringBlock(block); |
| 48 | if (pool == NULL) { |
Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame^] | 49 | return ::testing::AssertionFailure() |
| 50 | << "table has no string pool for block " << block; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 51 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 53 | const String8 actual_str = pool->string8ObjectAt(val.data); |
| 54 | if (String8(expected_str) != actual_str) { |
| 55 | return ::testing::AssertionFailure() << actual_str.string(); |
| 56 | } |
| 57 | return ::testing::AssertionSuccess() << actual_str.string(); |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 60 | } // namespace android |