• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "global_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "test_common.h"
21 #include "utils/errors.h"
22 #include "utils/string_utils.h"
23 
24 #define private public
25 
26 #include "global.h"
27 
28 using namespace OHOS::Global::Resource;
29 using namespace testing::ext;
30 
31 class GlobalTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34 
35     static void TearDownTestCase(void);
36 
37     void SetUp();
38 
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void GlobalTest::SetUpTestCase(void)
43 {
44     // step 1: input testsuit setup step
45     g_logLevel = LOG_DEBUG;
46 }
47 
TearDownTestCase()48 void GlobalTest::TearDownTestCase()
49 {
50     // step 2: input testsuit teardown step
51 }
52 
SetUp()53 void GlobalTest::SetUp()
54 {
55     // step 3: input testcase setup step
56 }
57 
TearDown()58 void GlobalTest::TearDown()
59 {
60     // step 4: input testcase teardown step
61 }
62 
GetResId(const std::string & name,ResType resType)63 int GetResId(const std::string &name, ResType resType)
64 {
65     auto rc = new ResConfigImpl;
66     rc->SetLocaleInfo("en", nullptr, "US");
67     std::string resPath = FormatFullPath(g_resFilePath);
68     const char *path = resPath.c_str();
69 
70     const HapResource *pResource = HapResource::LoadFromIndex(path, rc);
71 
72     int ret = pResource->GetIdByName(name.c_str(), resType);
73     delete pResource;
74     delete rc;
75     return ret;
76 }
77 
78 /*
79  * @tc.name: GlobalFuncTest001
80  * @tc.desc: Test GLOBAL_ConfigLanguage function, file case.
81  * @tc.type: FUNC
82  */
83 HWTEST_F(GlobalTest, GlobalFuncTest001, TestSize.Level1)
84 {
85     char lan[10];
86     char region[10];
87 
88     GLOBAL_ConfigLanguage("en-Latn-US");
89     int32_t ret = GLOBAL_GetLanguage(lan, 10);
90     ASSERT_EQ(OK, ret);
91     ASSERT_EQ(std::string("en"), lan);
92     ret = GLOBAL_GetRegion(region, 10);
93     ASSERT_EQ(OK, ret);
94     ASSERT_EQ(std::string("US"), region);
95 
96     GLOBAL_ConfigLanguage("en_Latn_US");
97     ret = GLOBAL_GetLanguage(lan, 10);
98     ASSERT_EQ(OK, ret);
99     ASSERT_EQ(std::string("en"), lan);
100     ret = GLOBAL_GetRegion(region, 10);
101     ASSERT_EQ(OK, ret);
102     ASSERT_EQ(std::string("US"), region);
103 
104     GLOBAL_ConfigLanguage("en-US");
105     ret = GLOBAL_GetLanguage(lan, 10);
106     ASSERT_EQ(OK, ret);
107     ASSERT_EQ(std::string("en"), lan);
108     ret = GLOBAL_GetRegion(region, 10);
109     ASSERT_EQ(OK, ret);
110     ASSERT_EQ(std::string("US"), region);
111 
112     GLOBAL_ConfigLanguage("en_US");
113     ret = GLOBAL_GetLanguage(lan, 10);
114     ASSERT_EQ(OK, ret);
115     ASSERT_EQ(std::string("en"), lan);
116     ret = GLOBAL_GetRegion(region, 10);
117     ASSERT_EQ(OK, ret);
118     ASSERT_EQ(std::string("US"), region);
119 
120     GLOBAL_ConfigLanguage("en");
121     ret = GLOBAL_GetLanguage(lan, 10);
122     ASSERT_EQ(OK, ret);
123     ASSERT_EQ(std::string("en"), lan);
124 }
125 
126 /*
127  * @tc.name: GlobalFuncTest002
128  * @tc.desc: Test GLOBAL_GetValueByName function, file case.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(GlobalTest, GlobalFuncTest002, TestSize.Level1)
132 {
133     GLOBAL_ConfigLanguage("en_Latn_US");
134     char *values = nullptr;
135     int32_t ret = GLOBAL_GetValueByName("app_name", FormatFullPath(g_resFilePath).c_str(), &values);
136     // when ret != OK , values has been freed.
137     ASSERT_EQ(OK, ret);
138     EXPECT_EQ(std::string("App Name"), values);
139     free(values);
140 }
141 
142 /*
143  * @tc.name: GlobalFuncTest003
144  * @tc.desc: Test GLOBAL_GetValueById function, file case.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(GlobalTest, GlobalFuncTest003, TestSize.Level1)
148 {
149     GLOBAL_ConfigLanguage("en_Latn_US");
150     char *values = nullptr;
151     int id = GetResId("app_name", ResType::STRING);
152     ASSERT_TRUE(id > 0);
153     int32_t ret = GLOBAL_GetValueById(static_cast<uint32_t>(id), FormatFullPath(g_resFilePath).c_str(), &values);
154     // when ret != OK , values has been freed.
155     ASSERT_EQ(OK, ret);
156     EXPECT_EQ(std::string("App Name"), values);
157     free(values);
158 }
159