summaryrefslogtreecommitdiff
path: root/tests/unittest/ICUEnvironment.h
blob: 8fd285ff74927ad8c0c52e0d81626d5397f9e13a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINIKIN_TEST_ICU_ENVIRONMENT_H
#define MINIKIN_TEST_ICU_ENVIRONMENT_H

// low level file access for mapping ICU data
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <cutils/log.h>
#include <gtest/gtest.h>
#include <unicode/uclean.h>
#include <unicode/udata.h>

namespace minikin {

class ICUEnvironment : public testing::Environment {
public:
    ICUEnvironment() : testing::Environment(), mData(nullptr), mSize(0) {}

    void* mData;
    size_t mSize;

    virtual void SetUp() override {
        const char* fn = "/apex/com.android.i18n/etc/icu/" U_ICUDATA_NAME ".dat";
        int fd = open(fn, O_RDONLY);
        LOG_ALWAYS_FATAL_IF(fd == -1);
        struct stat sb;
        LOG_ALWAYS_FATAL_IF(fstat(fd, &sb) != 0);

        mSize = sb.st_size;
        void* mData = mmap(nullptr, mSize, PROT_READ, MAP_SHARED, fd, 0);
        close(fd);

        UErrorCode errorCode = U_ZERO_ERROR;
        udata_setCommonData(mData, &errorCode);
        LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode));

        errorCode = U_ZERO_ERROR;
        u_init(&errorCode);
        LOG_ALWAYS_FATAL_IF(U_FAILURE(errorCode));
    }

    virtual void TearDown() override {
        u_cleanup();
        munmap(mData, mSize);
    }
};

}  // namespace minikin
#endif  //  MINIKIN_TEST_ICU_ENVIRONMENT_H