OLD | NEW |
(Empty) | |
| 1 // Copyright 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. |
| 4 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 11 #import "ios/chrome/browser/sessions/test_session_service.h" |
| 12 #import "ios/chrome/browser/tabs/tab.h" |
| 13 #import "ios/chrome/browser/tabs/tab_model.h" |
| 14 #import "ios/chrome/browser/ui/tabs/tab_strip_controller.h" |
| 15 #import "ios/chrome/browser/ui/tabs/tab_strip_view.h" |
| 16 #import "ios/chrome/browser/ui/tabs/tab_view.h" |
| 17 #include "ios/chrome/browser/ui/ui_util.h" |
| 18 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/gtest_mac.h" |
| 21 #include "testing/platform_test.h" |
| 22 #import "third_party/ocmock/OCMock/OCMock.h" |
| 23 #import "third_party/ocmock/gtest_support.h" |
| 24 |
| 25 @interface ArrayBackedTabModel : TabModel { |
| 26 @private |
| 27 base::scoped_nsobject<NSMutableArray> tabsForTesting_; |
| 28 } |
| 29 |
| 30 @end |
| 31 |
| 32 @implementation ArrayBackedTabModel |
| 33 |
| 34 - (instancetype)initWithSessionWindow:(SessionWindowIOS*)window |
| 35 sessionService:(SessionServiceIOS*)service |
| 36 browserState:(ios::ChromeBrowserState*)browserState { |
| 37 if ((self = [super initWithSessionWindow:window |
| 38 sessionService:service |
| 39 browserState:browserState])) { |
| 40 tabsForTesting_.reset([[NSMutableArray alloc] initWithCapacity:5]); |
| 41 } |
| 42 return self; |
| 43 } |
| 44 |
| 45 - (void)addTabForTesting:(Tab*)tab { |
| 46 [tabsForTesting_ addObject:tab]; |
| 47 } |
| 48 |
| 49 - (Tab*)tabAtIndex:(NSUInteger)index { |
| 50 return (Tab*)[tabsForTesting_ objectAtIndex:index]; |
| 51 } |
| 52 |
| 53 - (NSUInteger)indexOfTab:(Tab*)tab { |
| 54 return [tabsForTesting_ indexOfObject:tab]; |
| 55 } |
| 56 |
| 57 - (BOOL)isEmpty { |
| 58 return [tabsForTesting_ count] == 0; |
| 59 } |
| 60 |
| 61 - (NSUInteger)count { |
| 62 return [tabsForTesting_ count]; |
| 63 } |
| 64 |
| 65 // Pass along fast enumeration calls to the tab array. |
| 66 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
| 67 objects:(id*)stackbuf |
| 68 count:(NSUInteger)len { |
| 69 return [tabsForTesting_ countByEnumeratingWithState:state |
| 70 objects:stackbuf |
| 71 count:len]; |
| 72 } |
| 73 |
| 74 - (void)closeTabAtIndex:(NSUInteger)index { |
| 75 DCHECK(index < [tabsForTesting_ count]); |
| 76 [self closeTab:[tabsForTesting_ objectAtIndex:index]]; |
| 77 } |
| 78 |
| 79 @end |
| 80 |
| 81 namespace { |
| 82 |
| 83 class TabStripControllerTest : public PlatformTest { |
| 84 protected: |
| 85 void SetUp() override { |
| 86 if (!IsIPadIdiom()) |
| 87 return; |
| 88 |
| 89 // Need a ChromeBrowserState for the tab model. |
| 90 TestChromeBrowserState::Builder test_cbs_builder; |
| 91 chrome_browser_state_ = test_cbs_builder.Build(); |
| 92 |
| 93 // Setup mock TabModel, sessionService, and Tabs. |
| 94 base::scoped_nsobject<TestSessionService> test_service( |
| 95 [[TestSessionService alloc] init]); |
| 96 real_tab_model_.reset([[ArrayBackedTabModel alloc] |
| 97 initWithSessionWindow:nil |
| 98 sessionService:test_service |
| 99 browserState:chrome_browser_state_.get()]); |
| 100 id tabModel = [OCMockObject partialMockForObject:real_tab_model_]; |
| 101 id tab1 = [OCMockObject mockForClass:[Tab class]]; |
| 102 id tab2 = [OCMockObject mockForClass:[Tab class]]; |
| 103 |
| 104 // Populate the tab model. |
| 105 [real_tab_model_ addTabForTesting:tab1]; |
| 106 [real_tab_model_ addTabForTesting:tab2]; |
| 107 |
| 108 // Stub methods for TabModel. |
| 109 [[[tabModel stub] andDo:^(NSInvocation* invocation) { |
| 110 // Return the raw pointer to the C++ object. |
| 111 ios::ChromeBrowserState* browser_state = chrome_browser_state_.get(); |
| 112 [invocation setReturnValue:&browser_state]; |
| 113 }] browserState]; |
| 114 [[tabModel stub] addObserver:[OCMArg any]]; |
| 115 [[tabModel stub] removeObserver:[OCMArg any]]; |
| 116 |
| 117 // Stub methods for Tabs. |
| 118 [[[tab1 stub] andReturn:@"Tab Title 1"] title]; |
| 119 [[[tab2 stub] andReturn:@"Tab Title 2"] title]; |
| 120 [[[tab1 stub] andReturn:nil] favicon]; |
| 121 [[[tab2 stub] andReturn:nil] favicon]; |
| 122 [[tab1 stub] close]; |
| 123 [[tab2 stub] close]; |
| 124 |
| 125 tabModel_.reset([tabModel retain]); |
| 126 tab1_.reset([tab1 retain]); |
| 127 tab2_.reset([tab2 retain]); |
| 128 controller_.reset([[TabStripController alloc] |
| 129 initWithTabModel:(TabModel*)tabModel_.get() |
| 130 style:TabStrip::kStyleDark]); |
| 131 |
| 132 // Force the view to load. |
| 133 UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectZero]; |
| 134 [window addSubview:[controller_ view]]; |
| 135 window_.reset(window); |
| 136 } |
| 137 void TearDown() override { |
| 138 if (!IsIPadIdiom()) |
| 139 return; |
| 140 [real_tab_model_ browserStateDestroyed]; |
| 141 } |
| 142 |
| 143 web::TestWebThreadBundle thread_bundle_; |
| 144 base::scoped_nsobject<OCMockObject> tab1_; |
| 145 base::scoped_nsobject<OCMockObject> tab2_; |
| 146 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 147 base::scoped_nsobject<OCMockObject> tabModel_; |
| 148 base::scoped_nsobject<TabStripController> controller_; |
| 149 base::scoped_nsobject<UIWindow> window_; |
| 150 base::scoped_nsobject<ArrayBackedTabModel> real_tab_model_; |
| 151 }; |
| 152 |
| 153 TEST_F(TabStripControllerTest, LoadAndDisplay) { |
| 154 if (!IsIPadIdiom()) |
| 155 return; |
| 156 |
| 157 // If this doesn't crash, we're good. |
| 158 ASSERT_OCMOCK_VERIFY(tabModel_); |
| 159 ASSERT_OCMOCK_VERIFY(tab1_); |
| 160 ASSERT_OCMOCK_VERIFY(tab2_); |
| 161 |
| 162 // There should be two TabViews and one new tab button nested within the |
| 163 // parent view (which contains exactly one scroll view). |
| 164 EXPECT_EQ(3U, |
| 165 [[[[[controller_ view] subviews] objectAtIndex:0] subviews] count]); |
| 166 } |
| 167 |
| 168 } // namespace |
OLD | NEW |