Add unit test for the Settings API Bubble.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_popup_controller_impl.h
blob899c50b1458b2bed79facb0d84e457317ccf1c88
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.
5 #ifndef CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/i18n/rtl.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
13 #include "chrome/browser/ui/autofill/popup_controller_common.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/rect_f.h"
18 namespace autofill {
20 class AutofillPopupDelegate;
21 class AutofillPopupView;
23 // This class is a controller for an AutofillPopupView. It implements
24 // AutofillPopupController to allow calls from AutofillPopupView. The
25 // other, public functions are available to its instantiator.
26 class AutofillPopupControllerImpl : public AutofillPopupController {
27 public:
28 // Creates a new |AutofillPopupControllerImpl|, or reuses |previous| if the
29 // construction arguments are the same. |previous| may be invalidated by this
30 // call. The controller will listen for keyboard input routed to
31 // |web_contents| while the popup is showing, unless |web_contents| is NULL.
32 static base::WeakPtr<AutofillPopupControllerImpl> GetOrCreate(
33 base::WeakPtr<AutofillPopupControllerImpl> previous,
34 base::WeakPtr<AutofillPopupDelegate> delegate,
35 content::WebContents* web_contents,
36 gfx::NativeView container_view,
37 const gfx::RectF& element_bounds,
38 base::i18n::TextDirection text_direction);
40 // Shows the popup, or updates the existing popup with the given values.
41 void Show(const std::vector<base::string16>& names,
42 const std::vector<base::string16>& subtexts,
43 const std::vector<base::string16>& icons,
44 const std::vector<int>& identifiers);
46 // Updates the data list values currently shown with the popup.
47 void UpdateDataListValues(const std::vector<base::string16>& values,
48 const std::vector<base::string16>& labels);
50 // Hides the popup and destroys the controller. This also invalidates
51 // |delegate_|.
52 virtual void Hide() OVERRIDE;
54 // Invoked when the view was destroyed by by someone other than this class.
55 virtual void ViewDestroyed() OVERRIDE;
57 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event);
59 // Tells the view to capture mouse events. Must be called before |Show()|.
60 void set_hide_on_outside_click(bool hide_on_outside_click);
62 protected:
63 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest,
64 CloseWidgetAndNoLeaking);
65 FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest,
66 ProperlyResetController);
68 AutofillPopupControllerImpl(base::WeakPtr<AutofillPopupDelegate> delegate,
69 content::WebContents* web_contents,
70 gfx::NativeView container_view,
71 const gfx::RectF& element_bounds,
72 base::i18n::TextDirection text_direction);
73 virtual ~AutofillPopupControllerImpl();
75 // AutofillPopupController implementation.
76 virtual void UpdateBoundsAndRedrawPopup() OVERRIDE;
77 virtual void SetSelectionAtPoint(const gfx::Point& point) OVERRIDE;
78 virtual bool AcceptSelectedLine() OVERRIDE;
79 virtual void SelectionCleared() OVERRIDE;
80 virtual bool ShouldRepostEvent(const ui::MouseEvent& event) OVERRIDE;
81 virtual bool ShouldHideOnOutsideClick() const OVERRIDE;
82 virtual void AcceptSuggestion(size_t index) OVERRIDE;
83 virtual int GetIconResourceID(
84 const base::string16& resource_name) const OVERRIDE;
85 virtual bool CanDelete(size_t index) const OVERRIDE;
86 virtual bool IsWarning(size_t index) const OVERRIDE;
87 virtual gfx::Rect GetRowBounds(size_t index) OVERRIDE;
88 virtual void SetPopupBounds(const gfx::Rect& bounds) OVERRIDE;
89 virtual const gfx::Rect& popup_bounds() const OVERRIDE;
90 virtual gfx::NativeView container_view() OVERRIDE;
91 virtual const gfx::RectF& element_bounds() const OVERRIDE;
92 virtual bool IsRTL() const OVERRIDE;
94 virtual const std::vector<base::string16>& names() const OVERRIDE;
95 virtual const std::vector<base::string16>& subtexts() const OVERRIDE;
96 virtual const std::vector<base::string16>& icons() const OVERRIDE;
97 virtual const std::vector<int>& identifiers() const OVERRIDE;
98 #if !defined(OS_ANDROID)
99 virtual const gfx::FontList& GetNameFontListForRow(
100 size_t index) const OVERRIDE;
101 virtual const gfx::FontList& subtext_font_list() const OVERRIDE;
102 #endif
103 virtual int selected_line() const OVERRIDE;
105 content::WebContents* web_contents();
107 // Change which line is currently selected by the user.
108 void SetSelectedLine(int selected_line);
110 // Increase the selected line by 1, properly handling wrapping.
111 void SelectNextLine();
113 // Decrease the selected line by 1, properly handling wrapping.
114 void SelectPreviousLine();
116 // The user has removed a suggestion.
117 bool RemoveSelectedLine();
119 // Convert a y-coordinate to the closest line.
120 int LineFromY(int y);
122 // Returns the height of a row depending on its type.
123 int GetRowHeightFromId(int identifier) const;
125 // Returns true if the given id refers to an element that can be accepted.
126 bool CanAccept(int id);
128 // Returns true if the popup still has non-options entries to show the user.
129 bool HasSuggestions();
131 // Set the Autofill entry values. Exposed to allow tests to set these values
132 // without showing the popup.
133 void SetValues(const std::vector<base::string16>& names,
134 const std::vector<base::string16>& subtexts,
135 const std::vector<base::string16>& icons,
136 const std::vector<int>& identifier);
138 AutofillPopupView* view() { return view_; }
140 // |view_| pass throughs (virtual for testing).
141 virtual void ShowView();
142 virtual void InvalidateRow(size_t row);
144 // Protected so tests can access.
145 #if !defined(OS_ANDROID)
146 // Calculates the desired width of the popup based on its contents.
147 int GetDesiredPopupWidth() const;
149 // Calculates the desired height of the popup based on its contents.
150 int GetDesiredPopupHeight() const;
152 // Calculate the width of the row, excluding all the text. This provides
153 // the size of the row that won't be reducible (since all the text can be
154 // elided if there isn't enough space).
155 int RowWidthWithoutText(int row) const;
156 #endif
158 base::WeakPtr<AutofillPopupControllerImpl> GetWeakPtr();
160 // Contains common popup functionality such as popup layout. Protected for
161 // testing.
162 scoped_ptr<PopupControllerCommon> controller_common_;
164 private:
165 // Clear the internal state of the controller. This is needed to ensure that
166 // when the popup is reused it doesn't leak values between uses.
167 void ClearState();
169 #if !defined(OS_ANDROID)
170 // Calculates and sets the bounds of the popup, including placing it properly
171 // to prevent it from going off the screen.
172 void UpdatePopupBounds();
173 #endif
175 AutofillPopupView* view_; // Weak reference.
176 base::WeakPtr<AutofillPopupDelegate> delegate_;
178 // The bounds of the Autofill popup.
179 gfx::Rect popup_bounds_;
181 // The text direction of the popup.
182 base::i18n::TextDirection text_direction_;
184 // The current Autofill query values.
185 std::vector<base::string16> names_;
186 std::vector<base::string16> subtexts_;
187 std::vector<base::string16> icons_;
188 std::vector<int> identifiers_;
190 // Since names_ can be elided to ensure that it fits on the screen, we need to
191 // keep an unelided copy of the names to be able to pass to the delegate.
192 std::vector<base::string16> full_names_;
194 #if !defined(OS_ANDROID)
195 // The fonts for the popup text.
196 gfx::FontList name_font_list_;
197 gfx::FontList subtext_font_list_;
198 gfx::FontList warning_font_list_;
199 #endif
201 // The line that is currently selected by the user.
202 // |kNoSelection| indicates that no line is currently selected.
203 int selected_line_;
205 // Whether the popup view should hide on mouse presses outside of it.
206 bool hide_on_outside_click_;
208 base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_;
211 } // namespace autofill
213 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_