1 /*
2  * Copyright (C) 2017 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 #include "CommonHelpers.h"
18 
19 #include <iostream>
20 
21 #include "android-base/file.h"
22 #include "android-base/logging.h"
23 #include "android-base/strings.h"
24 
25 namespace android {
26 
27 static std::string sTestDataPath;
28 
InitializeTest(int * argc,char ** argv)29 void InitializeTest(int* argc, char** argv) {
30   // Set the default test data path to be the executable path directory + data.
31   SetTestDataPath(base::GetExecutableDirectory() + "/tests/data");
32 
33   for (int i = 1; i < *argc; i++) {
34     const std::string arg = argv[i];
35     if (base::StartsWith(arg, "--testdata=")) {
36       SetTestDataPath(arg.substr(strlen("--testdata=")));
37       for (int j = i; j != *argc; j++) {
38         argv[j] = argv[j + 1];
39       }
40       --(*argc);
41       --i;
42     } else if (arg == "-h" || arg == "--help") {
43       std::cerr << "\nAdditional options specific to this test:\n"
44                    "  --testdata=[PATH]\n"
45                    "      Specify the location of test data used within the tests.\n";
46       exit(1);
47     }
48   }
49 }
50 
SetTestDataPath(const std::string & path)51 void SetTestDataPath(const std::string& path) {
52   sTestDataPath = path;
53 }
54 
GetTestDataPath()55 const std::string& GetTestDataPath() {
56   CHECK(!sTestDataPath.empty()) << "no test data path set.";
57   return sTestDataPath;
58 }
59 
GetStringFromPool(const ResStringPool * pool,uint32_t idx)60 std::string GetStringFromPool(const ResStringPool* pool, uint32_t idx) {
61   String8 str = pool->string8ObjectAt(idx);
62   return std::string(str.string(), str.length());
63 }
64 
65 }  // namespace android
66