1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/spellchecker/spellcheck_service.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/strings/string_split.h"
12 #include "base/supports_user_data.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/user_prefs/user_prefs.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 struct SpellcheckLanguageTestCase
{
20 SpellcheckLanguageTestCase(const std::string
& accept_languages
,
21 const std::string
& unsplit_spellcheck_dictionaries
,
22 size_t num_expected_enabled_spellcheck_languages
,
23 const std::string
& unsplit_expected_languages
)
24 : accept_languages(accept_languages
),
25 num_expected_enabled_spellcheck_languages(
26 num_expected_enabled_spellcheck_languages
) {
27 spellcheck_dictionaries
= base::SplitString(
28 unsplit_spellcheck_dictionaries
, ",",
29 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
30 expected_spellcheck_languages
= base::SplitString(
31 unsplit_expected_languages
, ",",
32 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
34 ~SpellcheckLanguageTestCase() {}
36 std::string accept_languages
;
37 std::vector
<std::string
> spellcheck_dictionaries
;
38 size_t num_expected_enabled_spellcheck_languages
;
39 std::vector
<std::string
> expected_spellcheck_languages
;
42 class SpellcheckServiceUnitTest
43 : public testing::TestWithParam
<SpellcheckLanguageTestCase
> {
45 SpellcheckServiceUnitTest()
46 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
47 user_prefs::UserPrefs::Set(&context_
, &prefs_
);
49 ~SpellcheckServiceUnitTest() override
{}
51 void SetUp() override
{
52 prefs()->registry()->RegisterListPref(prefs::kSpellCheckDictionaries
);
53 prefs()->registry()->RegisterStringPref(prefs::kAcceptLanguages
,
57 base::SupportsUserData
* context() { return &context_
; }
58 TestingPrefServiceSimple
* prefs() { return &prefs_
; }
61 struct : public base::SupportsUserData
{} context_
;
62 TestingPrefServiceSimple prefs_
;
63 base::MessageLoop message_loop_
;
64 content::TestBrowserThread ui_thread_
;
66 DISALLOW_COPY_AND_ASSIGN(SpellcheckServiceUnitTest
);
69 INSTANTIATE_TEST_CASE_P(
70 SpellcheckLanguageTestCases
,
71 SpellcheckServiceUnitTest
,
73 SpellcheckLanguageTestCase("en,en-US", "en-US", 1UL, "en-US"),
74 SpellcheckLanguageTestCase("en-US,en", "en-US", 1UL, "en-US"),
75 SpellcheckLanguageTestCase("en,en-US,en-AU",
79 SpellcheckLanguageTestCase("en,en-US,fr", "en-US", 1UL, "en-US,fr"),
80 SpellcheckLanguageTestCase("en,en-JP,fr,aa", "fr", 1UL, "fr"),
81 SpellcheckLanguageTestCase("en,en-US", "en-US", 1UL, "en-US"),
82 SpellcheckLanguageTestCase("en-US,en", "en-US", 1UL, "en-US"),
83 SpellcheckLanguageTestCase("en,fr,en-US,en-AU",
87 SpellcheckLanguageTestCase("en,en-JP,fr,zz,en-US",
92 TEST_P(SpellcheckServiceUnitTest
, GetSpellcheckLanguages
) {
93 prefs()->SetString(prefs::kAcceptLanguages
, GetParam().accept_languages
);
94 base::ListValue dictionaries
;
95 dictionaries
.AppendStrings(GetParam().spellcheck_dictionaries
);
96 prefs()->Set(prefs::kSpellCheckDictionaries
, dictionaries
);
98 std::vector
<std::string
> spellcheck_languages
;
99 EXPECT_EQ(GetParam().num_expected_enabled_spellcheck_languages
,
100 SpellcheckService::GetSpellCheckLanguages(context(),
101 &spellcheck_languages
));
103 EXPECT_EQ(GetParam().expected_spellcheck_languages
, spellcheck_languages
);