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 MINIKIN_TEST_ICU_ENVIRONMENT_H
18 #define MINIKIN_TEST_ICU_ENVIRONMENT_H
19 
20 // low level file access for mapping ICU data
21 #include <fcntl.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 
25 #include <cutils/log.h>
26 #include <gtest/gtest.h>
27 #include <unicode/uclean.h>
28 #include <unicode/udata.h>
29 
30 namespace minikin {
31 
32 class ICUEnvironment : public testing::Environment {
33 public:
ICUEnvironment()34     ICUEnvironment() : testing::Environment(), mData(nullptr), mSize(0) {}
35 
36     void* mData;
37     size_t mSize;
38 
SetUp()39     virtual void SetUp() override {
40         const char* fn = "/system/usr/icu/" U_ICUDATA_NAME ".dat";
41         int fd = open(fn, O_RDONLY);
42         LOG_ALWAYS_FATAL_IF(fd == -1);
43         struct stat sb;
44         LOG_ALWAYS_FATAL_IF(fstat(fd, &sb) != 0);
45 
46         mSize = sb.st_size;
47         void* mData = mmap(nullptr, mSize, PROT_READ, MAP_SHARED, fd, 0);
48         close(fd);
49 
50         UErrorCode errorCode = U_ZERO_ERROR;
51         udata_setCommonData(mData, &errorCode);
52         LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode));
53 
54         errorCode = U_ZERO_ERROR;
55         u_init(&errorCode);
56         LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode));
57     }
58 
TearDown()59     virtual void TearDown() override {
60         u_cleanup();
61         munmap(mData, mSize);
62     }
63 };
64 
65 }  // namespace minikin
66 #endif  //  MINIKIN_TEST_ICU_ENVIRONMENT_H
67