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 <climits>
17 #include <gtest/gtest.h>
18 #include "UnitDateTimeRecallTest.h"
19
20 using namespace std;
21 using namespace testing::ext;
22 namespace OHOS {
23 using namespace I18N;
24 using namespace testing;
25
26 class DateTimeTest : public ::testing::TestWithParam<LocaleInfo> {};
27
28 class DateTimeLocaleRecallTest : public testing::Test {
29 public:
30 static const int INDEX_NEG_ONE = -1;
31
32 public:
33 static const int INDEX_MAX_WEEK = 7;
34
35 public:
36 static const int INDEX_MAX_MONTH = 12;
37 };
38
39 class DateTimePreformanceTest : public testing::Test {
40 public:
41 static const int LOOP_NUM = 1000;
42
43 public:
44 static constexpr float BASELINE = 0.5;
45 };
46
47 /* *
48 * get locale index
49 *
50 * expectResult return locale index
51 */
GetLocaleIndex(LocaleInfo locale,vector<LocaleInfo> locales)52 static int GetLocaleIndex(LocaleInfo locale, vector<LocaleInfo> locales)
53 {
54 for (unsigned int index = 0; index < locales.size(); index++) {
55 if (locale == locales[index]) {
56 return index;
57 }
58 }
59 return -1;
60 }
61
62 /* *
63 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_DATETIMEFORMAT_LOCALE_0100
64 * @tc.name test datetime locale recall,locale is LocaleInfo en,ZZ
65 * @tc.desc [C- SOFTWARE -0200]
66 */
67 HWTEST_F(DateTimeLocaleRecallTest, GLOBAL_DateTimeFormat_LocaleRecall_0100, Function | MediumTest | Level0)
68 {
69 LocaleInfo *locale = new LocaleInfo("en", "ZZ");
70 DateTimeFormat *datetime = new DateTimeFormat(HOUR12_MINUTE_SECOND, *locale);
71 std::string formatWeekName = datetime->GetWeekName(0, DateTimeDataType::FORMAT_ABBR);
72 LocaleInfo *localeRecall = new LocaleInfo("en", "", "US");
73 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
74 if (localeIndex >= 0) {
75 EXPECT_EQ(formatWeekName, g_formatAbbrDayNames[localeIndex][0]);
76 }
77 delete localeRecall;
78 delete datetime;
79 delete locale;
80 }
81
82 /* *
83 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_DATETIMEFORMAT_LOCALE_0200
84 * @tc.name test datetime locale recall,locale is LocaleInfo zh,ZZ
85 * @tc.desc [C- SOFTWARE -0200]
86 */
87 HWTEST_F(DateTimeLocaleRecallTest, GLOBAL_DateTimeFormat_LocaleRecall_0200, Function | MediumTest | Level0)
88 {
89 LocaleInfo *locale = new LocaleInfo("zh", "ZZ");
90 DateTimeFormat *datetime = new DateTimeFormat(HOUR12_MINUTE_SECOND, *locale);
91 std::string formatWeekName = datetime->GetWeekName(0, DateTimeDataType::FORMAT_ABBR);
92 LocaleInfo *localeRecall = new LocaleInfo("zh", "", "");
93 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
94 if (localeIndex >= 0) {
95 EXPECT_EQ(formatWeekName, g_formatAbbrDayNames[localeIndex][0]) << "index: " << localeIndex << std::endl;
96 }
97 delete localeRecall;
98 delete datetime;
99 delete locale;
100 }
101
102 /* *
103 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_DATETIMEFORMAT_LOCALE_0300
104 * @tc.name test datetime locale recall,locale is LocaleInfo zh,Hans,ZZ
105 * @tc.desc [C- SOFTWARE -0200]
106 */
107 HWTEST_F(DateTimeLocaleRecallTest, GLOBAL_DateTimeFormat_LocaleRecall_0300, Function | MediumTest | Level0)
108 {
109 LocaleInfo *locale = new LocaleInfo("zh", "Hans", "ZZ");
110 DateTimeFormat *datetime = new DateTimeFormat(HOUR12_MINUTE_SECOND, *locale);
111 std::string formatWeekName = datetime->GetWeekName(0, DateTimeDataType::FORMAT_ABBR);
112 LocaleInfo *localeRecall = new LocaleInfo("zh", "Hans", "");
113 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
114 if (localeIndex >= 0) {
115 EXPECT_EQ(formatWeekName, g_formatAbbrDayNames[localeIndex][0]) << "index: " << localeIndex << std::endl;
116 }
117 delete localeRecall;
118 delete datetime;
119 delete locale;
120 }
121
122 /* *
123 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_DATETIMEFORMAT_LOCALE_0400
124 * @tc.name test datetime locale recall,locale is LocaleInfo zz
125 * @tc.desc [C- SOFTWARE -0200]
126 */
127 HWTEST_F(DateTimeLocaleRecallTest, GLOBAL_DateTimeFormat_LocaleRecall_0400, Function | MediumTest | Level0)
128 {
129 LocaleInfo *locale = new LocaleInfo("zz", "");
130 DateTimeFormat *datetime = new DateTimeFormat(HOUR12_MINUTE_SECOND, *locale);
131 std::string formatWeekName = datetime->GetWeekName(0, DateTimeDataType::FORMAT_ABBR);
132 LocaleInfo *localeRecall = new LocaleInfo("en", "US");
133 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
134 if (localeIndex >= 0) {
135 EXPECT_EQ(formatWeekName, g_formatAbbrDayNames[localeIndex][0]) << "index: " << localeIndex << std::endl;
136 }
137 delete localeRecall;
138 delete datetime;
139 delete locale;
140 }
141 } // namespace OHOS