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 #include "grit/theme_resources.h"
13 #import "testing/gtest_mac.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #import "ui/gfx/geometry/point.h"
19 // The model object used in this test. This model implements all methods and
20 // updates its status when ConfirmBubbleController calls its methods.
21 class TestConfirmBubbleModel : public ConfirmBubbleModel {
23 TestConfirmBubbleModel(bool* model_deleted,
27 TestConfirmBubbleModel();
28 ~TestConfirmBubbleModel() override;
29 base::string16 GetTitle() const override;
30 base::string16 GetMessageText() const override;
31 gfx::Image* GetIcon() const override;
32 int GetButtons() const override;
33 base::string16 GetButtonLabel(BubbleButton button) const override;
34 void Accept() override;
35 void Cancel() override;
36 base::string16 GetLinkText() const override;
37 void LinkClicked() override;
41 bool* accept_clicked_;
42 bool* cancel_clicked_;
46 TestConfirmBubbleModel::TestConfirmBubbleModel(bool* model_deleted,
50 : model_deleted_(model_deleted),
51 accept_clicked_(accept_clicked),
52 cancel_clicked_(cancel_clicked),
53 link_clicked_(link_clicked) {
56 TestConfirmBubbleModel::~TestConfirmBubbleModel() {
57 *model_deleted_ = true;
60 base::string16 TestConfirmBubbleModel::GetTitle() const {
61 return base::ASCIIToUTF16("Test");
64 base::string16 TestConfirmBubbleModel::GetMessageText() const {
65 return base::ASCIIToUTF16("Test Message");
68 gfx::Image* TestConfirmBubbleModel::GetIcon() const {
69 return &ResourceBundle::GetSharedInstance().GetImageNamed(
73 int TestConfirmBubbleModel::GetButtons() const {
74 return BUTTON_OK | BUTTON_CANCEL;
77 base::string16 TestConfirmBubbleModel::GetButtonLabel(
78 BubbleButton button) const {
79 return button == BUTTON_OK ? base::ASCIIToUTF16("OK")
80 : base::ASCIIToUTF16("Cancel");
83 void TestConfirmBubbleModel::Accept() {
84 *accept_clicked_ = true;
87 void TestConfirmBubbleModel::Cancel() {
88 *cancel_clicked_ = true;
91 base::string16 TestConfirmBubbleModel::GetLinkText() const {
92 return base::ASCIIToUTF16("Link");
95 void TestConfirmBubbleModel::LinkClicked() {
96 *link_clicked_ = true;
101 class ConfirmBubbleControllerTest : public CocoaTest {
103 ConfirmBubbleControllerTest()
104 : model_deleted_(false),
105 accept_clicked_(false),
106 cancel_clicked_(false),
107 link_clicked_(false) {
108 NSView* view = [test_window() contentView];
109 // This model is owned by the controller created below.
110 model_.reset(new TestConfirmBubbleModel(&model_deleted_,
114 gfx::Point origin(0, 0);
116 [[ConfirmBubbleController alloc] initWithParent:view
117 origin:origin.ToCGPoint()
118 model:model_.Pass()];
119 [view addSubview:[controller_ view]
120 positioned:NSWindowAbove
124 ConfirmBubbleCocoa* GetBubble() const {
125 return (ConfirmBubbleCocoa*)[controller_ view];
128 bool model_deleted() const { return model_deleted_; }
129 bool accept_clicked() const { return accept_clicked_; }
130 bool cancel_clicked() const { return cancel_clicked_; }
131 bool link_clicked() const { return link_clicked_; }
134 ConfirmBubbleController* controller_; // weak; owns self
135 scoped_ptr<TestConfirmBubbleModel> model_;
137 bool accept_clicked_;
138 bool cancel_clicked_;
142 // Verify clicking a button or a link removes the ConfirmBubbleCocoa object and
143 // calls an appropriate model method.
144 TEST_F(ConfirmBubbleControllerTest, ClickOk) {
145 NSView* view = [test_window() contentView];
146 ConfirmBubbleCocoa* bubble = GetBubble();
147 bool contains_bubble_view = [[view subviews] containsObject:bubble];
148 EXPECT_TRUE(contains_bubble_view);
150 // Click its OK button and verify this view has been removed from the test
151 // window. Also verify TestConfirmBubbleModel::Accept() has been called.
154 contains_bubble_view = [[view subviews] containsObject:bubble];
155 EXPECT_FALSE(contains_bubble_view);
156 EXPECT_TRUE(accept_clicked());
157 EXPECT_FALSE(cancel_clicked());
158 EXPECT_FALSE(link_clicked());
161 TEST_F(ConfirmBubbleControllerTest, ClickCancel) {
162 NSView* view = [test_window() contentView];
163 ConfirmBubbleCocoa* bubble = GetBubble();
164 bool contains_bubble_view = [[view subviews] containsObject:bubble];
165 EXPECT_TRUE(contains_bubble_view);
167 // Click its cancel button and verify this view has been removed from the test
168 // window. Also verify TestConfirmBubbleModel::Cancel() has been called.
169 [bubble clickCancel];
171 contains_bubble_view = [[view subviews] containsObject:bubble];
172 EXPECT_FALSE(contains_bubble_view);
173 EXPECT_FALSE(accept_clicked());
174 EXPECT_TRUE(cancel_clicked());
175 EXPECT_FALSE(link_clicked());
178 TEST_F(ConfirmBubbleControllerTest, ClickLink) {
179 NSView* view = [test_window() contentView];
180 ConfirmBubbleCocoa* bubble = GetBubble();
181 bool contains_bubble_view = [[view subviews] containsObject:bubble];
182 EXPECT_TRUE(contains_bubble_view);
184 // Click its link and verify this view has been removed from the test window.
185 // Also verify TestConfirmBubbleModel::LinkClicked() has been called.
188 contains_bubble_view = [[view subviews] containsObject:bubble];
189 EXPECT_FALSE(contains_bubble_view);
190 EXPECT_FALSE(accept_clicked());
191 EXPECT_FALSE(cancel_clicked());
192 EXPECT_TRUE(link_clicked());