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"
16 using testing::AtLeast;
17 using testing::Return;
21 class MockPasswordGenerationPopupController
22 : public autofill::PasswordGenerationPopupController {
24 MockPasswordGenerationPopupController()
25 : help_text_(base::ASCIIToUTF16("Help me if you can I'm feeling dooown")),
26 font_list_("Lucida Grande, 12px"),
27 popup_bounds_(gfx::Rect(0, 0, 200, 100)) {}
29 virtual void OnSavedPasswordsLinkClicked() OVERRIDE {}
31 virtual const gfx::FontList& font_list() const OVERRIDE {
35 virtual const gfx::Rect& password_bounds() const OVERRIDE {
36 return password_bounds_;
39 virtual const gfx::Rect& divider_bounds() const OVERRIDE {
40 return divider_bounds_;
43 virtual const gfx::Rect& help_bounds() const OVERRIDE {
47 virtual bool display_password() const OVERRIDE { return true; }
49 virtual bool password_selected() const OVERRIDE { return false; }
51 MOCK_CONST_METHOD0(password, base::string16());
53 virtual base::string16 SuggestedText() OVERRIDE {
54 return base::ASCIIToUTF16("Suggested by Chrome");
57 virtual const base::string16& HelpText() OVERRIDE { return help_text_; }
59 virtual const gfx::Range& HelpTextLinkRange() OVERRIDE { return link_range_; }
61 // AutofillPopupViewDelegate implementation.
62 virtual void Hide() OVERRIDE {}
63 MOCK_METHOD0(ViewDestroyed, void());
64 virtual void SetSelectionAtPoint(const gfx::Point&) OVERRIDE {}
65 virtual bool AcceptSelectedLine() OVERRIDE { return true; }
66 virtual void SelectionCleared() OVERRIDE {}
67 virtual const gfx::Rect& popup_bounds() const OVERRIDE {
70 MOCK_METHOD0(container_view, gfx::NativeView());
73 base::string16 help_text_;
74 const gfx::FontList font_list_;
75 gfx::Range link_range_;
77 gfx::Rect password_bounds_;
78 gfx::Rect divider_bounds_;
79 gfx::Rect help_bounds_;
80 const gfx::Rect popup_bounds_;
82 DISALLOW_COPY_AND_ASSIGN(MockPasswordGenerationPopupController);
85 class PasswordGenerationPopupViewCocoaTest : public CocoaTest {
87 PasswordGenerationPopupViewCocoaTest()
88 : password_(base::ASCIIToUTF16("wow! such password"))
91 virtual void SetUp() OVERRIDE {
92 mock_controller_.reset(new MockPasswordGenerationPopupController);
93 EXPECT_CALL(*mock_controller_, password())
94 .WillRepeatedly(Return(password_));
96 view_.reset([[PasswordGenerationPopupViewCocoa alloc]
97 initWithController:mock_controller_.get()
100 NSView* contentView = [test_window() contentView];
101 [contentView addSubview:view_];
102 EXPECT_CALL(*mock_controller_, container_view())
103 .WillRepeatedly(Return(contentView));
106 base::string16 password_;
107 scoped_ptr<MockPasswordGenerationPopupController> mock_controller_;
108 base::scoped_nsobject<PasswordGenerationPopupViewCocoa> view_;
111 TEST_VIEW(PasswordGenerationPopupViewCocoaTest, view_);
113 TEST_F(PasswordGenerationPopupViewCocoaTest, ShowAndHide) {
114 // Verify that the view fetches a password from the controller.
115 EXPECT_CALL(*mock_controller_, password()).Times(AtLeast(1))
116 .WillRepeatedly(Return(password_));
118 view_.reset([[PasswordGenerationPopupViewCocoa alloc]
119 initWithController:mock_controller_.get()
127 // Verifies that it doesn't crash when the controller is destroyed before the
129 TEST_F(PasswordGenerationPopupViewCocoaTest, ControllerDestroyed) {
131 mock_controller_.reset();
132 [view_ controllerDestroyed];