2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file Localization_testcases.cpp
18 * @author Marcin Kaminski (marcin.ka@samsung.com)
20 * @brief This file contains tests for localization related code.
23 #include <dpl/log/log.h>
24 #include <dpl/test/test_runner.h>
27 #include <LanguageTagsProvider.h>
29 RUNNER_TEST_GROUP_INIT(LanguageTagsProvider)
31 RUNNER_TEST(tagsFromSystemLocales)
33 LogInfo("Generating tags from system locales");
35 char* currlocals = vconf_get_str(VCONFKEY_LANGSET);
36 LogDebug("Locales fetched from system settings: " << currlocals);
37 RUNNER_ASSERT_MSG(!!currlocals, "NULL locales received from system");
38 int result = vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8");
39 LogDebug("Returned vconf set execution status: " << result);
40 RUNNER_ASSERT_MSG(result == 0, "Invalid value returned by vconf_set_str on setting locales");
42 /* Ensure that system locales where fetched */
43 LanguageTagsProviderSingleton::Instance().resetLanguageTags();
44 LogDebug("Language tags set based on current system locales");
46 LanguageTags ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
47 RUNNER_ASSERT_MSG(!ltlist.empty(), "Empty tag list returned");
49 /* Correct list generated from given locales should contain: "en-US", "en" and "" */
51 correct.push_back(L"en-US");
52 correct.push_back(L"en");
53 correct.push_back(L"");
55 RUNNER_ASSERT_MSG( correct==ltlist, "Received and expected language tags lists differ");
57 /* Restore system locales */
58 result = vconf_set_str(VCONFKEY_LANGSET, currlocals);
59 RUNNER_ASSERT_MSG(result == 0, "Invalid value returned by vconf_set_str on restoring locales");
60 LogDebug("System locales restored");
63 RUNNER_TEST(tagsFromGivenLocales)
65 LogInfo("Generating tags from given locales");
67 const char *locales1 = "it_IT.UTF-8", *locales2="en_GB";
69 LogDebug("Using locales with codepage: " << locales1);
70 LanguageTagsProviderSingleton::Instance().setLanguageTagsFromLocales(locales1);
71 LanguageTags ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
72 /* Correct list generated from given locales should contain: "it-IT", "it" and
73 * two default values: "en" and "" */
75 correct.push_back(L"it-IT");
76 correct.push_back(L"it");
77 correct.push_back(L"");
78 RUNNER_ASSERT_MSG(correct==ltlist, "Received and expected language tags lists differ");
80 LogDebug("Using locales without codepage: " << locales2);
81 LanguageTagsProviderSingleton::Instance().setLanguageTagsFromLocales(locales2);
82 ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
84 correct.push_back(L"en-GB");
85 correct.push_back(L"en");
86 correct.push_back(L"");
87 RUNNER_ASSERT_MSG(correct==ltlist, "Received and expected language tags lists differ");
90 RUNNER_TEST(tagsFromNullLocales)
92 LogInfo("Generating tags when NULL locales given");
94 LanguageTagsProviderSingleton::Instance().setLanguageTagsFromLocales(NULL);
95 LanguageTags ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
96 /* List with two values "en" and "" should be returned */
98 correct.push_back(L"");
99 RUNNER_ASSERT_MSG(correct==ltlist, "Received and expected language tags lists differ");
103 RUNNER_TEST(tagsFromGivenTagList)
105 LogInfo("Copying given tags list");
107 LogDebug("Correct full list (with default values)");
108 LanguageTags correct;
109 correct.push_back(L"de-DE");
110 correct.push_back(L"de");
111 correct.push_back(L"");
112 LanguageTagsProviderSingleton::Instance().setLanguageTags(correct);
113 LanguageTags result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
114 RUNNER_ASSERT_MSG(correct==result, "Received and expected language tags lists differ");
116 LogDebug("Tags list without default values)");
118 nondef.push_back(L"de-DE");
119 nondef.push_back(L"de");
120 LanguageTagsProviderSingleton::Instance().setLanguageTags(correct);
121 result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
123 /* Received list should contains elements from input list with default
124 * values added (as "correct" list has) */
125 RUNNER_ASSERT_MSG(!result.empty(), "Empty tags list should never be returned");
126 RUNNER_ASSERT_MSG(nondef!=result, "Received list is same as given incomplete one");
127 RUNNER_ASSERT_MSG(correct==result, "Received and expected language tags lists differ");
130 RUNNER_TEST(tagsFromEmptyList)
132 LogInfo("Generating tags when empty tag list given");
135 LanguageTagsProviderSingleton::Instance().setLanguageTags(input);
136 LanguageTags result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
137 RUNNER_ASSERT_MSG(!result.empty(), "Empty tags list should never be returned");
140 RUNNER_TEST(defaultWidgetLocale)
142 LogInfo("Adding default widget locales to language tags list");
145 input.push_back(L"de-DE");
146 input.push_back(L"de");
147 input.push_back(L"");
148 LanguageTagsProviderSingleton::Instance().setLanguageTags(input);
149 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(L"it");
150 LanguageTags result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
151 RUNNER_ASSERT_MSG(result.size() == 4, "4 different language tags expected");
152 LanguageTags reference;
153 reference.push_back(L"de-DE");
154 reference.push_back(L"de");
155 reference.push_back(L"it");
156 reference.push_back(L"");
157 RUNNER_ASSERT_MSG(result == reference, "Received and expected language tags lists differ");
158 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(L"it");
159 LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(L"de-DE");
160 result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
161 RUNNER_ASSERT_MSG(result == reference, "Adding already included tag should not change tags list");