OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/memory/scoped_nsobject.h" | 6 #include "base/memory/scoped_nsobject.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
11 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h" | 11 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h" |
12 #import "chrome/browser/ui/cocoa/confirm_bubble_view.h" | 12 #import "chrome/browser/ui/cocoa/confirm_bubble_view.h" |
13 #include "chrome/browser/ui/confirm_bubble_model.h" | 13 #include "chrome/browser/ui/confirm_bubble_model.h" |
14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
15 #import "testing/gtest_mac.h" | 15 #import "testing/gtest_mac.h" |
16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
17 #import "ui/gfx/point.h" | 17 #import "ui/gfx/point.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // The model object used in this test. This model implements all methods and | 21 // The model object used in this test. This model implements all methods and |
22 // updates its status when ConfirmBubbleController calls its methods. | 22 // updates its status when ConfirmBubbleController calls its methods. |
23 class TestConfirmBubbleModel : public ConfirmBubbleModel { | 23 class TestConfirmBubbleModel : public ConfirmBubbleModel { |
24 public: | 24 public: |
25 TestConfirmBubbleModel(bool* model_deleted, | |
26 bool* accept_clicked, | |
27 bool* cancel_clicked, | |
28 bool* link_clicked); | |
25 TestConfirmBubbleModel(); | 29 TestConfirmBubbleModel(); |
26 virtual ~TestConfirmBubbleModel() OVERRIDE; | 30 virtual ~TestConfirmBubbleModel() OVERRIDE; |
27 virtual string16 GetTitle() const OVERRIDE; | 31 virtual string16 GetTitle() const OVERRIDE; |
28 virtual string16 GetMessageText() const OVERRIDE; | 32 virtual string16 GetMessageText() const OVERRIDE; |
29 virtual gfx::Image* GetIcon() const OVERRIDE; | 33 virtual gfx::Image* GetIcon() const OVERRIDE; |
30 virtual int GetButtons() const OVERRIDE; | 34 virtual int GetButtons() const OVERRIDE; |
31 virtual string16 GetButtonLabel(BubbleButton button) const OVERRIDE; | 35 virtual string16 GetButtonLabel(BubbleButton button) const OVERRIDE; |
32 virtual void Accept() OVERRIDE; | 36 virtual void Accept() OVERRIDE; |
33 virtual void Cancel() OVERRIDE; | 37 virtual void Cancel() OVERRIDE; |
34 virtual string16 GetLinkText() const OVERRIDE; | 38 virtual string16 GetLinkText() const OVERRIDE; |
35 virtual void LinkClicked() OVERRIDE; | 39 virtual void LinkClicked() OVERRIDE; |
36 | 40 |
37 bool accept_clicked() const { return accept_clicked_; } | |
38 bool cancel_clicked() const { return cancel_clicked_; } | |
39 bool link_clicked() const { return link_clicked_; } | |
40 | |
41 private: | 41 private: |
42 bool accept_clicked_; | 42 bool* model_deleted_; |
43 bool cancel_clicked_; | 43 bool* accept_clicked_; |
44 bool link_clicked_; | 44 bool* cancel_clicked_; |
45 bool* link_clicked_; | |
45 }; | 46 }; |
46 | 47 |
47 TestConfirmBubbleModel::TestConfirmBubbleModel() : | 48 TestConfirmBubbleModel::TestConfirmBubbleModel(bool* model_deleted, |
48 accept_clicked_(false), | 49 bool* accept_clicked, |
49 cancel_clicked_(false), | 50 bool* cancel_clicked, |
50 link_clicked_(false) { | 51 bool* link_clicked) |
52 : model_deleted_(model_deleted), | |
53 accept_clicked_(accept_clicked), | |
54 cancel_clicked_(cancel_clicked), | |
55 link_clicked_(link_clicked) { | |
51 } | 56 } |
52 | 57 |
53 TestConfirmBubbleModel::~TestConfirmBubbleModel() { | 58 TestConfirmBubbleModel::~TestConfirmBubbleModel() { |
59 *model_deleted_ = true; | |
54 } | 60 } |
55 | 61 |
56 string16 TestConfirmBubbleModel::GetTitle() const { | 62 string16 TestConfirmBubbleModel::GetTitle() const { |
57 return ASCIIToUTF16("Test"); | 63 return ASCIIToUTF16("Test"); |
58 } | 64 } |
59 | 65 |
60 string16 TestConfirmBubbleModel::GetMessageText() const { | 66 string16 TestConfirmBubbleModel::GetMessageText() const { |
61 return ASCIIToUTF16("Test Message"); | 67 return ASCIIToUTF16("Test Message"); |
62 } | 68 } |
63 | 69 |
64 gfx::Image* TestConfirmBubbleModel::GetIcon() const { | 70 gfx::Image* TestConfirmBubbleModel::GetIcon() const { |
65 return &ResourceBundle::GetSharedInstance().GetImageNamed( | 71 return &ResourceBundle::GetSharedInstance().GetImageNamed( |
66 IDR_PRODUCT_LOGO_16); | 72 IDR_PRODUCT_LOGO_16); |
67 } | 73 } |
68 | 74 |
69 int TestConfirmBubbleModel::GetButtons() const { | 75 int TestConfirmBubbleModel::GetButtons() const { |
70 return BUTTON_OK | BUTTON_CANCEL; | 76 return BUTTON_OK | BUTTON_CANCEL; |
71 } | 77 } |
72 | 78 |
73 string16 TestConfirmBubbleModel::GetButtonLabel(BubbleButton button) const { | 79 string16 TestConfirmBubbleModel::GetButtonLabel(BubbleButton button) const { |
74 return button == BUTTON_OK ? ASCIIToUTF16("OK") : ASCIIToUTF16("Cancel"); | 80 return button == BUTTON_OK ? ASCIIToUTF16("OK") : ASCIIToUTF16("Cancel"); |
75 } | 81 } |
76 | 82 |
77 void TestConfirmBubbleModel::Accept() { | 83 void TestConfirmBubbleModel::Accept() { |
78 accept_clicked_ = true; | 84 *accept_clicked_ = true; |
79 } | 85 } |
80 | 86 |
81 void TestConfirmBubbleModel::Cancel() { | 87 void TestConfirmBubbleModel::Cancel() { |
82 cancel_clicked_ = true; | 88 *cancel_clicked_ = true; |
83 } | 89 } |
84 | 90 |
85 string16 TestConfirmBubbleModel::GetLinkText() const { | 91 string16 TestConfirmBubbleModel::GetLinkText() const { |
86 return ASCIIToUTF16("Link"); | 92 return ASCIIToUTF16("Link"); |
87 } | 93 } |
88 | 94 |
89 void TestConfirmBubbleModel::LinkClicked() { | 95 void TestConfirmBubbleModel::LinkClicked() { |
90 link_clicked_ = true; | 96 *link_clicked_ = true; |
91 } | 97 } |
92 | 98 |
93 } // namespace | 99 } // namespace |
94 | 100 |
95 class ConfirmBubbleControllerTest : public CocoaTest { | 101 class ConfirmBubbleControllerTest : public CocoaTest { |
96 public: | 102 public: |
97 ConfirmBubbleControllerTest() { | 103 ConfirmBubbleControllerTest() { |
98 NSView* view = [test_window() contentView]; | 104 NSView* view = [test_window() contentView]; |
99 model_.reset(new TestConfirmBubbleModel); | 105 model_ = new TestConfirmBubbleModel(&model_deleted_, |
Robert Sesek
2012/02/17 15:24:25
Comment: // Owned by the controller.
Hironori Bono
2012/02/22 07:22:50
Done.
| |
106 &accept_clicked_, | |
107 &cancel_clicked_, | |
108 &link_clicked_); | |
100 gfx::Point origin(0, 0); | 109 gfx::Point origin(0, 0); |
101 controller_ = | 110 controller_ = |
102 [[ConfirmBubbleController alloc] initWithParent:view | 111 [[ConfirmBubbleController alloc] initWithParent:view |
103 origin:origin.ToCGPoint() | 112 origin:origin.ToCGPoint() |
104 model:model_.get()]; | 113 model:model_]; |
105 [view addSubview:[controller_ view] | 114 [view addSubview:[controller_ view] |
106 positioned:NSWindowAbove | 115 positioned:NSWindowAbove |
107 relativeTo:nil]; | 116 relativeTo:nil]; |
108 } | 117 } |
109 | 118 |
110 ConfirmBubbleView* GetBubbleView() const { | 119 ConfirmBubbleView* GetBubbleView() const { |
111 return (ConfirmBubbleView*)[controller_ view]; | 120 return (ConfirmBubbleView*)[controller_ view]; |
112 } | 121 } |
113 | 122 |
114 TestConfirmBubbleModel* model() const { | 123 TestConfirmBubbleModel* model() const { return model_; } |
115 return model_.get(); | 124 bool model_deleted() const { return model_deleted_; } |
116 } | 125 bool accept_clicked() const { return accept_clicked_; } |
126 bool cancel_clicked() const { return cancel_clicked_; } | |
127 bool link_clicked() const { return link_clicked_; } | |
117 | 128 |
118 private: | 129 private: |
119 ConfirmBubbleController* controller_; // weak; owns self | 130 ConfirmBubbleController* controller_; // weak; owns self |
120 scoped_ptr<TestConfirmBubbleModel> model_; | 131 TestConfirmBubbleModel* model_; // weak |
132 bool model_deleted_; | |
133 bool accept_clicked_; | |
134 bool cancel_clicked_; | |
135 bool link_clicked_; | |
121 }; | 136 }; |
122 | 137 |
123 // Verify clicking a button or a link removes the ConfirmBubbleView object and | 138 // Verify clicking a button or a link removes the ConfirmBubbleView object and |
124 // calls an appropriate model method. | 139 // calls an appropriate model method. |
125 TEST_F(ConfirmBubbleControllerTest, ClickOk) { | 140 TEST_F(ConfirmBubbleControllerTest, ClickOk) { |
126 NSView* view = [test_window() contentView]; | 141 NSView* view = [test_window() contentView]; |
127 ConfirmBubbleView* bubble_view = GetBubbleView(); | 142 ConfirmBubbleView* bubble_view = GetBubbleView(); |
128 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 143 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
129 EXPECT_TRUE(contains_bubble_view); | 144 EXPECT_TRUE(contains_bubble_view); |
130 | 145 |
131 // Click its OK button and verify this view has been removed from the test | 146 // Click its OK button and verify this view has been removed from the test |
132 // window. Also verify TestConfirmBubbleModel::Accept() has been called. | 147 // window. Also verify TestConfirmBubbleModel::Accept() has been called. |
133 [bubble_view clickOk]; | 148 [bubble_view clickOk]; |
134 | 149 |
135 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 150 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
136 EXPECT_FALSE(contains_bubble_view); | 151 EXPECT_FALSE(contains_bubble_view); |
137 EXPECT_TRUE(model()->accept_clicked()); | 152 EXPECT_TRUE(model_deleted()); |
138 EXPECT_FALSE(model()->cancel_clicked()); | 153 EXPECT_TRUE(accept_clicked()); |
139 EXPECT_FALSE(model()->link_clicked()); | 154 EXPECT_FALSE(cancel_clicked()); |
155 EXPECT_FALSE(link_clicked()); | |
140 } | 156 } |
141 | 157 |
142 TEST_F(ConfirmBubbleControllerTest, ClickCancel) { | 158 TEST_F(ConfirmBubbleControllerTest, ClickCancel) { |
143 NSView* view = [test_window() contentView]; | 159 NSView* view = [test_window() contentView]; |
144 ConfirmBubbleView* bubble_view = GetBubbleView(); | 160 ConfirmBubbleView* bubble_view = GetBubbleView(); |
145 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 161 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
146 EXPECT_TRUE(contains_bubble_view); | 162 EXPECT_TRUE(contains_bubble_view); |
147 | 163 |
148 // Click its cancel button and verify this view has been removed from the test | 164 // Click its cancel button and verify this view has been removed from the test |
149 // window. Also verify TestConfirmBubbleModel::Cancel() has been called. | 165 // window. Also verify TestConfirmBubbleModel::Cancel() has been called. |
150 [bubble_view clickCancel]; | 166 [bubble_view clickCancel]; |
151 | 167 |
152 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 168 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
153 EXPECT_FALSE(contains_bubble_view); | 169 EXPECT_FALSE(contains_bubble_view); |
154 EXPECT_FALSE(model()->accept_clicked()); | 170 EXPECT_TRUE(model_deleted()); |
155 EXPECT_TRUE(model()->cancel_clicked()); | 171 EXPECT_FALSE(accept_clicked()); |
156 EXPECT_FALSE(model()->link_clicked()); | 172 EXPECT_TRUE(cancel_clicked()); |
173 EXPECT_FALSE(link_clicked()); | |
157 } | 174 } |
158 | 175 |
159 TEST_F(ConfirmBubbleControllerTest, ClickLink) { | 176 TEST_F(ConfirmBubbleControllerTest, ClickLink) { |
160 NSView* view = [test_window() contentView]; | 177 NSView* view = [test_window() contentView]; |
161 ConfirmBubbleView* bubble_view = GetBubbleView(); | 178 ConfirmBubbleView* bubble_view = GetBubbleView(); |
162 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 179 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
163 EXPECT_TRUE(contains_bubble_view); | 180 EXPECT_TRUE(contains_bubble_view); |
164 | 181 |
165 // Click its link and verify this view has been removed from the test window. | 182 // Click its link and verify this view has been removed from the test window. |
166 // Also verify TestConfirmBubbleModel::LinkClicked() has been called. | 183 // Also verify TestConfirmBubbleModel::LinkClicked() has been called. |
167 [bubble_view clickLink]; | 184 [bubble_view clickLink]; |
168 | 185 |
169 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 186 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
170 EXPECT_FALSE(contains_bubble_view); | 187 EXPECT_FALSE(contains_bubble_view); |
171 EXPECT_FALSE(model()->accept_clicked()); | 188 EXPECT_TRUE(model_deleted()); |
172 EXPECT_FALSE(model()->cancel_clicked()); | 189 EXPECT_FALSE(accept_clicked()); |
173 EXPECT_TRUE(model()->link_clicked()); | 190 EXPECT_FALSE(cancel_clicked()); |
191 EXPECT_TRUE(link_clicked()); | |
174 } | 192 } |
OLD | NEW |