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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
8 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" | 8 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" |
9 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 9 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
10 #import "chrome/browser/ui/cocoa/web_intent_bubble_controller.h" | 10 #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" |
11 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" | 11 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" |
12 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" | 12 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" |
| 13 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 14 #include "content/browser/tab_contents/test_tab_contents.h" |
| 15 #include "content/test/test_browser_thread.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 class MockIntentPickerDelegate : public WebIntentPickerDelegate { | 20 class MockIntentPickerDelegate : public WebIntentPickerDelegate { |
18 public: | 21 public: |
19 virtual ~MockIntentPickerDelegate() {} | 22 virtual ~MockIntentPickerDelegate() {} |
20 | 23 |
21 MOCK_METHOD2(OnServiceChosen, void(size_t index, Disposition disposition)); | 24 MOCK_METHOD2(OnServiceChosen, void(size_t index, Disposition disposition)); |
22 MOCK_METHOD1(OnInlineDispositionWebContentsCreated, | 25 MOCK_METHOD1(OnInlineDispositionWebContentsCreated, |
23 void(content::WebContents* web_contents)); | 26 void(content::WebContents* web_contents)); |
24 MOCK_METHOD0(OnCancelled, void()); | 27 MOCK_METHOD0(OnCancelled, void()); |
25 MOCK_METHOD0(OnClosing, void()); | 28 MOCK_METHOD0(OnClosing, void()); |
26 }; | 29 }; |
27 | 30 |
28 } // namespace | 31 } // namespace |
29 | 32 |
30 class WebIntentBubbleControllerTest : public CocoaTest { | 33 class WebIntentPickerSheetControllerTest |
| 34 : public TabContentsWrapperTestHarness { |
31 public: | 35 public: |
32 virtual ~WebIntentBubbleControllerTest() { | 36 WebIntentPickerSheetControllerTest() |
| 37 : ui_thread_(content::BrowserThread::UI, MessageLoopForUI::current()) {} |
| 38 |
| 39 virtual ~WebIntentPickerSheetControllerTest() { |
33 message_loop_.RunAllPending(); | 40 message_loop_.RunAllPending(); |
34 } | 41 } |
| 42 |
35 virtual void TearDown() { | 43 virtual void TearDown() { |
36 // Do not animate out because that is hard to test around. | |
37 if (window_) | |
38 [window_ setDelayOnClose:NO]; | |
39 | |
40 if (picker_.get()) { | 44 if (picker_.get()) { |
41 EXPECT_CALL(delegate_, OnCancelled()); | 45 EXPECT_CALL(delegate_, OnCancelled()); |
42 EXPECT_CALL(delegate_, OnClosing()); | 46 EXPECT_CALL(delegate_, OnClosing()); |
43 | 47 |
44 [controller_ close]; | 48 [controller_ closeSheet]; |
45 // Closing |controller_| destroys |picker_|. | 49 // Closing |controller_| destroys |picker_|. |
46 ignore_result(picker_.release()); | 50 ignore_result(picker_.release()); |
47 } | 51 } |
48 CocoaTest::TearDown(); | 52 TabContentsWrapperTestHarness::TearDown(); |
49 } | 53 } |
50 | 54 |
51 void CreatePicker() { | 55 void CreatePicker() { |
52 picker_.reset(new WebIntentPickerCocoa()); | 56 picker_.reset(new WebIntentPickerCocoa()); |
53 picker_->delegate_ = &delegate_; | 57 picker_->delegate_ = &delegate_; |
54 picker_->model_ = &model_; | 58 picker_->model_ = &model_; |
55 window_ = nil; | 59 window_ = nil; |
56 controller_ = nil; | 60 controller_ = nil; |
57 } | 61 } |
58 | 62 |
59 void CreateBubble() { | 63 void CreateBubble() { |
60 CreatePicker(); | 64 picker_.reset(new WebIntentPickerCocoa(NULL, contents_wrapper(), |
61 NSPoint anchor=NSMakePoint(0, 0); | 65 &delegate_, &model_)); |
62 | 66 |
63 controller_ = | 67 controller_ = |
64 [[WebIntentBubbleController alloc] initWithPicker:picker_.get() | 68 [[WebIntentPickerSheetController alloc] initWithPicker:picker_.get()]; |
65 parentWindow:test_window() | 69 window_ = [controller_ window]; |
66 anchoredAt:anchor]; | |
67 window_ = static_cast<InfoBubbleWindow*>([controller_ window]); | |
68 [controller_ showWindow:nil]; | 70 [controller_ showWindow:nil]; |
69 } | 71 } |
70 | 72 |
71 // Checks the controller's window for the requisite subviews and icons. | 73 // Checks the controller's window for the requisite subviews and icons. |
72 void CheckWindow(size_t icon_count) { | 74 void CheckWindow(size_t icon_count) { |
73 NSArray* flip_views = [[window_ contentView] subviews]; | 75 NSArray* flip_views = [[window_ contentView] subviews]; |
74 | 76 |
75 // Expect 1 subview - the flip view. | 77 // Expect 1 subview - the flip view. |
76 ASSERT_EQ(1U, [flip_views count]); | 78 ASSERT_EQ(1U, [flip_views count]); |
77 | 79 |
78 NSArray* views = [[flip_views objectAtIndex:0] subviews]; | 80 NSArray* views = [[flip_views objectAtIndex:0] subviews]; |
79 | 81 |
80 // 3 + |icon_count| subviews - Icon, Header text, |icon_count| buttons, | 82 // 3 + |icon_count| subviews - Icon, Header text, |icon_count| buttons, |
81 // and a CWS link. | 83 // and a CWS link. |
82 ASSERT_EQ(3U + icon_count, [views count]); | 84 ASSERT_EQ(3U + icon_count, [views count]); |
83 | 85 |
84 ASSERT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSTextField class]]); | 86 ASSERT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSTextField class]]); |
85 ASSERT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSImageView class]]); | 87 ASSERT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSImageView class]]); |
86 for(NSUInteger i = 0; i < icon_count; ++i) { | 88 for(NSUInteger i = 0; i < icon_count; ++i) { |
87 ASSERT_TRUE([[views objectAtIndex:2 + i] isKindOfClass:[NSButton class]]); | 89 ASSERT_TRUE([[views objectAtIndex:2 + i] isKindOfClass: |
| 90 [NSButton class]]); |
88 } | 91 } |
89 | 92 |
90 // Verify the Chrome Web Store button. | 93 // Verify the Chrome Web Store button. |
91 NSButton* button = static_cast<NSButton*>([views lastObject]); | 94 NSButton* button = static_cast<NSButton*>([views lastObject]); |
92 ASSERT_TRUE([button isKindOfClass:[NSButton class]]); | 95 ASSERT_TRUE([button isKindOfClass:[NSButton class]]); |
93 EXPECT_TRUE([[button cell] isKindOfClass:[HyperlinkButtonCell class]]); | 96 EXPECT_TRUE([[button cell] isKindOfClass:[HyperlinkButtonCell class]]); |
94 CheckButton(button, @selector(showChromeWebStore:)); | 97 CheckButton(button, @selector(showChromeWebStore:)); |
95 | 98 |
96 // Verify buttons pointing to services. | 99 // Verify buttons pointing to services. |
97 for(NSUInteger i = 0; i < icon_count; ++i) { | 100 for(NSUInteger i = 0; i < icon_count; ++i) { |
98 NSButton* button = [views objectAtIndex:2 + i]; | 101 NSButton* button = [views objectAtIndex:2 + i]; |
99 CheckServiceButton(button, i); | 102 CheckServiceButton(button, i); |
100 } | 103 } |
101 | |
102 EXPECT_EQ([window_ delegate], controller_); | |
103 } | 104 } |
104 | 105 |
105 // Checks that a service button is hooked up correctly. | 106 // Checks that a service button is hooked up correctly. |
106 void CheckServiceButton(NSButton* button, NSUInteger service_index) { | 107 void CheckServiceButton(NSButton* button, NSUInteger service_index) { |
107 CheckButton(button, @selector(invokeService:)); | 108 CheckButton(button, @selector(invokeService:)); |
108 EXPECT_EQ(NSInteger(service_index), [button tag]); | 109 EXPECT_EQ(NSInteger(service_index), [button tag]); |
109 } | 110 } |
110 // Checks that a button is hooked up correctly. | 111 // Checks that a button is hooked up correctly. |
111 void CheckButton(id button, SEL action) { | 112 void CheckButton(id button, SEL action) { |
112 EXPECT_TRUE([button isKindOfClass:[NSButton class]] || | 113 EXPECT_TRUE([button isKindOfClass:[NSButton class]] || |
113 [button isKindOfClass:[NSButtonCell class]]); | 114 [button isKindOfClass:[NSButtonCell class]]); |
114 EXPECT_EQ(action, [button action]); | 115 EXPECT_EQ(action, [button action]); |
115 EXPECT_EQ(controller_, [button target]); | 116 EXPECT_EQ(controller_, [button target]); |
116 EXPECT_TRUE([button stringValue]); | 117 EXPECT_TRUE([button stringValue]); |
117 } | 118 } |
118 | 119 |
119 WebIntentBubbleController* controller_; // Weak, owns self. | 120 content::TestBrowserThread ui_thread_; |
120 InfoBubbleWindow* window_; // Weak, owned by controller. | 121 WebIntentPickerSheetController* controller_; // Weak, owns self. |
| 122 NSWindow* window_; // Weak, owned by controller. |
121 scoped_ptr<WebIntentPickerCocoa> picker_; | 123 scoped_ptr<WebIntentPickerCocoa> picker_; |
122 MockIntentPickerDelegate delegate_; | 124 MockIntentPickerDelegate delegate_; |
123 MessageLoopForUI message_loop_; | |
124 WebIntentPickerModel model_; // The model used by the picker | 125 WebIntentPickerModel model_; // The model used by the picker |
125 }; | 126 }; |
126 | 127 |
127 TEST_F(WebIntentBubbleControllerTest, EmptyBubble) { | 128 TEST_F(WebIntentPickerSheetControllerTest, EmptyBubble) { |
128 CreateBubble(); | 129 CreateBubble(); |
129 | 130 |
130 CheckWindow(/*icon_count=*/0); | 131 CheckWindow(/*icon_count=*/0); |
131 } | 132 } |
132 | 133 |
133 TEST_F(WebIntentBubbleControllerTest, PopulatedBubble) { | 134 TEST_F(WebIntentPickerSheetControllerTest, PopulatedBubble) { |
134 CreateBubble(); | 135 CreateBubble(); |
135 | 136 |
136 WebIntentPickerModel model; | 137 WebIntentPickerModel model; |
137 model.AddInstalledService(string16(), GURL(), | 138 model.AddInstalledService(string16(), GURL(), |
138 WebIntentPickerModel::DISPOSITION_WINDOW); | 139 WebIntentPickerModel::DISPOSITION_WINDOW); |
139 model.AddInstalledService(string16(), GURL(), | 140 model.AddInstalledService(string16(), GURL(), |
140 WebIntentPickerModel::DISPOSITION_WINDOW); | 141 WebIntentPickerModel::DISPOSITION_WINDOW); |
141 | 142 |
142 [controller_ performLayoutWithModel:&model]; | 143 [controller_ performLayoutWithModel:&model]; |
143 | 144 |
144 CheckWindow(/*icon_count=*/2); | 145 CheckWindow(/*icon_count=*/2); |
145 } | 146 } |
146 | 147 |
147 TEST_F(WebIntentBubbleControllerTest, OnCancelledWillSignalClose) { | 148 TEST_F(WebIntentPickerSheetControllerTest, OnCancelledWillSignalClose) { |
148 CreatePicker(); | 149 CreatePicker(); |
149 | 150 |
150 EXPECT_CALL(delegate_, OnCancelled()); | 151 EXPECT_CALL(delegate_, OnCancelled()); |
151 EXPECT_CALL(delegate_, OnClosing()); | 152 EXPECT_CALL(delegate_, OnClosing()); |
152 picker_->OnCancelled(); | 153 picker_->OnCancelled(); |
153 | 154 |
154 ignore_result(picker_.release()); // Closing |picker_| will self-destruct it. | 155 ignore_result(picker_.release()); // Closing |picker_| will destruct it. |
155 } | 156 } |
156 | 157 |
157 TEST_F(WebIntentBubbleControllerTest, CloseWillClose) { | 158 TEST_F(WebIntentPickerSheetControllerTest, CloseWillClose) { |
158 CreateBubble(); | 159 CreateBubble(); |
159 | 160 |
160 EXPECT_CALL(delegate_, OnCancelled()); | 161 EXPECT_CALL(delegate_, OnCancelled()); |
161 EXPECT_CALL(delegate_, OnClosing()); | 162 EXPECT_CALL(delegate_, OnClosing()); |
162 picker_->Close(); | 163 picker_->Close(); |
163 | 164 |
164 ignore_result(picker_.release()); // Closing |picker_| will self-destruct it. | 165 ignore_result(picker_.release()); // Closing |picker_| will destruct it. |
165 } | 166 } |
166 | 167 |
167 TEST_F(WebIntentBubbleControllerTest, DontCancelAfterServiceInvokation) { | 168 TEST_F(WebIntentPickerSheetControllerTest, DontCancelAfterServiceInvokation) { |
168 CreateBubble(); | 169 CreateBubble(); |
169 model_.AddInstalledService(string16(), GURL(), | 170 model_.AddInstalledService(string16(), GURL(), |
170 WebIntentPickerModel::DISPOSITION_WINDOW); | 171 WebIntentPickerModel::DISPOSITION_WINDOW); |
171 | 172 |
172 EXPECT_CALL(delegate_, OnServiceChosen( | 173 EXPECT_CALL(delegate_, OnServiceChosen( |
173 0, WebIntentPickerModel::DISPOSITION_WINDOW)); | 174 0, WebIntentPickerModel::DISPOSITION_WINDOW)); |
174 EXPECT_CALL(delegate_, OnCancelled()).Times(0); | 175 EXPECT_CALL(delegate_, OnCancelled()).Times(0); |
175 EXPECT_CALL(delegate_, OnClosing()); | 176 EXPECT_CALL(delegate_, OnClosing()); |
176 | 177 |
177 picker_->OnServiceChosen(0); | 178 picker_->OnServiceChosen(0); |
178 picker_->Close(); | 179 picker_->Close(); |
179 | 180 |
180 ignore_result(picker_.release()); // Closing |picker_| will self-destruct it. | 181 ignore_result(picker_.release()); // Closing |picker_| will destruct it. |
181 } | 182 } |
OLD | NEW |