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. 4 5 #include "base/path_service.h" 6 #include "base/synchronization/waitable_event.h" 7 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/spellchecker/spellcheck_factory.h" 9 #include "chrome/browser/spellchecker/spellcheck_service.h" 10 #include "chrome/browser/ui/browser.h" 11 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/spellcheck_common.h" 13 #include "chrome/test/base/in_process_browser_test.h" 14 #include "chrome/test/base/ui_test_utils.h" 15 #include "url/gurl.h" 16 17 namespace { 18 19 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this 20 // BDICT data for other tests. 21 const uint8 kCorruptedBDICT[] = { 22 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00, 23 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, 24 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66, 25 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35, 26 0x32, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 27 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 28 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00, 29 0x00, 0x00, 0x00, 0xe6, 0x49, 0x00, 0x68, 0x02, 30 0x73, 0x06, 0x74, 0x0b, 0x77, 0x11, 0x79, 0x15, 31 }; 32 33 } // namespace 34 35 class SpellcheckServiceBrowserTest : public InProcessBrowserTest { 36 public: 37 Profile* GetProfile() { 38 return browser()->profile(); 39 } 40 }; 41 42 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not 43 // run this test on Mac because Mac does not use hunspell by default. 44 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { 45 // Write the corrupted BDICT data to create a corrupted BDICT file. 46 base::FilePath dict_dir; 47 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); 48 base::FilePath bdict_path = 49 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); 50 51 size_t actual = file_util::WriteFile(bdict_path, 52 reinterpret_cast<const char*>(kCorruptedBDICT), 53 arraysize(kCorruptedBDICT)); 54 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); 55 56 // Attach an event to the SpellcheckService object so we can receive its 57 // status updates. 58 base::WaitableEvent event(true, false); 59 SpellcheckService::AttachStatusEvent(&event); 60 61 // Ensure that the SpellcheckService object does not already exist. Otherwise 62 // the next line will not force creation of the SpellcheckService and the 63 // test will fail. 64 SpellcheckService* service = 65 SpellcheckServiceFactory::GetForProfileWithoutCreating(GetProfile()); 66 ASSERT_EQ(NULL, service); 67 68 // Getting the spellcheck_service will initialize the SpellcheckService 69 // object with the corrupted BDICT file created above since the hunspell 70 // dictionary is loaded in the SpellcheckService constructor right now. 71 // The SpellCheckHost object will send a BDICT_CORRUPTED event. 72 SpellcheckServiceFactory::GetForProfile(GetProfile()); 73 74 // Check the received event. Also we check if Chrome has successfully deleted 75 // the corrupted dictionary. We delete the corrupted dictionary to avoid 76 // leaking it when this test fails. 77 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); 78 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 79 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, 80 SpellcheckService::GetStatusEvent()); 81 if (base::PathExists(bdict_path)) { 82 ADD_FAILURE(); 83 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); 84 } 85 } 86