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