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 "base/path_service.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/spellchecker/spellcheck_factory.h"
10 #include "chrome/browser/spellchecker/spellcheck_service.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/common/spellcheck_common.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "components/user_prefs/user_prefs.h"
17 #include "content/public/test/test_utils.h"
20 using content::BrowserContext
;
24 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this
25 // BDICT data for other tests.
26 const uint8 kCorruptedBDICT
[] = {
27 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00,
28 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,
29 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66,
30 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35,
31 0x32, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
32 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00,
33 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0xe6, 0x49, 0x00, 0x68, 0x02,
35 0x73, 0x06, 0x74, 0x0b, 0x77, 0x11, 0x79, 0x15,
40 class SpellcheckServiceBrowserTest
: public InProcessBrowserTest
{
42 BrowserContext
* GetContext() {
43 return static_cast<BrowserContext
*>(browser()->profile());
47 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not
48 // run this test on Mac because Mac does not use hunspell by default.
49 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest
, DeleteCorruptedBDICT
) {
50 // Write the corrupted BDICT data to create a corrupted BDICT file.
51 base::FilePath dict_dir
;
52 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES
, &dict_dir
));
53 base::FilePath bdict_path
=
54 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir
);
56 size_t actual
= base::WriteFile(bdict_path
,
57 reinterpret_cast<const char*>(kCorruptedBDICT
),
58 arraysize(kCorruptedBDICT
));
59 EXPECT_EQ(arraysize(kCorruptedBDICT
), actual
);
61 // Attach an event to the SpellcheckService object so we can receive its
63 base::WaitableEvent
event(true, false);
64 SpellcheckService::AttachStatusEvent(&event
);
66 BrowserContext
* context
= GetContext();
68 // Ensure that the SpellcheckService object does not already exist. Otherwise
69 // the next line will not force creation of the SpellcheckService and the
71 SpellcheckService
* service
= static_cast<SpellcheckService
*>(
72 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext(
75 ASSERT_EQ(NULL
, service
);
77 // Getting the spellcheck_service will initialize the SpellcheckService
78 // object with the corrupted BDICT file created above since the hunspell
79 // dictionary is loaded in the SpellcheckService constructor right now.
80 // The SpellCheckHost object will send a BDICT_CORRUPTED event.
81 SpellcheckServiceFactory::GetForContext(context
);
83 // Check the received event. Also we check if Chrome has successfully deleted
84 // the corrupted dictionary. We delete the corrupted dictionary to avoid
85 // leaking it when this test fails.
86 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
87 content::RunAllPendingInMessageLoop(content::BrowserThread::UI
);
88 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED
,
89 SpellcheckService::GetStatusEvent());
90 if (base::PathExists(bdict_path
)) {
92 EXPECT_TRUE(base::DeleteFile(bdict_path
, true));
96 // Checks that preferences migrate correctly.
97 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest
, PreferencesMigrated
) {
98 PrefService
* prefs
= user_prefs::UserPrefs::Get(GetContext());
99 prefs
->Set(prefs::kSpellCheckDictionaries
, base::ListValue());
100 prefs
->SetString(prefs::kSpellCheckDictionary
, "en-US");
102 // Create a SpellcheckService which will migrate the preferences.
103 SpellcheckServiceFactory::GetForContext(GetContext());
105 // Make sure the preferences have been migrated.
106 std::string new_pref
;
108 prefs
->GetList(prefs::kSpellCheckDictionaries
)->GetString(0, &new_pref
));
109 EXPECT_EQ("en-US", new_pref
);
110 EXPECT_TRUE(prefs
->GetString(prefs::kSpellCheckDictionary
).empty());
113 // Checks that preferences are not migrated when they shouldn't be.
114 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest
, PreferencesNotMigrated
) {
115 PrefService
* prefs
= user_prefs::UserPrefs::Get(GetContext());
116 base::ListValue dictionaries
;
117 dictionaries
.AppendString("en-US");
118 prefs
->Set(prefs::kSpellCheckDictionaries
, dictionaries
);
119 prefs
->SetString(prefs::kSpellCheckDictionary
, "fr");
121 // Create a SpellcheckService which will migrate the preferences.
122 SpellcheckServiceFactory::GetForContext(GetContext());
124 // Make sure the preferences have not been migrated.
125 std::string new_pref
;
127 prefs
->GetList(prefs::kSpellCheckDictionaries
)->GetString(0, &new_pref
));
128 EXPECT_EQ("en-US", new_pref
);
129 EXPECT_TRUE(prefs
->GetString(prefs::kSpellCheckDictionary
).empty());