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