1 // Copyright (c) 2012 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 #include "base/compiler_specific.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/confirm_bubble_cocoa.h"
10 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h"
11 #include "chrome/browser/ui/confirm_bubble_model.h"
12 #import "testing/gtest_mac.h"
13 #import "ui/gfx/geometry/point.h"
17 // The model object used in this test. This model implements all methods and
18 // updates its status when ConfirmBubbleController calls its methods.
19 class TestConfirmBubbleModel : public ConfirmBubbleModel {
21 TestConfirmBubbleModel(bool* model_deleted,
25 TestConfirmBubbleModel();
26 ~TestConfirmBubbleModel() override;
27 base::string16 GetTitle() const override;
28 base::string16 GetMessageText() const override;
29 int GetButtons() const override;
30 base::string16 GetButtonLabel(BubbleButton button) const override;
31 void Accept() override;
32 void Cancel() override;
33 base::string16 GetLinkText() const override;
34 void LinkClicked() override;
38 bool* accept_clicked_;
39 bool* cancel_clicked_;
43 TestConfirmBubbleModel::TestConfirmBubbleModel(bool* model_deleted,
47 : model_deleted_(model_deleted),
48 accept_clicked_(accept_clicked),
49 cancel_clicked_(cancel_clicked),
50 link_clicked_(link_clicked) {
53 TestConfirmBubbleModel::~TestConfirmBubbleModel() {
54 *model_deleted_ = true;
57 base::string16 TestConfirmBubbleModel::GetTitle() const {
58 return base::ASCIIToUTF16("Test");
61 base::string16 TestConfirmBubbleModel::GetMessageText() const {
62 return base::ASCIIToUTF16("Test Message");
65 int TestConfirmBubbleModel::GetButtons() const {
66 return BUTTON_OK | BUTTON_CANCEL;
69 base::string16 TestConfirmBubbleModel::GetButtonLabel(
70 BubbleButton button) const {
71 return button == BUTTON_OK ? base::ASCIIToUTF16("OK")
72 : base::ASCIIToUTF16("Cancel");
75 void TestConfirmBubbleModel::Accept() {
76 *accept_clicked_ = true;
79 void TestConfirmBubbleModel::Cancel() {
80 *cancel_clicked_ = true;
83 base::string16 TestConfirmBubbleModel::GetLinkText() const {
84 return base::ASCIIToUTF16("Link");
87 void TestConfirmBubbleModel::LinkClicked() {
88 *link_clicked_ = true;
93 class ConfirmBubbleControllerTest : public CocoaTest {
95 ConfirmBubbleControllerTest()
96 : model_deleted_(false),
97 accept_clicked_(false),
98 cancel_clicked_(false),
99 link_clicked_(false) {
100 NSView* view = [test_window() contentView];
101 // This model is owned by the controller created below.
102 model_.reset(new TestConfirmBubbleModel(&model_deleted_,
106 gfx::Point origin(0, 0);
108 [[ConfirmBubbleController alloc] initWithParent:view
109 origin:origin.ToCGPoint()
110 model:model_.Pass()];
111 [view addSubview:[controller_ view]
112 positioned:NSWindowAbove
116 ConfirmBubbleCocoa* GetBubble() const {
117 return (ConfirmBubbleCocoa*)[controller_ view];
120 bool model_deleted() const { return model_deleted_; }
121 bool accept_clicked() const { return accept_clicked_; }
122 bool cancel_clicked() const { return cancel_clicked_; }
123 bool link_clicked() const { return link_clicked_; }
126 ConfirmBubbleController* controller_; // weak; owns self
127 scoped_ptr<TestConfirmBubbleModel> model_;
129 bool accept_clicked_;
130 bool cancel_clicked_;
134 // Verify clicking a button or a link removes the ConfirmBubbleCocoa object and
135 // calls an appropriate model method.
136 TEST_F(ConfirmBubbleControllerTest, ClickOk) {
137 NSView* view = [test_window() contentView];
138 ConfirmBubbleCocoa* bubble = GetBubble();
139 bool contains_bubble_view = [[view subviews] containsObject:bubble];
140 EXPECT_TRUE(contains_bubble_view);
142 // Click its OK button and verify this view has been removed from the test
143 // window. Also verify TestConfirmBubbleModel::Accept() has been called.
146 contains_bubble_view = [[view subviews] containsObject:bubble];
147 EXPECT_FALSE(contains_bubble_view);
148 EXPECT_TRUE(accept_clicked());
149 EXPECT_FALSE(cancel_clicked());
150 EXPECT_FALSE(link_clicked());
153 TEST_F(ConfirmBubbleControllerTest, ClickCancel) {
154 NSView* view = [test_window() contentView];
155 ConfirmBubbleCocoa* bubble = GetBubble();
156 bool contains_bubble_view = [[view subviews] containsObject:bubble];
157 EXPECT_TRUE(contains_bubble_view);
159 // Click its cancel button and verify this view has been removed from the test
160 // window. Also verify TestConfirmBubbleModel::Cancel() has been called.
161 [bubble clickCancel];
163 contains_bubble_view = [[view subviews] containsObject:bubble];
164 EXPECT_FALSE(contains_bubble_view);
165 EXPECT_FALSE(accept_clicked());
166 EXPECT_TRUE(cancel_clicked());
167 EXPECT_FALSE(link_clicked());
170 TEST_F(ConfirmBubbleControllerTest, ClickLink) {
171 NSView* view = [test_window() contentView];
172 ConfirmBubbleCocoa* bubble = GetBubble();
173 bool contains_bubble_view = [[view subviews] containsObject:bubble];
174 EXPECT_TRUE(contains_bubble_view);
176 // Click its link and verify this view has been removed from the test window.
177 // Also verify TestConfirmBubbleModel::LinkClicked() has been called.
180 contains_bubble_view = [[view subviews] containsObject:bubble];
181 EXPECT_FALSE(contains_bubble_view);
182 EXPECT_FALSE(accept_clicked());
183 EXPECT_FALSE(cancel_clicked());
184 EXPECT_TRUE(link_clicked());