1 // Copyright 2017 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 "components/omnibox/browser/test_omnibox_view.h"
6
7 #include <algorithm>
8
9 #include "ui/gfx/native_widget_types.h"
10
11 // static
CreateState(std::string text,size_t sel_start,size_t sel_end,size_t all_sel_length)12 OmniboxView::State TestOmniboxView::CreateState(std::string text,
13 size_t sel_start,
14 size_t sel_end,
15 size_t all_sel_length) {
16 OmniboxView::State state;
17 state.text = base::UTF8ToUTF16(text);
18 state.keyword = base::string16();
19 state.is_keyword_selected = false;
20 state.sel_start = sel_start;
21 state.sel_end = sel_end;
22 state.all_sel_length = all_sel_length;
23 return state;
24 }
25
SetModel(std::unique_ptr<OmniboxEditModel> model)26 void TestOmniboxView::SetModel(std::unique_ptr<OmniboxEditModel> model) {
27 model_ = std::move(model);
28 }
29
GetText() const30 base::string16 TestOmniboxView::GetText() const {
31 return text_;
32 }
33
SetWindowTextAndCaretPos(const base::string16 & text,size_t caret_pos,bool update_popup,bool notify_text_changed)34 void TestOmniboxView::SetWindowTextAndCaretPos(const base::string16& text,
35 size_t caret_pos,
36 bool update_popup,
37 bool notify_text_changed) {
38 text_ = text;
39 selection_ = gfx::Range(caret_pos);
40 }
41
IsSelectAll() const42 bool TestOmniboxView::IsSelectAll() const {
43 return selection_.EqualsIgnoringDirection(gfx::Range(0, text_.size()));
44 }
45
GetSelectionBounds(size_t * start,size_t * end) const46 void TestOmniboxView::GetSelectionBounds(size_t* start, size_t* end) const {
47 *start = selection_.start();
48 *end = selection_.end();
49 }
50
GetAllSelectionsLength() const51 size_t TestOmniboxView::GetAllSelectionsLength() const {
52 return 0;
53 }
54
SelectAll(bool reversed)55 void TestOmniboxView::SelectAll(bool reversed) {
56 if (reversed)
57 selection_ = gfx::Range(text_.size(), 0);
58 else
59 selection_ = gfx::Range(0, text_.size());
60 }
61
OnTemporaryTextMaybeChanged(const base::string16 & display_text,const AutocompleteMatch & match,bool save_original_selection,bool notify_text_changed)62 void TestOmniboxView::OnTemporaryTextMaybeChanged(
63 const base::string16& display_text,
64 const AutocompleteMatch& match,
65 bool save_original_selection,
66 bool notify_text_changed) {
67 text_ = display_text;
68
69 if (save_original_selection)
70 saved_temporary_selection_ = selection_;
71 }
72
OnInlineAutocompleteTextMaybeChanged(const base::string16 & display_text,std::vector<gfx::Range> selections,size_t user_text_length)73 void TestOmniboxView::OnInlineAutocompleteTextMaybeChanged(
74 const base::string16& display_text,
75 std::vector<gfx::Range> selections,
76 size_t user_text_length) {
77 const bool text_changed = text_ != display_text;
78 text_ = display_text;
79 inline_autocompletion_ = display_text.substr(user_text_length);
80
81 // Just like the Views control, only change the selection if the text has
82 // actually changed.
83 if (text_changed)
84 selection_ = gfx::Range(text_.size(), user_text_length);
85 }
86
OnInlineAutocompleteTextCleared()87 void TestOmniboxView::OnInlineAutocompleteTextCleared() {
88 inline_autocompletion_.clear();
89 }
90
OnRevertTemporaryText(const base::string16 & display_text,const AutocompleteMatch & match)91 void TestOmniboxView::OnRevertTemporaryText(const base::string16& display_text,
92 const AutocompleteMatch& match) {
93 selection_ = saved_temporary_selection_;
94 }
95
OnAfterPossibleChange(bool allow_keyword_ui_change)96 bool TestOmniboxView::OnAfterPossibleChange(bool allow_keyword_ui_change) {
97 return false;
98 }
99
GetNativeView() const100 gfx::NativeView TestOmniboxView::GetNativeView() const {
101 return nullptr;
102 }
103
GetRelativeWindowForPopup() const104 gfx::NativeView TestOmniboxView::GetRelativeWindowForPopup() const {
105 return nullptr;
106 }
107
IsImeComposing() const108 bool TestOmniboxView::IsImeComposing() const {
109 return false;
110 }
111
GetOmniboxTextLength() const112 int TestOmniboxView::GetOmniboxTextLength() const {
113 return 0;
114 }
115