Chromium Code Reviews| 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/memory/scoped_nsobject.h" |
| 6 #include "base/message_loop.h" | |
| 7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 8 #import "chrome/browser/ui/cocoa/hover_close_button.h" | 6 #import "chrome/browser/ui/cocoa/hover_close_button.h" |
| 9 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" | 7 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" |
| 10 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | |
| 11 #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" | 8 #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" |
|
Robert Sesek
2012/04/12 14:03:19
In test .cc/mm files, the .h for the implementatio
groby-ooo-7-16
2012/04/16 23:03:14
Done.
| |
| 12 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" | 9 #include "chrome/browser/ui/intents/web_intent_picker_model.h" |
| 13 #include "chrome/browser/ui/cocoa/web_intent_picker_test_base.h" | |
| 14 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" | |
| 15 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | |
| 16 #include "content/test/test_browser_thread.h" | 10 #include "content/test/test_browser_thread.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/platform_test.h" | |
| 18 | 13 |
| 19 class WebIntentPickerSheetControllerTest | 14 class WebIntentPickerSheetControllerTest : public PlatformTest { |
|
Robert Sesek
2012/04/12 14:03:19
Use CocoaTest (cocoa_test_helpers.h)
groby-ooo-7-16
2012/04/16 23:03:14
Done.
| |
| 20 : public TabContentsWrapperTestHarness, | |
| 21 public WebIntentPickerTestBase { | |
| 22 public: | 15 public: |
| 23 WebIntentPickerSheetControllerTest() | |
| 24 : ui_thread_(content::BrowserThread::UI, MessageLoopForUI::current()) {} | |
| 25 | 16 |
| 26 virtual ~WebIntentPickerSheetControllerTest() { | 17 virtual void SetUp() { |
| 27 message_loop_.RunAllPending(); | 18 controller_.reset( |
| 28 } | 19 [[WebIntentPickerSheetController alloc] initWithPicker:nil]); |
| 29 | 20 window_ = [controller_ window]; |
| 30 virtual void TearDown() { | |
| 31 if (picker_.get()) { | |
| 32 EXPECT_CALL(delegate_, OnCancelled()); | |
| 33 EXPECT_CALL(delegate_, OnClosing()); | |
| 34 | |
| 35 [controller_ cancelOperation:controller_]; | |
| 36 // Closing |controller_| destroys |picker_|. | |
| 37 ignore_result(picker_.release()); | |
| 38 } | |
| 39 TabContentsWrapperTestHarness::TearDown(); | |
| 40 } | |
| 41 | |
| 42 void CreatePicker() { | |
| 43 picker_.reset(new WebIntentPickerCocoa()); | |
| 44 picker_->delegate_ = &delegate_; | |
| 45 picker_->model_ = &model_; | |
| 46 window_ = nil; | |
| 47 controller_ = nil; | |
| 48 } | 21 } |
| 49 | 22 |
| 50 // Checks the controller's window for the requisite subviews and icons. | 23 // Checks the controller's window for the requisite subviews and icons. |
| 51 void CheckWindow(size_t icon_count) { | 24 void CheckWindow(size_t row_count) { |
| 52 NSArray* flip_views = [[window_ contentView] subviews]; | 25 NSArray* flip_views = [[window_ contentView] subviews]; |
| 53 | 26 |
| 54 // Check for proper firstResponder. | 27 // Check for proper firstResponder. |
| 55 ASSERT_EQ(controller_, [window_ firstResponder]); | 28 ASSERT_EQ(controller_.get(), [window_ firstResponder]); |
| 56 | 29 |
| 57 // Expect 1 subview - the flip view. | 30 // Expect 1 subview - the flip view. |
| 58 ASSERT_EQ(1U, [flip_views count]); | 31 ASSERT_EQ(1U, [flip_views count]); |
| 59 | 32 |
| 60 NSArray* views = [[flip_views objectAtIndex:0] subviews]; | 33 NSArray* views = [[flip_views objectAtIndex:0] subviews]; |
| 61 | 34 |
| 62 // 4 + |icon_count| subviews - icon, header text, close button, | 35 // 4 + |row_count| subviews - icon, header text, close button, |
| 63 // |icon_count| buttons, and a CWS link. | 36 // |row_count| buttons, and a CWS link. |
| 64 ASSERT_EQ(4U + icon_count, [views count]); | 37 ASSERT_EQ(4U + row_count, [views count]); |
| 65 | 38 |
| 66 ASSERT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSTextField class]]); | 39 ASSERT_TRUE([[views objectAtIndex:0] isKindOfClass:[NSTextField class]]); |
| 67 ASSERT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSImageView class]]); | 40 ASSERT_TRUE([[views objectAtIndex:1] isKindOfClass:[NSImageView class]]); |
| 68 ASSERT_TRUE([[views objectAtIndex:2] isKindOfClass: | 41 ASSERT_TRUE([[views objectAtIndex:2] isKindOfClass: |
| 69 [HoverCloseButton class]]); | 42 [HoverCloseButton class]]); |
| 70 for(NSUInteger i = 0; i < icon_count; ++i) { | 43 for(NSUInteger i = 0; i < row_count; ++i) { |
| 71 ASSERT_TRUE([[views objectAtIndex:3 + i] isKindOfClass: | 44 ASSERT_TRUE([[views objectAtIndex:3 + i] isKindOfClass: |
| 72 [NSButton class]]); | 45 [NSButton class]]); |
| 73 } | 46 } |
| 74 | 47 |
| 75 // Verify the close button | 48 // Verify the close button |
| 76 NSButton* close_button = static_cast<NSButton*>([views objectAtIndex:2]); | 49 NSButton* close_button = static_cast<NSButton*>([views objectAtIndex:2]); |
| 77 CheckButton(close_button, @selector(cancelOperation:)); | 50 CheckButton(close_button, @selector(cancelOperation:)); |
| 78 | 51 |
| 79 // Verify the Chrome Web Store button. | 52 // Verify the Chrome Web Store button. |
| 80 NSButton* button = static_cast<NSButton*>([views lastObject]); | 53 NSButton* button = static_cast<NSButton*>([views lastObject]); |
| 81 ASSERT_TRUE([button isKindOfClass:[NSButton class]]); | 54 ASSERT_TRUE([button isKindOfClass:[NSButton class]]); |
| 82 EXPECT_TRUE([[button cell] isKindOfClass:[HyperlinkButtonCell class]]); | 55 EXPECT_TRUE([[button cell] isKindOfClass:[HyperlinkButtonCell class]]); |
| 83 CheckButton(button, @selector(showChromeWebStore:)); | 56 CheckButton(button, @selector(showChromeWebStore:)); |
| 84 | 57 |
| 85 // Verify buttons pointing to services. | 58 // Verify buttons pointing to services. |
| 86 for(NSUInteger i = 0; i < icon_count; ++i) { | 59 for(NSUInteger i = 0; i < row_count; ++i) { |
| 87 NSButton* button = [views objectAtIndex:3 + i]; | 60 NSButton* button = [views objectAtIndex:3 + i]; |
| 88 CheckServiceButton(button, i); | 61 CheckServiceButton(button, i); |
| 89 } | 62 } |
| 90 } | 63 } |
| 91 | 64 |
| 92 // Checks that a service button is hooked up correctly. | 65 // Checks that a service button is hooked up correctly. |
| 93 void CheckServiceButton(NSButton* button, NSUInteger service_index) { | 66 void CheckServiceButton(NSButton* button, NSUInteger service_index) { |
| 94 CheckButton(button, @selector(invokeService:)); | 67 CheckButton(button, @selector(invokeService:)); |
| 95 EXPECT_EQ(NSInteger(service_index), [button tag]); | 68 EXPECT_EQ(NSInteger(service_index), [button tag]); |
| 96 } | 69 } |
| 97 | 70 |
| 98 // Checks that a button is hooked up correctly. | 71 // Checks that a button is hooked up correctly. |
| 99 void CheckButton(id button, SEL action) { | 72 void CheckButton(id button, SEL action) { |
| 100 EXPECT_TRUE([button isKindOfClass:[NSButton class]] || | 73 EXPECT_TRUE([button isKindOfClass:[NSButton class]] || |
| 101 [button isKindOfClass:[NSButtonCell class]]); | 74 [button isKindOfClass:[NSButtonCell class]]); |
| 102 EXPECT_EQ(action, [button action]); | 75 EXPECT_EQ(action, [button action]); |
| 103 EXPECT_EQ(controller_, [button target]); | 76 EXPECT_EQ(controller_.get(), [button target]); |
| 104 EXPECT_TRUE([button stringValue]); | 77 EXPECT_TRUE([button stringValue]); |
| 105 } | 78 } |
| 106 | 79 |
| 107 content::TestBrowserThread ui_thread_; | 80 // Controller under test. |
| 81 scoped_nsobject<WebIntentPickerSheetController> controller_; | |
| 82 | |
| 83 NSWindow* window_; // Weak, owned by |controller_|. | |
| 108 }; | 84 }; |
| 109 | 85 |
| 110 TEST_F(WebIntentPickerSheetControllerTest, EmptyBubble) { | 86 TEST_F(WebIntentPickerSheetControllerTest, NoRows) { |
| 111 CreateBubble(contents_wrapper()); | 87 CheckWindow(/*row_count=*/0); |
| 112 | |
| 113 CheckWindow(/*icon_count=*/0); | |
| 114 } | 88 } |
| 115 | 89 |
| 116 TEST_F(WebIntentPickerSheetControllerTest, PopulatedBubble) { | 90 TEST_F(WebIntentPickerSheetControllerTest, PopulatedRows) { |
| 117 CreateBubble(contents_wrapper()); | |
| 118 | |
| 119 WebIntentPickerModel model; | 91 WebIntentPickerModel model; |
| 120 model.AddInstalledService(string16(), GURL(), | 92 model.AddInstalledService(string16(), GURL(), |
| 121 WebIntentPickerModel::DISPOSITION_WINDOW); | 93 WebIntentPickerModel::DISPOSITION_WINDOW); |
| 122 model.AddInstalledService(string16(), GURL(), | 94 model.AddInstalledService(string16(), GURL(), |
| 123 WebIntentPickerModel::DISPOSITION_WINDOW); | 95 WebIntentPickerModel::DISPOSITION_WINDOW); |
| 124 | 96 |
| 125 [controller_ performLayoutWithModel:&model]; | 97 [controller_ performLayoutWithModel:&model]; |
| 126 | 98 |
| 127 CheckWindow(/*icon_count=*/2); | 99 CheckWindow(/*row_count=*/2); |
| 128 } | |
| 129 | |
| 130 TEST_F(WebIntentPickerSheetControllerTest, OnCancelledWillSignalClose) { | |
| 131 CreatePicker(); | |
| 132 | |
| 133 EXPECT_CALL(delegate_, OnCancelled()); | |
| 134 EXPECT_CALL(delegate_, OnClosing()); | |
| 135 picker_->OnCancelled(); | |
| 136 | |
| 137 ignore_result(picker_.release()); // Closing |picker_| will destruct it. | |
| 138 } | 100 } |
| 139 | 101 |
| 140 TEST_F(WebIntentPickerSheetControllerTest, SuggestionView) { | 102 TEST_F(WebIntentPickerSheetControllerTest, SuggestionView) { |
| 141 CreateBubble(contents_wrapper()); | |
| 142 | |
| 143 WebIntentPickerModel model; | 103 WebIntentPickerModel model; |
| 144 | 104 |
| 145 model.AddSuggestedExtension(string16(), string16(), 2.5); | 105 model.AddSuggestedExtension(string16(), string16(), 2.5); |
| 146 [controller_ performLayoutWithModel:&model]; | 106 [controller_ performLayoutWithModel:&model]; |
| 147 | 107 |
| 148 // Get subviews. | 108 // Get subviews. |
| 149 NSArray* flip_views = [[window_ contentView] subviews]; | 109 NSArray* flip_views = [[window_ contentView] subviews]; |
| 150 NSArray* main_views = [[flip_views objectAtIndex:0] subviews]; | 110 NSArray* main_views = [[flip_views objectAtIndex:0] subviews]; |
| 151 | 111 |
| 152 // 4th object should be the suggestion view. | 112 // 4th object should be the suggestion view. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 168 isKindOfClass:[NSButton class]]); | 128 isKindOfClass:[NSButton class]]); |
| 169 NSButton* title_button = [[item_view subviews] objectAtIndex:1]; | 129 NSButton* title_button = [[item_view subviews] objectAtIndex:1]; |
| 170 CheckButton(title_button, @selector(openExtensionLink:)); | 130 CheckButton(title_button, @selector(openExtensionLink:)); |
| 171 | 131 |
| 172 // Verify "Add to Chromium" button is hooked up properly | 132 // Verify "Add to Chromium" button is hooked up properly |
| 173 ASSERT_TRUE([[[item_view subviews] objectAtIndex:7] | 133 ASSERT_TRUE([[[item_view subviews] objectAtIndex:7] |
| 174 isKindOfClass:[NSButton class]]); | 134 isKindOfClass:[NSButton class]]); |
| 175 NSButton* add_button = [[item_view subviews] objectAtIndex:7]; | 135 NSButton* add_button = [[item_view subviews] objectAtIndex:7]; |
| 176 CheckButton(add_button, @selector(installExtension:)); | 136 CheckButton(add_button, @selector(installExtension:)); |
| 177 } | 137 } |
| OLD | NEW |