Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / password_generation_popup_view_cocoa_unittest.mm
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.
4
5 #import "chrome/browser/ui/cocoa/autofill/password_generation_popup_view_cocoa.h"
6
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
16 using testing::AtLeast;
17 using testing::Return;
18
19 namespace {
20
21 class MockPasswordGenerationPopupController
22    : public autofill::PasswordGenerationPopupController {
23  public:
24   MockPasswordGenerationPopupController()
25     : help_text_(base::ASCIIToUTF16("Help me if you can I'm feeling dooown")),
26       popup_bounds_(gfx::Rect(0, 0, 200, 100)) {}
27
28   virtual void OnSavedPasswordsLinkClicked() OVERRIDE {}
29
30   virtual int GetMinimumWidth() OVERRIDE { return 200; }
31
32   virtual bool display_password() const OVERRIDE { return true; }
33
34   virtual bool password_selected() const OVERRIDE { return false; }
35
36   MOCK_CONST_METHOD0(password, base::string16());
37
38   virtual base::string16 SuggestedText() OVERRIDE {
39     return base::ASCIIToUTF16("Suggested by Chrome");
40   }
41
42   virtual const base::string16& HelpText() OVERRIDE { return help_text_; }
43
44   virtual const gfx::Range& HelpTextLinkRange() OVERRIDE { return link_range_; }
45
46   // AutofillPopupViewDelegate implementation.
47   virtual void Hide() OVERRIDE {}
48   MOCK_METHOD0(ViewDestroyed, void());
49   virtual void SetSelectionAtPoint(const gfx::Point&) OVERRIDE {}
50   virtual bool AcceptSelectedLine() OVERRIDE { return true; }
51   virtual void SelectionCleared() OVERRIDE {}
52   virtual const gfx::Rect& popup_bounds() const OVERRIDE {
53     return popup_bounds_;
54   }
55   MOCK_METHOD0(container_view, gfx::NativeView());
56
57  private:
58   base::string16 help_text_;
59   gfx::Range link_range_;
60
61   const gfx::Rect popup_bounds_;
62
63   DISALLOW_COPY_AND_ASSIGN(MockPasswordGenerationPopupController);
64 };
65
66 class PasswordGenerationPopupViewCocoaTest : public CocoaTest {
67  protected:
68   PasswordGenerationPopupViewCocoaTest()
69     : password_(base::ASCIIToUTF16("wow! such password"))
70   {}
71
72   virtual void SetUp() OVERRIDE {
73     mock_controller_.reset(new MockPasswordGenerationPopupController);
74     EXPECT_CALL(*mock_controller_, password())
75         .WillRepeatedly(Return(password_));
76
77     view_.reset([[PasswordGenerationPopupViewCocoa alloc]
78         initWithController:mock_controller_.get()
79                      frame:NSZeroRect]);
80
81     NSView* contentView = [test_window() contentView];
82     [contentView addSubview:view_];
83     EXPECT_CALL(*mock_controller_, container_view())
84         .WillRepeatedly(Return(contentView));
85   }
86
87   base::string16 password_;
88   scoped_ptr<MockPasswordGenerationPopupController> mock_controller_;
89   base::scoped_nsobject<PasswordGenerationPopupViewCocoa> view_;
90 };
91
92 TEST_VIEW(PasswordGenerationPopupViewCocoaTest, view_);
93
94 TEST_F(PasswordGenerationPopupViewCocoaTest, ShowAndHide) {
95   // Verify that the view fetches a password from the controller.
96   EXPECT_CALL(*mock_controller_, password()).Times(AtLeast(1))
97       .WillRepeatedly(Return(password_));
98
99   view_.reset([[PasswordGenerationPopupViewCocoa alloc]
100       initWithController:mock_controller_.get()
101                    frame:NSZeroRect]);
102
103   [view_ showPopup];
104   [view_ display];
105   [view_ hidePopup];
106 }
107
108 // Verifies that it doesn't crash when the controller is destroyed before the
109 // popup is hidden.
110 TEST_F(PasswordGenerationPopupViewCocoaTest, ControllerDestroyed) {
111   [view_ showPopup];
112   mock_controller_.reset();
113   [view_ controllerDestroyed];
114   [view_ display];
115   [view_ hidePopup];
116 }
117
118 }  // namespace