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