OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/translate/core/browser/translate_prefs.h" | 5 #include "components/translate/core/browser/translate_prefs.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "components/pref_registry/testing_pref_service_syncable.h" | 13 #include "components/pref_registry/testing_pref_service_syncable.h" |
14 #include "components/prefs/scoped_user_pref_update.h" | 14 #include "components/prefs/scoped_user_pref_update.h" |
15 #include "components/translate/core/browser/translate_download_manager.h" | 15 #include "components/translate/core/browser/translate_download_manager.h" |
16 #include "components/translate/core/browser/translate_prefs.h" | 16 #include "components/translate/core/browser/translate_prefs.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char kTestLanguage[] = "en"; | 21 const char kTestLanguage[] = "en"; |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 namespace translate { | 25 namespace translate { |
26 | 26 |
27 TEST(TranslatePrefsTest, CreateBlockedLanguages) { | |
28 TranslateDownloadManager::GetInstance()->set_application_locale("en"); | |
29 std::vector<std::string> blacklisted_languages; | |
30 blacklisted_languages.push_back("en"); | |
31 blacklisted_languages.push_back("fr"); | |
32 // Hebrew: synonym to 'he' | |
33 blacklisted_languages.push_back("iw"); | |
34 // Haitian is not used as Accept-Language | |
35 blacklisted_languages.push_back("ht"); | |
36 | |
37 std::vector<std::string> accept_languages; | |
38 accept_languages.push_back("en"); | |
39 // The subcode (IT) will be ignored when merging, except for Chinese. | |
40 accept_languages.push_back("it-IT"); | |
41 accept_languages.push_back("ja"); | |
42 // Filippino: synonym to 'tl' | |
43 accept_languages.push_back("fil"); | |
44 // General Chinese is not used as Translate language, but not filtered | |
45 // when merging. | |
46 accept_languages.push_back("zh"); | |
47 // Chinese with a sub code is acceptable for the blocked-language list. | |
48 accept_languages.push_back("zh-TW"); | |
49 | |
50 std::vector<std::string> blocked_languages; | |
51 | |
52 TranslatePrefs::CreateBlockedLanguages( | |
53 &blocked_languages, blacklisted_languages, accept_languages); | |
54 | |
55 // The order of the elements cannot be determined. | |
56 std::vector<std::string> expected; | |
57 expected.push_back("en"); | |
58 expected.push_back("fr"); | |
59 expected.push_back("iw"); | |
60 expected.push_back("ht"); | |
61 expected.push_back("it"); | |
62 expected.push_back("ja"); | |
63 expected.push_back("tl"); | |
64 expected.push_back("zh"); | |
65 expected.push_back("zh-TW"); | |
66 | |
67 EXPECT_EQ(expected.size(), blocked_languages.size()); | |
68 for (std::vector<std::string>::const_iterator it = expected.begin(); | |
69 it != expected.end(); ++it) { | |
70 EXPECT_NE(blocked_languages.end(), std::find(blocked_languages.begin(), | |
71 blocked_languages.end(), *it)); | |
72 } | |
73 } | |
74 | |
75 TEST(TranslatePrefsTest, CreateBlockedLanguagesNonEnglishUI) { | |
76 std::vector<std::string> blacklisted_languages; | |
77 blacklisted_languages.push_back("fr"); | |
78 | |
79 std::vector<std::string> accept_languages; | |
80 accept_languages.push_back("en"); | |
81 accept_languages.push_back("ja"); | |
82 accept_languages.push_back("zh"); | |
83 | |
84 // Run in an English locale. | |
85 { | |
86 TranslateDownloadManager::GetInstance()->set_application_locale("en"); | |
87 std::vector<std::string> blocked_languages; | |
88 TranslatePrefs::CreateBlockedLanguages( | |
89 &blocked_languages, blacklisted_languages, accept_languages); | |
90 std::vector<std::string> expected; | |
91 expected.push_back("en"); | |
92 expected.push_back("fr"); | |
93 expected.push_back("ja"); | |
94 expected.push_back("zh"); | |
95 | |
96 EXPECT_EQ(expected.size(), blocked_languages.size()); | |
97 for (std::vector<std::string>::const_iterator it = expected.begin(); | |
98 it != expected.end(); ++it) { | |
99 EXPECT_NE( | |
100 blocked_languages.end(), | |
101 std::find(blocked_languages.begin(), blocked_languages.end(), *it)); | |
102 } | |
103 } | |
104 | |
105 // Run in a Japanese locale. | |
106 // English should not be included in the result even though Accept Languages | |
107 // has English because the UI is not English. | |
108 { | |
109 TranslateDownloadManager::GetInstance()->set_application_locale("ja"); | |
110 std::vector<std::string> blocked_languages; | |
111 TranslatePrefs::CreateBlockedLanguages( | |
112 &blocked_languages, blacklisted_languages, accept_languages); | |
113 std::vector<std::string> expected; | |
114 expected.push_back("fr"); | |
115 expected.push_back("ja"); | |
116 expected.push_back("zh"); | |
117 | |
118 EXPECT_EQ(expected.size(), blocked_languages.size()); | |
119 for (std::vector<std::string>::const_iterator it = expected.begin(); | |
120 it != expected.end(); ++it) { | |
121 EXPECT_NE( | |
122 blocked_languages.end(), | |
123 std::find(blocked_languages.begin(), blocked_languages.end(), *it)); | |
124 } | |
125 } | |
126 } | |
127 | |
128 class TranslatePrefTest : public testing::Test { | 27 class TranslatePrefTest : public testing::Test { |
129 protected: | 28 protected: |
130 TranslatePrefTest() : prefs_(new user_prefs::TestingPrefServiceSyncable()) { | 29 TranslatePrefTest() : prefs_(new user_prefs::TestingPrefServiceSyncable()) { |
131 #if defined(OS_CHROMEOS) | 30 #if defined(OS_CHROMEOS) |
132 const char* preferred_languages_prefs = | 31 const char* preferred_languages_prefs = |
133 "settings.language.preferred_languages"; | 32 "settings.language.preferred_languages"; |
134 #else | 33 #else |
135 const char* preferred_languages_prefs = NULL; | 34 const char* preferred_languages_prefs = NULL; |
136 #endif | 35 #endif |
137 translate_prefs_.reset(new translate::TranslatePrefs( | 36 translate_prefs_.reset(new translate::TranslatePrefs( |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 update.AddDenialTime(now_); | 213 update.AddDenialTime(now_); |
315 EXPECT_EQ(update.GetOldestDenialTime(), | 214 EXPECT_EQ(update.GetOldestDenialTime(), |
316 now_ - base::TimeDelta::FromMinutes(3)); | 215 now_ - base::TimeDelta::FromMinutes(3)); |
317 | 216 |
318 update.AddDenialTime(now_); | 217 update.AddDenialTime(now_); |
319 EXPECT_EQ(update.GetOldestDenialTime(), | 218 EXPECT_EQ(update.GetOldestDenialTime(), |
320 now_ - base::TimeDelta::FromMinutes(2)); | 219 now_ - base::TimeDelta::FromMinutes(2)); |
321 } | 220 } |
322 | 221 |
323 } // namespace translate | 222 } // namespace translate |
OLD | NEW |