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

Side by Side Diff: chrome/browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm

Issue 2700243002: [Mac] Test for Security State Bubble Decoration (Closed)
Patch Set: Nits Created 3 years, 10 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
OLDNEW
(Empty)
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 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/macros.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_command_controller.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
14 #import "chrome/browser/ui/cocoa/location_bar/security_state_bubble_decoration.h "
15 #include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h"
16 #include "components/toolbar/test_toolbar_model.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
Robert Sesek 2017/02/23 21:06:51 You can place all your test classes inside an anon
spqchan 2017/02/23 22:45:06 Done.
19 // Mocks the OmniboxView so that we can set if the omnibox is empty or not.
20 class MockOmniboxView : public OmniboxViewMac {
Robert Sesek 2017/02/23 21:06:51 Why is this Mock but the rest of the test helpers
spqchan 2017/02/23 22:45:06 This is Mock because we're mocking it but we're no
Robert Sesek 2017/02/24 17:00:39 Acknowledged.
21 public:
22 MockOmniboxView(OmniboxEditController* controller,
23 Profile* profile,
24 CommandUpdater* command_updater,
25 AutocompleteTextField* field)
26 : OmniboxViewMac(controller, profile, command_updater, field) {}
27
28 // Sets if the MockOmniboxView should be empty or not.
29 void SetIsEmpty(bool is_empty) { is_empty_ = is_empty; }
Robert Sesek 2017/02/23 21:06:50 Generally use hacker_case for simple setters.
spqchan 2017/02/23 22:45:06 Done.
30
31 // Returns 0 if |is_empty_| is false. Otherwise, return 1.
32 int GetOmniboxTextLength() const override { return is_empty_ ? 0 : 1; }
33
34 private:
35 // True if the OmniboxView should be empty.
36 bool is_empty_;
Robert Sesek 2017/02/23 21:06:50 Initialize this in the ctor or inline.
spqchan 2017/02/23 22:45:06 Done.
37 };
Robert Sesek 2017/02/23 21:06:50 DISALLOW_COPY_AND_ASSIGN
spqchan 2017/02/23 22:45:06 Done.
38
39 // Testing class for LocationBarViewMac. This class is used mock the
40 // ToolbarModel.
41 class TestingLocationBarViewMac : public LocationBarViewMac {
42 public:
43 TestingLocationBarViewMac(AutocompleteTextField* field,
44 CommandUpdater* command_updater,
45 Profile* profile,
46 Browser* browser)
47 : LocationBarViewMac(field, command_updater, profile, browser) {}
48
49 // Overridden so that LocationBarViewMac will use the test ToolbarModel
50 // instead.
51 const ToolbarModel* GetToolbarModel() const override {
52 return &toolbar_model_;
53 }
54
55 // Overridden so that LocationBarViewMac will use the test ToolbarModel
56 // instead.
57 ToolbarModel* GetToolbarModel() override { return &toolbar_model_; }
58
59 // Sets the security level of |toolbar_model_|.
60 void SetSecurityLevel(security_state::SecurityLevel level) {
61 toolbar_model_.set_security_level(level);
62 }
63
64 private:
65 // The toolbar model used for testing.
66 TestToolbarModel toolbar_model_;
67
68 DISALLOW_COPY_AND_ASSIGN(TestingLocationBarViewMac);
69 };
70
71 // Testing class for TestingSecurityStateBubbleDecoration.
72 class TestingSecurityStateBubbleDecoration
73 : public SecurityStateBubbleDecoration {
74 public:
75 TestingSecurityStateBubbleDecoration(LocationIconDecoration* location_icon,
76 LocationBarViewMac* owner)
77 : SecurityStateBubbleDecoration(location_icon, owner) {}
78
79 void AnimateIn(bool image_fade = true) override {
80 has_animated_ = true;
81 is_showing_ = true;
82 SecurityStateBubbleDecoration::AnimateIn(image_fade);
83 }
84
85 void AnimateOut() override {
86 has_animated_ = true;
87 is_showing_ = false;
88 SecurityStateBubbleDecoration::AnimateOut();
89 }
90
91 void ShowWithoutAnimation() override {
92 is_showing_ = true;
93 has_animated_ = false;
94 SecurityStateBubbleDecoration::ShowWithoutAnimation();
95 }
96
97 void ResetAnimation() override {
98 is_showing_ = false;
99 has_animated_ = false;
100 SecurityStateBubbleDecoration::ResetAnimation();
101 }
102
103 bool has_animated() const { return has_animated_; };
104
105 bool is_showing() const { return is_showing_; };
106
107 void ResetAnimationFlag() { has_animated_ = false; }
108
109 private:
110 // True if the decoration has animated.
111 bool has_animated_;
Robert Sesek 2017/02/23 21:06:51 Initialize these.
spqchan 2017/02/23 22:45:06 Done.
112
113 // True if the decoration is showing.
114 bool is_showing_;
115
116 DISALLOW_COPY_AND_ASSIGN(TestingSecurityStateBubbleDecoration);
117 };
118
119 class LocationBarViewMacTest : public CocoaProfileTest {
120 public:
121 void SetUp() override {
122 CocoaProfileTest::SetUp();
123 ASSERT_TRUE(browser());
124
125 // Width must be large, otherwise it'll be too narrow to fit the security
126 // state decoration.
127 NSRect frame = NSMakeRect(0, 0, 500, 30);
128 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]);
129
130 location_bar_.reset(new TestingLocationBarViewMac(
131 field_.get(), browser()->command_controller()->command_updater(),
132 browser()->profile(), browser()));
133
134 location_bar_->security_state_bubble_decoration_.reset(
135 new TestingSecurityStateBubbleDecoration(
136 location_bar_->location_icon_decoration_.get(),
137 location_bar_.get()));
138 decoration()->disable_animations_during_testing_ = true;
139
140 omnibox_view_ = new MockOmniboxView(
141 nullptr, browser()->profile(),
142 browser()->command_controller()->command_updater(), field_.get());
143
144 location_bar_->omnibox_view_.reset(omnibox_view_);
145 }
146
147 void TearDown() override {
148 location_bar_.reset();
149 CocoaProfileTest::TearDown();
150 }
151
152 TestingSecurityStateBubbleDecoration* decoration() const {
153 TestingSecurityStateBubbleDecoration* decoration =
154 static_cast<TestingSecurityStateBubbleDecoration*>(
155 location_bar_->security_state_bubble_decoration_.get());
156
157 return decoration;
158 }
159
160 TestingLocationBarViewMac* location_bar() const {
161 return location_bar_.get();
162 }
163
164 MockOmniboxView* omnibox_view() const {
165 MockOmniboxView* omnibox_view =
166 static_cast<MockOmniboxView*>(location_bar_->omnibox_view_.get());
167 return omnibox_view;
168 }
169
170 AutocompleteTextField* field() const { return field_.get(); }
171
172 void UpdateSecurityState(bool tab_changed) {
173 location_bar_->Layout();
174 location_bar_->UpdateSecurityState(tab_changed);
175 }
176
177 protected:
178 LocationBarViewMacTest() {}
179
180 private:
181 // The LocationBarView object we're testing.
182 std::unique_ptr<TestingLocationBarViewMac> location_bar_;
183
184 // The autocomplete text field.
185 base::scoped_nsobject<AutocompleteTextField> field_;
186
187 // The mocked omnibox view.
Robert Sesek 2017/02/23 21:06:50 "Weak, owned by location_bar_."
spqchan 2017/02/23 22:45:07 Done.
188 MockOmniboxView* omnibox_view_;
189
190 DISALLOW_COPY_AND_ASSIGN(LocationBarViewMacTest);
191 };
192
193 // Tests to see if the security decoration shows and animates correctly.
Robert Sesek 2017/02/23 21:06:50 Rather than just say "correctly", I'd describe wha
spqchan 2017/02/23 22:45:07 Done.
194 TEST_F(LocationBarViewMacTest, ShowAndAnimateSecurityDecoration) {
195 location_bar()->SetSecurityLevel(security_state::DANGEROUS);
196 UpdateSecurityState(false);
197 EXPECT_TRUE(decoration()->is_showing());
198 EXPECT_TRUE(decoration()->has_animated());
199
200 decoration()->ResetAnimationFlag();
201
202 location_bar()->SetSecurityLevel(security_state::NONE);
203 UpdateSecurityState(false);
204 EXPECT_FALSE(decoration()->is_showing());
205 EXPECT_TRUE(decoration()->has_animated());
206
207 decoration()->ResetAnimationFlag();
208
209 location_bar()->SetSecurityLevel(security_state::SECURE);
210 UpdateSecurityState(false);
211 EXPECT_TRUE(decoration()->is_showing());
212 EXPECT_FALSE(decoration()->has_animated());
213
214 decoration()->ResetAnimationFlag();
215
216 location_bar()->SetSecurityLevel(security_state::EV_SECURE);
217 UpdateSecurityState(false);
218 EXPECT_TRUE(decoration()->is_showing());
219 EXPECT_FALSE(decoration()->has_animated());
220
221 decoration()->ResetAnimationFlag();
222
223 location_bar()->SetSecurityLevel(security_state::NONE);
224 UpdateSecurityState(false);
225 EXPECT_FALSE(decoration()->is_showing());
226 EXPECT_FALSE(decoration()->has_animated());
227 }
228
229 // Tests to see if the security decoration does not animate correctly when the
230 // omnibox is updated from a switched tab.
231 TEST_F(LocationBarViewMacTest, SecurityDecorationWithTabChanges) {
232 // Show nonsecure decoration.
233 location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING);
234 UpdateSecurityState(false);
235 EXPECT_TRUE(decoration()->is_showing());
236 EXPECT_TRUE(decoration()->has_animated());
237
238 decoration()->ResetAnimationFlag();
239
240 // Switch to a tab with no decoration.
241 location_bar()->SetSecurityLevel(security_state::NONE);
242 UpdateSecurityState(true);
243 EXPECT_FALSE(decoration()->is_showing());
244 EXPECT_FALSE(decoration()->has_animated());
245
246 decoration()->ResetAnimationFlag();
247
248 // Switch back to the tab with the nonsecure decoration.
249 location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING);
250 UpdateSecurityState(true);
251 EXPECT_TRUE(decoration()->is_showing());
252 EXPECT_FALSE(decoration()->has_animated());
253
254 decoration()->ResetAnimationFlag();
255
256 // Show the secure decoration
Robert Sesek 2017/02/23 21:06:51 nit: punctuation
spqchan 2017/02/23 22:45:06 Done.
257 location_bar()->SetSecurityLevel(security_state::SECURE);
258 UpdateSecurityState(false);
259 EXPECT_TRUE(decoration()->is_showing());
260 EXPECT_FALSE(decoration()->has_animated());
261
262 decoration()->ResetAnimationFlag();
263
264 // Switch to a tab with no decoration.
265 location_bar()->SetSecurityLevel(security_state::NONE);
266 UpdateSecurityState(true);
267 EXPECT_FALSE(decoration()->is_showing());
268 EXPECT_FALSE(decoration()->has_animated());
269 }
270
271 // Tests to see if the security decoration shows and animates correctly when
272 // omnibox is empty.
273 TEST_F(LocationBarViewMacTest, SecurityDecorationWithEmptyOmnibox) {
274 // Set the omnibox to empty and then set the security level to nonsecure.
275 // The decoration should not appear.
276 omnibox_view()->SetIsEmpty(true);
277 location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING);
278 UpdateSecurityState(false);
279 EXPECT_FALSE(decoration()->is_showing());
280 EXPECT_FALSE(decoration()->has_animated());
281
282 decoration()->ResetAnimationFlag();
283
284 // Set the omnibox to nonempty. The decoration should now appear.
285 omnibox_view()->SetIsEmpty(false);
286 UpdateSecurityState(false);
287 EXPECT_TRUE(decoration()->is_showing());
288 EXPECT_TRUE(decoration()->has_animated());
289 }
290
291 // Tests to see that the security decoration animates out when the omnibox's
292 // width becomes narrow.
293 TEST_F(LocationBarViewMacTest, SecurityDecorationWidthChanges) {
294 // Show the nonsecure decoration.
295 location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING);
296 UpdateSecurityState(false);
297 EXPECT_TRUE(decoration()->is_showing());
298 EXPECT_TRUE(decoration()->has_animated());
299
300 decoration()->ResetAnimationFlag();
301
302 // Make the omnibox narrow.
303 [field() setFrame:NSMakeRect(0, 0, 119, 30)];
304 UpdateSecurityState(false);
305 EXPECT_FALSE(decoration()->is_showing());
306 EXPECT_TRUE(decoration()->has_animated());
307
308 decoration()->ResetAnimationFlag();
309
310 // Make the omnibox wide again.
311 [field() setFrame:NSMakeRect(0, 0, 500, 30)];
312 UpdateSecurityState(false);
313 EXPECT_TRUE(decoration()->is_showing());
314 EXPECT_TRUE(decoration()->has_animated());
315
316 decoration()->ResetAnimationFlag();
317
318 // Show secure decoration.
319 location_bar()->SetSecurityLevel(security_state::SECURE);
320 UpdateSecurityState(false);
321 EXPECT_TRUE(decoration()->is_showing());
322 EXPECT_FALSE(decoration()->has_animated());
323
324 decoration()->ResetAnimationFlag();
325
326 // Make the omnibox narrow.
327 [field() setFrame:NSMakeRect(0, 0, 119, 30)];
328 UpdateSecurityState(false);
329 EXPECT_FALSE(decoration()->is_showing());
330 EXPECT_TRUE(decoration()->has_animated());
331
332 decoration()->ResetAnimationFlag();
333
334 // Make the omnibox wide again.
335 [field() setFrame:NSMakeRect(0, 0, 500, 30)];
336 UpdateSecurityState(false);
337 EXPECT_TRUE(decoration()->is_showing());
338 EXPECT_TRUE(decoration()->has_animated());
339 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698