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