922a21da889ab6cb59801b45cd22954bdc8790e4
[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       font_list_("Lucida Grande, 12px"),
27       popup_bounds_(gfx::Rect(0, 0, 200, 100)) {}
28
29   virtual void OnSavedPasswordsLinkClicked() OVERRIDE {}
30
31   virtual const gfx::FontList& font_list() const OVERRIDE {
32     return font_list_;
33   }
34
35   virtual const gfx::Rect& password_bounds() const OVERRIDE {
36     return password_bounds_;
37   }
38
39   virtual const gfx::Rect& divider_bounds() const OVERRIDE {
40     return divider_bounds_;
41   }
42
43   virtual const gfx::Rect& help_bounds() const OVERRIDE {
44     return help_bounds_;
45   }
46
47   virtual bool display_password() const OVERRIDE { return true; }
48
49   virtual bool password_selected() const OVERRIDE { return false; }
50
51   MOCK_CONST_METHOD0(password, base::string16());
52
53   virtual base::string16 SuggestedText() OVERRIDE {
54     return base::ASCIIToUTF16("Suggested by Chrome");
55   }
56
57   virtual const base::string16& HelpText() OVERRIDE { return help_text_; }
58
59   virtual const gfx::Range& HelpTextLinkRange() OVERRIDE { return link_range_; }
60
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 {
68     return popup_bounds_;
69   }
70   MOCK_METHOD0(container_view, gfx::NativeView());
71
72  private:
73   base::string16 help_text_;
74   const gfx::FontList font_list_;
75   gfx::Range link_range_;
76
77   gfx::Rect password_bounds_;
78   gfx::Rect divider_bounds_;
79   gfx::Rect help_bounds_;
80   const gfx::Rect popup_bounds_;
81
82   DISALLOW_COPY_AND_ASSIGN(MockPasswordGenerationPopupController);
83 };
84
85 class PasswordGenerationPopupViewCocoaTest : public CocoaTest {
86  protected:
87   PasswordGenerationPopupViewCocoaTest()
88     : password_(base::ASCIIToUTF16("wow! such password"))
89   {}
90
91   virtual void SetUp() OVERRIDE {
92     mock_controller_.reset(new MockPasswordGenerationPopupController);
93     EXPECT_CALL(*mock_controller_, password())
94         .WillRepeatedly(Return(password_));
95
96     view_.reset([[PasswordGenerationPopupViewCocoa alloc]
97         initWithController:mock_controller_.get()
98                      frame:NSZeroRect]);
99
100     NSView* contentView = [test_window() contentView];
101     [contentView addSubview:view_];
102     EXPECT_CALL(*mock_controller_, container_view())
103         .WillRepeatedly(Return(contentView));
104   }
105
106   base::string16 password_;
107   scoped_ptr<MockPasswordGenerationPopupController> mock_controller_;
108   base::scoped_nsobject<PasswordGenerationPopupViewCocoa> view_;
109 };
110
111 TEST_VIEW(PasswordGenerationPopupViewCocoaTest, view_);
112
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_));
117
118   view_.reset([[PasswordGenerationPopupViewCocoa alloc]
119       initWithController:mock_controller_.get()
120                    frame:NSZeroRect]);
121
122   [view_ showPopup];
123   [view_ display];
124   [view_ hidePopup];
125 }
126
127 // Verifies that it doesn't crash when the controller is destroyed before the
128 // popup is hidden.
129 TEST_F(PasswordGenerationPopupViewCocoaTest, ControllerDestroyed) {
130   [view_ showPopup];
131   mock_controller_.reset();
132   [view_ controllerDestroyed];
133   [view_ display];
134   [view_ hidePopup];
135 }
136
137 }  // namespace