File: spelling_options_submenu_observer_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (215 lines) | stat: -rw-r--r-- 9,291 bytes parent folder | download | duplicates (5)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/renderer_context_menu/spelling_options_submenu_observer.h"

#include <memory>

#include "base/values.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/renderer_context_menu/mock_render_view_context_menu.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/language/core/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/browser/pref_names.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// A test class used in this file. This test should be a browser test because it
// accesses resources.
class SpellingOptionsSubMenuObserverTest : public InProcessBrowserTest {
 public:
  SpellingOptionsSubMenuObserverTest() = default;

  SpellingOptionsSubMenuObserverTest(
      const SpellingOptionsSubMenuObserverTest&) = delete;
  SpellingOptionsSubMenuObserverTest& operator=(
      const SpellingOptionsSubMenuObserverTest&) = delete;

  ~SpellingOptionsSubMenuObserverTest() override = default;

  void SetUpOnMainThread() override {
    menu_ = std::make_unique<MockRenderViewContextMenu>(false);
    observer_ = std::make_unique<SpellingOptionsSubMenuObserver>(
        // Pass nullptr as a delegate so that submenu items do not get put into
        // MockRenderViewContextMenu::items_.
        menu_.get(), nullptr, 1);
    menu_->SetObserver(observer_.get());
  }

  void TearDownOnMainThread() override {
    menu_->SetObserver(nullptr);
    observer_.reset();
    menu_.reset();
  }

  void InitMenu(bool enable_spellcheck,
                bool use_spellchecking_service,
                const std::string& accept_languages,
                const std::vector<std::string>& dictionaries) {
    menu()->GetPrefs()->SetBoolean(spellcheck::prefs::kSpellCheckEnable,
                                   enable_spellcheck);
    menu()->GetPrefs()->SetBoolean(
        spellcheck::prefs::kSpellCheckUseSpellingService,
        use_spellchecking_service);
    menu()->GetPrefs()->SetString(language::prefs::kAcceptLanguages,
                                  accept_languages);
    base::Value::List dictionaries_value;
    for (const std::string& dict : dictionaries) {
      dictionaries_value.Append(dict);
    }
    menu()->GetPrefs()->SetList(spellcheck::prefs::kSpellCheckDictionaries,
                                std::move(dictionaries_value));
    observer()->InitMenu(content::ContextMenuParams());
  }

  void ExpectPreferences(bool spellcheck_enabled,
                         const std::vector<std::string>& dictionaries) {
    EXPECT_EQ(spellcheck_enabled, menu()->GetPrefs()->GetBoolean(
                                      spellcheck::prefs::kSpellCheckEnable));
    base::Value::List dictionaries_value;
    for (const std::string& dict : dictionaries) {
      dictionaries_value.Append(dict);
    }
    EXPECT_EQ(dictionaries_value,
              menu()->GetPrefs()->GetList(
                  spellcheck::prefs::kSpellCheckDictionaries));
  }

  MockRenderViewContextMenu* menu() { return menu_.get(); }
  SpellingOptionsSubMenuObserver* observer() { return observer_.get(); }

 private:
  std::unique_ptr<MockRenderViewContextMenu> menu_;
  std::unique_ptr<SpellingOptionsSubMenuObserver> observer_;
};

// Tests that selecting the "Use basic spell check" item toggles the value
// of the "browser.enable_spellchecking" profile.
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, ToggleSpelling) {
  InitMenu(true, false, "en-US", std::vector<std::string>(1, "en-US"));

  // Verify this menu has the "Use basic spell check" item and this item
  // is checked.
  EXPECT_TRUE(menu()->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
  EXPECT_FALSE(menu()->GetPrefs()->GetBoolean(
      spellcheck::prefs::kSpellCheckUseSpellingService));

  // Select this item and verify that the "Use basic spell check" item is
  // not checked. Also, verify that the value of "browser.enable_spellchecking"
  // is now false.
  menu()->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0);
  ExpectPreferences(false, std::vector<std::string>(1, "en-US"));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));

  menu()->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 1);
  ExpectPreferences(true, std::vector<std::string>(1, "en-US"));
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
}

// TODO(https://crbug.com/410751413): Deleting temporary directories using
// test_file_util is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_ToggleSpellingWithEnhancedSpellCheck \
  DISABLED_ToggleSpellingWithEnhancedSpellCheck
#else
#define MAYBE_ToggleSpellingWithEnhancedSpellCheck \
  ToggleSpellingWithEnhancedSpellCheck
#endif
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest,
                       MAYBE_ToggleSpellingWithEnhancedSpellCheck) {
  InitMenu(true, true, "en-US,es", std::vector<std::string>(1, "en-US"));

  // Verify that because 'Use Enhanced spell check' is checked, the
  // 'Use basic spell check' is shown as unchecked
  EXPECT_TRUE(menu()->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));

  // Select this item and verify that "Use basic spell check" item is now
  // checked and that 'spellcheck.use_spelling_service' is now disabled.
  menu()->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 1);
  ExpectPreferences(true, std::vector<std::string>(1, "en-US"));
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
  EXPECT_FALSE(menu()->GetPrefs()->GetBoolean(
      spellcheck::prefs::kSpellCheckUseSpellingService));
}

// Single accept language is selected based on the dictionaries preference.
// Consequently selecting multilingual spellcheck should copy all accept
// languages into spellcheck dictionaries preference.
// TODO(https://crbug.com/410751413): Deleting temporary directories using
// test_file_util is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_SelectMultilingual DISABLED_SelectMultilingual
#else
#define MAYBE_SelectMultilingual SelectMultilingual
#endif
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest,
                       MAYBE_SelectMultilingual) {
  InitMenu(true, false, "en-US,es", std::vector<std::string>(1, "en-US"));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL));
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1));

  menu()->ExecuteCommand(IDC_SPELLCHECK_MULTI_LINGUAL, 0);
  std::vector<std::string> dictionaries;
  dictionaries.push_back("en-US");
  dictionaries.push_back("es");
  ExpectPreferences(true, dictionaries);
}

// Multilingual spellcheck is selected when all dictionaries are used for
// spellcheck. Consequently selecting "English (United States)" should set
// spellcheck dictionaries preferences to ["en-US"].
// TODO(https://crbug.com/410751413): Deleting temporary directories using
// test_file_util is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_SelectFirstLanguage DISABLED_SelectFirstLanguage
#else
#define MAYBE_SelectFirstLanguage SelectFirstLanguage
#endif
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest,
                       MAYBE_SelectFirstLanguage) {
  std::vector<std::string> dictionaries;
  dictionaries.push_back("en-US");
  dictionaries.push_back("es");
  InitMenu(true, false, "en-US,es", dictionaries);
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST));
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1));

  menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0);
  ExpectPreferences(true, std::vector<std::string>(1, "en-US"));
}

// Single dictionary should be selected based on preferences.
// TODO(https://crbug.com/410751413): Deleting temporary directories using
// test_file_util is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_SingleLanguageSelected DISABLED_SingleLanguageSelected
#else
#define MAYBE_SingleLanguageSelected SingleLanguageSelected
#endif
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest,
                       MAYBE_SingleLanguageSelected) {
  InitMenu(true, false, "en-US", std::vector<std::string>(1, "en-US"));
  EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST));
}

// Single dictionary should be deselected based on preferences. Selecting the
// dictionary should update the preference.
IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest,
                       SelectTheOnlyLanguage) {
  InitMenu(true, false, "en-US", std::vector<std::string>());
  EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST));

  menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0);
  ExpectPreferences(true, std::vector<std::string>(1, "en-US"));
}

}  // namespace