Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: components/omnibox/browser/omnibox_edit_unittest.cc

Issue 2048693003: Misc. omnibox cleanup: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resync Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "components/omnibox/browser/autocomplete_classifier.h" 11 #include "components/omnibox/browser/autocomplete_classifier.h"
12 #include "components/omnibox/browser/autocomplete_controller.h" 12 #include "components/omnibox/browser/autocomplete_controller.h"
13 #include "components/omnibox/browser/autocomplete_scheme_classifier.h" 13 #include "components/omnibox/browser/autocomplete_scheme_classifier.h"
14 #include "components/omnibox/browser/history_url_provider.h"
14 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" 15 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
15 #include "components/omnibox/browser/omnibox_client.h" 16 #include "components/omnibox/browser/omnibox_client.h"
16 #include "components/omnibox/browser/omnibox_edit_controller.h" 17 #include "components/omnibox/browser/omnibox_edit_controller.h"
17 #include "components/omnibox/browser/omnibox_edit_model.h" 18 #include "components/omnibox/browser/omnibox_edit_model.h"
18 #include "components/omnibox/browser/omnibox_view.h" 19 #include "components/omnibox/browser/omnibox_view.h"
19 #include "components/search_engines/search_terms_data.h" 20 #include "components/search_engines/search_terms_data.h"
20 #include "components/search_engines/template_url_service.h" 21 #include "components/search_engines/template_url_service.h"
21 #include "components/search_engines/template_url_service_client.h" 22 #include "components/search_engines/template_url_service_client.h"
22 #include "components/sessions/core/session_id.h" 23 #include "components/sessions/core/session_id.h"
23 #include "components/toolbar/test_toolbar_model.h" 24 #include "components/toolbar/test_toolbar_model.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 using base::ASCIIToUTF16;
27 using base::UTF8ToUTF16;
28
29 namespace { 27 namespace {
30 28
31 class TestingOmniboxView : public OmniboxView { 29 class TestingOmniboxView : public OmniboxView {
32 public: 30 public:
33 explicit TestingOmniboxView(OmniboxEditController* controller) 31 explicit TestingOmniboxView(OmniboxEditController* controller)
34 : OmniboxView(controller, nullptr) {} 32 : OmniboxView(controller, nullptr) {}
35 33
36 // OmniboxView: 34 // OmniboxView:
37 void Update() override {} 35 void Update() override {}
38 void OpenMatch(const AutocompleteMatch& match, 36 void OpenMatch(const AutocompleteMatch& match,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 return text_changed; 71 return text_changed;
74 } 72 }
75 void OnInlineAutocompleteTextCleared() override { 73 void OnInlineAutocompleteTextCleared() override {
76 inline_autocomplete_text_.clear(); 74 inline_autocomplete_text_.clear();
77 } 75 }
78 void OnRevertTemporaryText() override {} 76 void OnRevertTemporaryText() override {}
79 void OnBeforePossibleChange() override {} 77 void OnBeforePossibleChange() override {}
80 bool OnAfterPossibleChange(bool allow_keyword_ui_change) override { 78 bool OnAfterPossibleChange(bool allow_keyword_ui_change) override {
81 return false; 79 return false;
82 } 80 }
83 gfx::NativeView GetNativeView() const override { return NULL; } 81 gfx::NativeView GetNativeView() const override { return nullptr; }
84 gfx::NativeView GetRelativeWindowForPopup() const override { return NULL; } 82 gfx::NativeView GetRelativeWindowForPopup() const override { return nullptr; }
85 void SetGrayTextAutocompletion(const base::string16& input) override {} 83 void SetGrayTextAutocompletion(const base::string16& input) override {}
86 base::string16 GetGrayTextAutocompletion() const override { 84 base::string16 GetGrayTextAutocompletion() const override {
87 return base::string16(); 85 return base::string16();
88 } 86 }
89 int GetTextWidth() const override { return 0; } 87 int GetTextWidth() const override { return 0; }
90 int GetWidth() const override { return 0; } 88 int GetWidth() const override { return 0; }
91 bool IsImeComposing() const override { return false; } 89 bool IsImeComposing() const override { return false; }
92 int GetOmniboxTextLength() const override { return 0; } 90 int GetOmniboxTextLength() const override { return 0; }
93 void EmphasizeURLComponents() override {} 91 void EmphasizeURLComponents() override {}
94 92
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 nullptr, base::Closure())); 237 nullptr, base::Closure()));
240 provider_client->set_template_url_service(std::move(template_url_service)); 238 provider_client->set_template_url_service(std::move(template_url_service));
241 239
242 return std::move(provider_client); 240 return std::move(provider_client);
243 } 241 }
244 242
245 } // namespace 243 } // namespace
246 244
247 class OmniboxEditTest : public ::testing::Test { 245 class OmniboxEditTest : public ::testing::Test {
248 public: 246 public:
247 OmniboxEditTest()
248 : controller_(&toolbar_model_),
249 view_(&controller_),
250 model_(&view_, &controller_, base::MakeUnique<TestingOmniboxClient>()) {
251 }
252
249 TestToolbarModel* toolbar_model() { return &toolbar_model_; } 253 TestToolbarModel* toolbar_model() { return &toolbar_model_; }
254 const TestingOmniboxView& view() { return view_; }
255 OmniboxEditModel* model() { return &model_; }
250 256
251 private: 257 private:
252 base::MessageLoop message_loop_; 258 base::MessageLoop message_loop_;
253 TestToolbarModel toolbar_model_; 259 TestToolbarModel toolbar_model_;
260 TestingOmniboxEditController controller_;
261 TestingOmniboxView view_;
262 OmniboxEditModel model_;
263
264 DISALLOW_COPY_AND_ASSIGN(OmniboxEditTest);
254 }; 265 };
255 266
256 // Tests various permutations of AutocompleteModel::AdjustTextForCopy. 267 // Tests various permutations of AutocompleteModel::AdjustTextForCopy.
257 TEST_F(OmniboxEditTest, AdjustTextForCopy) { 268 TEST_F(OmniboxEditTest, AdjustTextForCopy) {
258 struct Data { 269 struct Data {
259 const char* perm_text; 270 const char* perm_text;
260 const int sel_start; 271 const int sel_start;
261 const bool is_all_selected; 272 const bool is_all_selected;
262 const char* input; 273 const char* input;
263 const char* expected_output; 274 const char* expected_output;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 // Makes sure intranet urls get 'http://' prefixed to them. 311 // Makes sure intranet urls get 'http://' prefixed to them.
301 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false }, 312 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false },
302 313
303 // Verifies a search term 'foo' doesn't end up with http. 314 // Verifies a search term 'foo' doesn't end up with http.
304 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false }, 315 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false },
305 316
306 // Makes sure extracted search terms are not modified. 317 // Makes sure extracted search terms are not modified.
307 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false, 318 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false,
308 "", true }, 319 "", true },
309 }; 320 };
310 TestingOmniboxEditController controller(toolbar_model());
311 TestingOmniboxView view(&controller);
312 OmniboxEditModel model(&view, &controller,
313 base::WrapUnique(new TestingOmniboxClient()));
314 321
315 for (size_t i = 0; i < arraysize(input); ++i) { 322 for (size_t i = 0; i < arraysize(input); ++i) {
316 toolbar_model()->set_text(ASCIIToUTF16(input[i].perm_text)); 323 toolbar_model()->set_text(base::ASCIIToUTF16(input[i].perm_text));
317 model.UpdatePermanentText(); 324 model()->UpdatePermanentText();
318 325
319 toolbar_model()->set_perform_search_term_replacement( 326 toolbar_model()->set_perform_search_term_replacement(
320 input[i].extracted_search_terms); 327 input[i].extracted_search_terms);
321 328
322 base::string16 result = ASCIIToUTF16(input[i].input); 329 base::string16 result = base::ASCIIToUTF16(input[i].input);
323 GURL url; 330 GURL url;
324 bool write_url; 331 bool write_url;
325 model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected, 332 model()->AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected,
326 &result, &url, &write_url); 333 &result, &url, &write_url);
327 EXPECT_EQ(ASCIIToUTF16(input[i].expected_output), result) << "@: " << i; 334 EXPECT_EQ(base::ASCIIToUTF16(input[i].expected_output), result) << "@: "
335 << i;
328 EXPECT_EQ(input[i].write_url, write_url) << " @" << i; 336 EXPECT_EQ(input[i].write_url, write_url) << " @" << i;
329 if (write_url) 337 if (write_url)
330 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i; 338 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i;
331 } 339 }
332 } 340 }
333 341
334 TEST_F(OmniboxEditTest, InlineAutocompleteText) { 342 TEST_F(OmniboxEditTest, InlineAutocompleteText) {
335 TestingOmniboxEditController controller(toolbar_model()); 343 // Test if the model updates the inline autocomplete text in the view.
336 TestingOmniboxView view(&controller); 344 EXPECT_EQ(base::string16(), view().inline_autocomplete_text());
337 OmniboxEditModel model(&view, &controller, 345 model()->SetUserText(base::ASCIIToUTF16("he"));
338 base::WrapUnique(new TestingOmniboxClient())); 346 model()->OnPopupDataChanged(base::ASCIIToUTF16("llo"), nullptr,
347 base::string16(), false);
348 EXPECT_EQ(base::ASCIIToUTF16("hello"), view().GetText());
349 EXPECT_EQ(base::ASCIIToUTF16("llo"), view().inline_autocomplete_text());
339 350
340 // Test if the model updates the inline autocomplete text in the view. 351 base::string16 text_before = base::ASCIIToUTF16("he");
341 EXPECT_EQ(base::string16(), view.inline_autocomplete_text()); 352 base::string16 text_after = base::ASCIIToUTF16("hel");
342 model.SetUserText(UTF8ToUTF16("he"));
343 model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false);
344 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
345 EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text());
346
347 base::string16 text_before = UTF8ToUTF16("he");
348 base::string16 text_after = UTF8ToUTF16("hel");
349 OmniboxView::StateChanges state_changes{ 353 OmniboxView::StateChanges state_changes{
350 &text_before, &text_after, 3, 3, false, true, false, false}; 354 &text_before, &text_after, 3, 3, false, true, false, false};
351 model.OnAfterPossibleChange(state_changes, true); 355 model()->OnAfterPossibleChange(state_changes, true);
352 EXPECT_EQ(base::string16(), view.inline_autocomplete_text()); 356 EXPECT_EQ(base::string16(), view().inline_autocomplete_text());
353 model.OnPopupDataChanged(UTF8ToUTF16("lo"), NULL, base::string16(), false); 357 model()->OnPopupDataChanged(base::ASCIIToUTF16("lo"), nullptr,
354 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText()); 358 base::string16(), false);
355 EXPECT_EQ(UTF8ToUTF16("lo"), view.inline_autocomplete_text()); 359 EXPECT_EQ(base::ASCIIToUTF16("hello"), view().GetText());
360 EXPECT_EQ(base::ASCIIToUTF16("lo"), view().inline_autocomplete_text());
356 361
357 model.Revert(); 362 model()->Revert();
358 EXPECT_EQ(base::string16(), view.GetText()); 363 EXPECT_EQ(base::string16(), view().GetText());
359 EXPECT_EQ(base::string16(), view.inline_autocomplete_text()); 364 EXPECT_EQ(base::string16(), view().inline_autocomplete_text());
360 365
361 model.SetUserText(UTF8ToUTF16("he")); 366 model()->SetUserText(base::ASCIIToUTF16("he"));
362 model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false); 367 model()->OnPopupDataChanged(base::ASCIIToUTF16("llo"), nullptr,
363 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText()); 368 base::string16(), false);
364 EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text()); 369 EXPECT_EQ(base::ASCIIToUTF16("hello"), view().GetText());
370 EXPECT_EQ(base::ASCIIToUTF16("llo"), view().inline_autocomplete_text());
365 371
366 model.AcceptTemporaryTextAsUserText(); 372 model()->AcceptTemporaryTextAsUserText();
367 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText()); 373 EXPECT_EQ(base::ASCIIToUTF16("hello"), view().GetText());
368 EXPECT_EQ(base::string16(), view.inline_autocomplete_text()); 374 EXPECT_EQ(base::string16(), view().inline_autocomplete_text());
369 } 375 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698