Update To 11.40.268.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 #include "ui/gfx/rect_f.h"
16
17 using testing::AtLeast;
18 using testing::Return;
19
20 namespace {
21
22 class MockPasswordGenerationPopupController
23    : public autofill::PasswordGenerationPopupController {
24  public:
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)) {}
28
29   MOCK_METHOD0(PasswordAccepted, void());
30
31   virtual void OnSavedPasswordsLinkClicked() override {}
32
33   virtual int GetMinimumWidth() override { return 200; }
34
35   virtual bool display_password() const override { return true; }
36
37   virtual bool password_selected() const override { return false; }
38
39   MOCK_CONST_METHOD0(password, base::string16());
40
41   virtual base::string16 SuggestedText() override {
42     return base::ASCIIToUTF16("Suggested by Chrome");
43   }
44
45   virtual base::string16 AccessibleName() override {
46     return base::ASCIIToUTF16("Password Suggestion");
47   }
48
49   virtual const base::string16& HelpText() override { return help_text_; }
50
51   virtual const gfx::Range& HelpTextLinkRange() override { return link_range_; }
52
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 {
60     return popup_bounds_;
61   }
62   MOCK_METHOD0(container_view, gfx::NativeView());
63   MOCK_CONST_METHOD0(element_bounds, gfx::RectF&());
64   MOCK_CONST_METHOD0(IsRTL, bool());
65
66  private:
67   base::string16 help_text_;
68   gfx::Range link_range_;
69
70   const gfx::Rect popup_bounds_;
71
72   DISALLOW_COPY_AND_ASSIGN(MockPasswordGenerationPopupController);
73 };
74
75 class PasswordGenerationPopupViewCocoaTest : public CocoaTest {
76  protected:
77   PasswordGenerationPopupViewCocoaTest()
78     : password_(base::ASCIIToUTF16("wow! such password"))
79   {}
80
81   virtual void SetUp() override {
82     mock_controller_.reset(new MockPasswordGenerationPopupController);
83     EXPECT_CALL(*mock_controller_, password())
84         .WillRepeatedly(Return(password_));
85
86     view_.reset([[PasswordGenerationPopupViewCocoa alloc]
87         initWithController:mock_controller_.get()
88                      frame:NSZeroRect]);
89
90     NSView* contentView = [test_window() contentView];
91     [contentView addSubview:view_];
92     EXPECT_CALL(*mock_controller_, container_view())
93         .WillRepeatedly(Return(contentView));
94   }
95
96   base::string16 password_;
97   scoped_ptr<MockPasswordGenerationPopupController> mock_controller_;
98   base::scoped_nsobject<PasswordGenerationPopupViewCocoa> view_;
99 };
100
101 TEST_VIEW(PasswordGenerationPopupViewCocoaTest, view_);
102
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_));
107
108   view_.reset([[PasswordGenerationPopupViewCocoa alloc]
109       initWithController:mock_controller_.get()
110                    frame:NSZeroRect]);
111
112   [view_ showPopup];
113   [view_ display];
114   [view_ hidePopup];
115 }
116
117 // Verifies that it doesn't crash when the controller is destroyed before the
118 // popup is hidden.
119 TEST_F(PasswordGenerationPopupViewCocoaTest, ControllerDestroyed) {
120   [view_ showPopup];
121   mock_controller_.reset();
122   [view_ controllerDestroyed];
123   [view_ display];
124   [view_ hidePopup];
125 }
126
127 }  // namespace