Chromium Code Reviews| Index: chrome/browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm |
| diff --git a/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f2e375794c420518adb980d198a49d33117774e6 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/location_bar/location_bar_view_mac_unittest.mm |
| @@ -0,0 +1,339 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "base/macros.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_command_controller.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| +#import "chrome/browser/ui/cocoa/location_bar/security_state_bubble_decoration.h" |
| +#include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h" |
| +#include "components/toolbar/test_toolbar_model.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
|
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.
|
| +// Mocks the OmniboxView so that we can set if the omnibox is empty or not. |
| +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.
|
| + public: |
| + MockOmniboxView(OmniboxEditController* controller, |
| + Profile* profile, |
| + CommandUpdater* command_updater, |
| + AutocompleteTextField* field) |
| + : OmniboxViewMac(controller, profile, command_updater, field) {} |
| + |
| + // Sets if the MockOmniboxView should be empty or not. |
| + 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.
|
| + |
| + // Returns 0 if |is_empty_| is false. Otherwise, return 1. |
| + int GetOmniboxTextLength() const override { return is_empty_ ? 0 : 1; } |
| + |
| + private: |
| + // True if the OmniboxView should be empty. |
| + 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.
|
| +}; |
|
Robert Sesek
2017/02/23 21:06:50
DISALLOW_COPY_AND_ASSIGN
spqchan
2017/02/23 22:45:06
Done.
|
| + |
| +// Testing class for LocationBarViewMac. This class is used mock the |
| +// ToolbarModel. |
| +class TestingLocationBarViewMac : public LocationBarViewMac { |
| + public: |
| + TestingLocationBarViewMac(AutocompleteTextField* field, |
| + CommandUpdater* command_updater, |
| + Profile* profile, |
| + Browser* browser) |
| + : LocationBarViewMac(field, command_updater, profile, browser) {} |
| + |
| + // Overridden so that LocationBarViewMac will use the test ToolbarModel |
| + // instead. |
| + const ToolbarModel* GetToolbarModel() const override { |
| + return &toolbar_model_; |
| + } |
| + |
| + // Overridden so that LocationBarViewMac will use the test ToolbarModel |
| + // instead. |
| + ToolbarModel* GetToolbarModel() override { return &toolbar_model_; } |
| + |
| + // Sets the security level of |toolbar_model_|. |
| + void SetSecurityLevel(security_state::SecurityLevel level) { |
| + toolbar_model_.set_security_level(level); |
| + } |
| + |
| + private: |
| + // The toolbar model used for testing. |
| + TestToolbarModel toolbar_model_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestingLocationBarViewMac); |
| +}; |
| + |
| +// Testing class for TestingSecurityStateBubbleDecoration. |
| +class TestingSecurityStateBubbleDecoration |
| + : public SecurityStateBubbleDecoration { |
| + public: |
| + TestingSecurityStateBubbleDecoration(LocationIconDecoration* location_icon, |
| + LocationBarViewMac* owner) |
| + : SecurityStateBubbleDecoration(location_icon, owner) {} |
| + |
| + void AnimateIn(bool image_fade = true) override { |
| + has_animated_ = true; |
| + is_showing_ = true; |
| + SecurityStateBubbleDecoration::AnimateIn(image_fade); |
| + } |
| + |
| + void AnimateOut() override { |
| + has_animated_ = true; |
| + is_showing_ = false; |
| + SecurityStateBubbleDecoration::AnimateOut(); |
| + } |
| + |
| + void ShowWithoutAnimation() override { |
| + is_showing_ = true; |
| + has_animated_ = false; |
| + SecurityStateBubbleDecoration::ShowWithoutAnimation(); |
| + } |
| + |
| + void ResetAnimation() override { |
| + is_showing_ = false; |
| + has_animated_ = false; |
| + SecurityStateBubbleDecoration::ResetAnimation(); |
| + } |
| + |
| + bool has_animated() const { return has_animated_; }; |
| + |
| + bool is_showing() const { return is_showing_; }; |
| + |
| + void ResetAnimationFlag() { has_animated_ = false; } |
| + |
| + private: |
| + // True if the decoration has animated. |
| + bool has_animated_; |
|
Robert Sesek
2017/02/23 21:06:51
Initialize these.
spqchan
2017/02/23 22:45:06
Done.
|
| + |
| + // True if the decoration is showing. |
| + bool is_showing_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestingSecurityStateBubbleDecoration); |
| +}; |
| + |
| +class LocationBarViewMacTest : public CocoaProfileTest { |
| + public: |
| + void SetUp() override { |
| + CocoaProfileTest::SetUp(); |
| + ASSERT_TRUE(browser()); |
| + |
| + // Width must be large, otherwise it'll be too narrow to fit the security |
| + // state decoration. |
| + NSRect frame = NSMakeRect(0, 0, 500, 30); |
| + field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); |
| + |
| + location_bar_.reset(new TestingLocationBarViewMac( |
| + field_.get(), browser()->command_controller()->command_updater(), |
| + browser()->profile(), browser())); |
| + |
| + location_bar_->security_state_bubble_decoration_.reset( |
| + new TestingSecurityStateBubbleDecoration( |
| + location_bar_->location_icon_decoration_.get(), |
| + location_bar_.get())); |
| + decoration()->disable_animations_during_testing_ = true; |
| + |
| + omnibox_view_ = new MockOmniboxView( |
| + nullptr, browser()->profile(), |
| + browser()->command_controller()->command_updater(), field_.get()); |
| + |
| + location_bar_->omnibox_view_.reset(omnibox_view_); |
| + } |
| + |
| + void TearDown() override { |
| + location_bar_.reset(); |
| + CocoaProfileTest::TearDown(); |
| + } |
| + |
| + TestingSecurityStateBubbleDecoration* decoration() const { |
| + TestingSecurityStateBubbleDecoration* decoration = |
| + static_cast<TestingSecurityStateBubbleDecoration*>( |
| + location_bar_->security_state_bubble_decoration_.get()); |
| + |
| + return decoration; |
| + } |
| + |
| + TestingLocationBarViewMac* location_bar() const { |
| + return location_bar_.get(); |
| + } |
| + |
| + MockOmniboxView* omnibox_view() const { |
| + MockOmniboxView* omnibox_view = |
| + static_cast<MockOmniboxView*>(location_bar_->omnibox_view_.get()); |
| + return omnibox_view; |
| + } |
| + |
| + AutocompleteTextField* field() const { return field_.get(); } |
| + |
| + void UpdateSecurityState(bool tab_changed) { |
| + location_bar_->Layout(); |
| + location_bar_->UpdateSecurityState(tab_changed); |
| + } |
| + |
| + protected: |
| + LocationBarViewMacTest() {} |
| + |
| + private: |
| + // The LocationBarView object we're testing. |
| + std::unique_ptr<TestingLocationBarViewMac> location_bar_; |
| + |
| + // The autocomplete text field. |
| + base::scoped_nsobject<AutocompleteTextField> field_; |
| + |
| + // 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.
|
| + MockOmniboxView* omnibox_view_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LocationBarViewMacTest); |
| +}; |
| + |
| +// 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.
|
| +TEST_F(LocationBarViewMacTest, ShowAndAnimateSecurityDecoration) { |
| + location_bar()->SetSecurityLevel(security_state::DANGEROUS); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + location_bar()->SetSecurityLevel(security_state::NONE); |
| + UpdateSecurityState(false); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + location_bar()->SetSecurityLevel(security_state::SECURE); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + location_bar()->SetSecurityLevel(security_state::EV_SECURE); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + location_bar()->SetSecurityLevel(security_state::NONE); |
| + UpdateSecurityState(false); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| +} |
| + |
| +// Tests to see if the security decoration does not animate correctly when the |
| +// omnibox is updated from a switched tab. |
| +TEST_F(LocationBarViewMacTest, SecurityDecorationWithTabChanges) { |
| + // Show nonsecure decoration. |
| + location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Switch to a tab with no decoration. |
| + location_bar()->SetSecurityLevel(security_state::NONE); |
| + UpdateSecurityState(true); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Switch back to the tab with the nonsecure decoration. |
| + location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING); |
| + UpdateSecurityState(true); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Show the secure decoration |
|
Robert Sesek
2017/02/23 21:06:51
nit: punctuation
spqchan
2017/02/23 22:45:06
Done.
|
| + location_bar()->SetSecurityLevel(security_state::SECURE); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Switch to a tab with no decoration. |
| + location_bar()->SetSecurityLevel(security_state::NONE); |
| + UpdateSecurityState(true); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| +} |
| + |
| +// Tests to see if the security decoration shows and animates correctly when |
| +// omnibox is empty. |
| +TEST_F(LocationBarViewMacTest, SecurityDecorationWithEmptyOmnibox) { |
| + // Set the omnibox to empty and then set the security level to nonsecure. |
| + // The decoration should not appear. |
| + omnibox_view()->SetIsEmpty(true); |
| + location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING); |
| + UpdateSecurityState(false); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Set the omnibox to nonempty. The decoration should now appear. |
| + omnibox_view()->SetIsEmpty(false); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| +} |
| + |
| +// Tests to see that the security decoration animates out when the omnibox's |
| +// width becomes narrow. |
| +TEST_F(LocationBarViewMacTest, SecurityDecorationWidthChanges) { |
| + // Show the nonsecure decoration. |
| + location_bar()->SetSecurityLevel(security_state::HTTP_SHOW_WARNING); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Make the omnibox narrow. |
| + [field() setFrame:NSMakeRect(0, 0, 119, 30)]; |
| + UpdateSecurityState(false); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Make the omnibox wide again. |
| + [field() setFrame:NSMakeRect(0, 0, 500, 30)]; |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Show secure decoration. |
| + location_bar()->SetSecurityLevel(security_state::SECURE); |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_FALSE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Make the omnibox narrow. |
| + [field() setFrame:NSMakeRect(0, 0, 119, 30)]; |
| + UpdateSecurityState(false); |
| + EXPECT_FALSE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| + |
| + decoration()->ResetAnimationFlag(); |
| + |
| + // Make the omnibox wide again. |
| + [field() setFrame:NSMakeRect(0, 0, 500, 30)]; |
| + UpdateSecurityState(false); |
| + EXPECT_TRUE(decoration()->is_showing()); |
| + EXPECT_TRUE(decoration()->has_animated()); |
| +} |