summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
blob: 249ca1b8ac3680f284e2ce06663b3b91964a5837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/scoped_temp_dir.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
#include "chrome/common/spellcheck_common.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::BrowserThread;
using chrome::spellcheck_common::WordList;

static ProfileKeyedService* BuildSpellcheckService(Profile* profile) {
  return new SpellcheckService(profile);
}

class SpellcheckCustomDictionaryTest : public testing::Test {
 protected:
  SpellcheckCustomDictionaryTest()
      : ui_thread_(BrowserThread::UI, &message_loop_),
        file_thread_(BrowserThread::FILE, &message_loop_),
        profile_(new TestingProfile()) {
  }

  void SetUp() OVERRIDE {
    // Use SetTestingFactoryAndUse to force creation and initialization.
    SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse(
        profile_.get(), &BuildSpellcheckService);
  }

  void TearDown() OVERRIDE {
    MessageLoop::current()->RunUntilIdle();
  }

  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;

  scoped_ptr<TestingProfile> profile_;
};

// TODO(rlp/rouslan): Shift some of these to a cutsom dictionary test suite.
TEST_F(SpellcheckCustomDictionaryTest, SpellcheckSetCustomWordList) {
  SpellcheckService* spellcheck_service =
      SpellcheckServiceFactory::GetForProfile(profile_.get());

  WordList loaded_custom_words;
  loaded_custom_words.push_back("foo");
  loaded_custom_words.push_back("bar");
  WordList expected(loaded_custom_words);
  SpellcheckCustomDictionary* custom_dictionary =
      spellcheck_service->GetCustomDictionary();
  custom_dictionary->SetCustomWordList(&loaded_custom_words);
  EXPECT_EQ(custom_dictionary->GetCustomWords(), expected);
}

TEST_F(SpellcheckCustomDictionaryTest, CustomWordAddedLocally) {
  SpellcheckService* spellcheck_service =
      SpellcheckServiceFactory::GetForProfile(profile_.get());

  WordList loaded_custom_words;
  SpellcheckCustomDictionary* custom_dictionary =
      spellcheck_service->GetCustomDictionary();
  custom_dictionary->Load();
  WordList expected;
  EXPECT_EQ(custom_dictionary->GetCustomWords(), expected);
  custom_dictionary->CustomWordAddedLocally("foo");
  expected.push_back("foo");
  EXPECT_EQ(custom_dictionary->GetCustomWords(), expected);
  custom_dictionary->CustomWordAddedLocally("bar");
  expected.push_back("bar");
  EXPECT_EQ(custom_dictionary->GetCustomWords(), expected);
}

TEST_F(SpellcheckCustomDictionaryTest, SaveAndLoad) {
  SpellcheckService* spellcheck_service =
      SpellcheckServiceFactory::GetForProfile(profile_.get());
  SpellcheckCustomDictionary* custom_dictionary =
      spellcheck_service->GetCustomDictionary();

  WordList loaded_custom_words;
  custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);

  // The custom word list should be empty now.
  WordList expected;
  EXPECT_EQ(loaded_custom_words, expected);

  custom_dictionary->WriteWordToCustomDictionary("foo");
  expected.push_back("foo");

  custom_dictionary->WriteWordToCustomDictionary("bar");
  expected.push_back("bar");

  // The custom word list should include written words.
  custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
  EXPECT_EQ(loaded_custom_words, expected);

  // Load in another instance of SpellCheckService.
  // The result should be the same.
  SpellcheckService spellcheck_service2(profile_.get());
  WordList loaded_custom_words2;
  spellcheck_service2.GetCustomDictionary()->
      LoadDictionaryIntoCustomWordList(&loaded_custom_words2);
  EXPECT_EQ(loaded_custom_words2, expected);
  // Flush the loop now to prevent service init tasks from being run during
  // TearDown();
  MessageLoop::current()->RunUntilIdle();
}

TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) {
  SpellcheckService* spellcheck_service =
      SpellcheckServiceFactory::GetForProfile(profile_.get());
  SpellcheckCustomDictionary* custom_dictionary =
      spellcheck_service->GetCustomDictionary();
  TestingProfile profile2;
  SpellcheckService* spellcheck_service2 =
      static_cast<SpellcheckService*>(
          SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse(
              &profile2, &BuildSpellcheckService));
  SpellcheckCustomDictionary* custom_dictionary2 =
      spellcheck_service2->GetCustomDictionary();

  WordList expected1;
  WordList expected2;

  custom_dictionary->WriteWordToCustomDictionary("foo");
  custom_dictionary->WriteWordToCustomDictionary("bar");
  expected1.push_back("foo");
  expected1.push_back("bar");

  custom_dictionary2->WriteWordToCustomDictionary("hoge");
  custom_dictionary2->WriteWordToCustomDictionary("fuga");
  expected2.push_back("hoge");
  expected2.push_back("fuga");

  WordList actual1;
  custom_dictionary->LoadDictionaryIntoCustomWordList(&actual1);
  EXPECT_EQ(actual1, expected1);

  WordList actual2;
  custom_dictionary2->LoadDictionaryIntoCustomWordList(&actual2);
  EXPECT_EQ(actual2, expected2);

  // Flush the loop now to prevent service init tasks from being run during
  // TearDown();
  MessageLoop::current()->RunUntilIdle();
}