tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / localizationTagsProvider / Localization_testcases.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file    Localization_testcases.cpp
18  * @author  Marcin Kaminski (marcin.ka@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for localization related code.
21  */
22
23 #include <dpl/log/log.h>
24 #include <dpl/test/test_runner.h>
25 #include <vconf.h>
26
27 #include <LanguageTagsProvider.h>
28
29 RUNNER_TEST_GROUP_INIT(LanguageTagsProvider)
30
31 RUNNER_TEST(tagsFromSystemLocales)
32 {
33     LogDebug("Generating tags from system locales");
34
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");
41
42     /* Ensure that system locales where fetched */
43     LanguageTagsProviderSingleton::Instance().resetLanguageTags();
44     LogDebug("Language tags set based on current system locales");
45
46     LanguageTags ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
47     RUNNER_ASSERT_MSG(!ltlist.empty(), "Empty tag list returned");
48
49     /* Correct list generated from given locales should contain: "en-US", "en" and "" */
50     LanguageTags correct;
51     correct.push_back(L"en-US");
52     correct.push_back(L"en");
53     correct.push_back(L"");
54
55     RUNNER_ASSERT_MSG( correct==ltlist, "Received and expected language tags lists differ");
56
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");
61 }
62
63 RUNNER_TEST(tagsFromGivenLocales)
64 {
65     LogDebug("Generating tags from given locales");
66
67     const char *locales1 = "it_IT.UTF-8", *locales2="en_GB";
68
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 "" */
74     LanguageTags correct;
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");
79
80     LogDebug("Using locales without codepage: " << locales2);
81     LanguageTagsProviderSingleton::Instance().setLanguageTagsFromLocales(locales2);
82     ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
83     correct.clear();
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");
88 }
89
90 RUNNER_TEST(tagsFromNullLocales)
91 {
92     LogDebug("Generating tags when NULL locales given");
93
94     LanguageTagsProviderSingleton::Instance().setLanguageTagsFromLocales(NULL);
95     LanguageTags ltlist = LanguageTagsProviderSingleton::Instance().getLanguageTags();
96     /* List with two values "en" and "" should be returned */
97     LanguageTags correct;
98     correct.push_back(L"");
99     RUNNER_ASSERT_MSG(correct==ltlist, "Received and expected language tags lists differ");
100 }
101
102
103 RUNNER_TEST(tagsFromGivenTagList)
104 {
105     LogDebug("Copying given tags list");
106
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");
115
116     LogDebug("Tags list without default values)");
117     LanguageTags nondef;
118     nondef.push_back(L"de-DE");
119     nondef.push_back(L"de");
120     LanguageTagsProviderSingleton::Instance().setLanguageTags(correct);
121     result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
122
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");
128 }
129
130 RUNNER_TEST(tagsFromEmptyList)
131 {
132     LogDebug("Generating tags when empty tag list given");
133
134     LanguageTags input;
135     LanguageTagsProviderSingleton::Instance().setLanguageTags(input);
136     LanguageTags result = LanguageTagsProviderSingleton::Instance().getLanguageTags();
137     RUNNER_ASSERT_MSG(!result.empty(), "Empty tags list should never be returned");
138 }
139
140 RUNNER_TEST(defaultWidgetLocale)
141 {
142     LogDebug("Adding default widget locales to language tags list");
143
144     LanguageTags input;
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");
162 }