1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
13 #include "ui/gfx/font_list.h"
14 #include "ui/gfx/range/range.h"
15 #include "ui/gfx/rect_f.h"
17 using testing::AtLeast;
18 using testing::Return;
22 class MockPasswordGenerationPopupController
23 : public autofill::PasswordGenerationPopupController {
25 MockPasswordGenerationPopupController()
26 : help_text_(base::ASCIIToUTF16("Help me if you can I'm feeling dooown")),
27 popup_bounds_(gfx::Rect(0, 0, 200, 100)) {}
29 MOCK_METHOD0(PasswordAccepted, void());
31 virtual void OnSavedPasswordsLinkClicked() override {}
33 virtual int GetMinimumWidth() override { return 200; }
35 virtual bool display_password() const override { return true; }
37 virtual bool password_selected() const override { return false; }
39 MOCK_CONST_METHOD0(password, base::string16());
41 virtual base::string16 SuggestedText() override {
42 return base::ASCIIToUTF16("Suggested by Chrome");
45 virtual base::string16 AccessibleName() override {
46 return base::ASCIIToUTF16("Password Suggestion");
49 virtual const base::string16& HelpText() override { return help_text_; }
51 virtual const gfx::Range& HelpTextLinkRange() override { return link_range_; }
53 // AutofillPopupViewDelegate implementation.
54 virtual void Hide() override {}
55 MOCK_METHOD0(ViewDestroyed, void());
56 virtual void SetSelectionAtPoint(const gfx::Point&) override {}
57 virtual bool AcceptSelectedLine() override { return true; }
58 virtual void SelectionCleared() override {}
59 virtual const gfx::Rect& popup_bounds() const override {
62 MOCK_METHOD0(container_view, gfx::NativeView());
63 MOCK_CONST_METHOD0(element_bounds, gfx::RectF&());
64 MOCK_CONST_METHOD0(IsRTL, bool());
67 base::string16 help_text_;
68 gfx::Range link_range_;
70 const gfx::Rect popup_bounds_;
72 DISALLOW_COPY_AND_ASSIGN(MockPasswordGenerationPopupController);
75 class PasswordGenerationPopupViewCocoaTest : public CocoaTest {
77 PasswordGenerationPopupViewCocoaTest()
78 : password_(base::ASCIIToUTF16("wow! such password"))
81 virtual void SetUp() override {
82 mock_controller_.reset(new MockPasswordGenerationPopupController);
83 EXPECT_CALL(*mock_controller_, password())
84 .WillRepeatedly(Return(password_));
86 view_.reset([[PasswordGenerationPopupViewCocoa alloc]
87 initWithController:mock_controller_.get()
90 NSView* contentView = [test_window() contentView];
91 [contentView addSubview:view_];
92 EXPECT_CALL(*mock_controller_, container_view())
93 .WillRepeatedly(Return(contentView));
96 base::string16 password_;
97 scoped_ptr<MockPasswordGenerationPopupController> mock_controller_;
98 base::scoped_nsobject<PasswordGenerationPopupViewCocoa> view_;
101 TEST_VIEW(PasswordGenerationPopupViewCocoaTest, view_);
103 TEST_F(PasswordGenerationPopupViewCocoaTest, ShowAndHide) {
104 // Verify that the view fetches a password from the controller.
105 EXPECT_CALL(*mock_controller_, password()).Times(AtLeast(1))
106 .WillRepeatedly(Return(password_));
108 view_.reset([[PasswordGenerationPopupViewCocoa alloc]
109 initWithController:mock_controller_.get()
117 // Verifies that it doesn't crash when the controller is destroyed before the
119 TEST_F(PasswordGenerationPopupViewCocoaTest, ControllerDestroyed) {
121 mock_controller_.reset();
122 [view_ controllerDestroyed];