| 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 <objc/runtime.h> | 5 #import <objc/runtime.h> |
| 6 | 6 |
| 7 #include "base/files/file_path.h" |
| 7 #include "base/mac/scoped_nsautorelease_pool.h" | 8 #include "base/mac/scoped_nsautorelease_pool.h" |
| 8 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 11 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 13 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h" |
| 12 #include "ios/chrome/browser/chrome_url_constants.h" | 14 #include "ios/chrome/browser/chrome_url_constants.h" |
| 13 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 15 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 14 #import "ios/chrome/browser/sessions/session_window.h" | 16 #import "ios/chrome/browser/sessions/session_window.h" |
| 15 #import "ios/chrome/browser/sessions/test_session_service.h" | 17 #import "ios/chrome/browser/sessions/test_session_service.h" |
| 16 #import "ios/chrome/browser/tabs/tab.h" | 18 #import "ios/chrome/browser/tabs/tab.h" |
| 17 #import "ios/chrome/browser/tabs/tab_model.h" | 19 #import "ios/chrome/browser/tabs/tab_model.h" |
| 18 #import "ios/chrome/browser/tabs/tab_model_observer.h" | 20 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
| 19 #import "ios/chrome/browser/tabs/tab_private.h" | 21 #import "ios/chrome/browser/tabs/tab_private.h" |
| 20 #import "ios/chrome/browser/web/chrome_web_client.h" | 22 #import "ios/chrome/browser/web/chrome_web_client.h" |
| 23 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager
.h" |
| 21 #import "ios/web/navigation/crw_session_controller.h" | 24 #import "ios/web/navigation/crw_session_controller.h" |
| 22 #import "ios/web/navigation/navigation_manager_impl.h" | 25 #import "ios/web/navigation/navigation_manager_impl.h" |
| 23 #import "ios/web/public/navigation_manager.h" | 26 #import "ios/web/public/navigation_manager.h" |
| 24 #include "ios/web/public/referrer.h" | 27 #include "ios/web/public/referrer.h" |
| 25 #include "ios/web/public/test/scoped_testing_web_client.h" | 28 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 26 #include "ios/web/public/test/test_web_thread_bundle.h" | 29 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 27 #include "ios/web/public/web_thread.h" | 30 #include "ios/web/public/web_thread.h" |
| 28 #import "ios/web/web_state/ui/crw_web_controller.h" | 31 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 29 #import "ios/web/web_state/web_state_impl.h" | 32 #import "ios/web/web_state/web_state_impl.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "testing/gtest_mac.h" | 34 #include "testing/gtest_mac.h" |
| 32 #include "testing/platform_test.h" | 35 #include "testing/platform_test.h" |
| 33 #import "third_party/ocmock/OCMock/OCMock.h" | 36 #import "third_party/ocmock/OCMock/OCMock.h" |
| 34 #include "third_party/ocmock/gtest_support.h" | 37 #include "third_party/ocmock/gtest_support.h" |
| 35 | 38 |
| 36 using web::WebStateImpl; | 39 using web::WebStateImpl; |
| 37 | 40 |
| 38 // For some of the unit tests, we need to make sure the session is saved | 41 // For some of the unit tests, we need to make sure the session is saved |
| 39 // immediately so we can read it back in to verify various attributes. This | 42 // immediately so we can read it back in to verify various attributes. This |
| 40 // is not a situation we normally expect to be in because we never | 43 // is not a situation we normally expect to be in because we never |
| 41 // want the session being saved on the main thread in the production app. | 44 // want the session being saved on the main thread in the production app. |
| 42 // We could expose this as part of the service's public API, but again that | 45 // We could expose this as part of the service's public API, but again that |
| 43 // might encourage use where we don't want it. As a result, just use the | 46 // might encourage use where we don't want it. As a result, just use the |
| 44 // known private-for-testing method directly. | 47 // known private-for-testing method directly. |
| 45 @interface SessionServiceIOS (Testing) | 48 @interface SessionServiceIOS (Testing) |
| 46 - (void)performSaveWindow:(SessionWindowIOS*)window | 49 - (void)performSaveWindow:(SessionWindowIOS*)window |
| 47 toDirectory:(NSString*)directory; | 50 toDirectory:(NSString*)directory; |
| 48 @end | 51 @end |
| 49 | 52 |
| 50 // Trivial objective C class whose unique aim is to be a wrapper of C++ | |
| 51 // classes. | |
| 52 @interface ClassesWrapper : NSObject { | |
| 53 @public | |
| 54 std::unique_ptr<WebStateImpl> _webStateImpl; | |
| 55 } | |
| 56 @end | |
| 57 | |
| 58 @implementation ClassesWrapper | |
| 59 @end | |
| 60 | |
| 61 @interface TabTest : Tab | 53 @interface TabTest : Tab |
| 62 | 54 |
| 63 - (instancetype)initWithWindowName:(NSString*)windowName | 55 - (instancetype)initWithWindowName:(NSString*)windowName |
| 64 lastVisitedTimestamp:(double)lastVisitedTimestamp | 56 lastVisitedTimestamp:(double)lastVisitedTimestamp |
| 65 browserState:(ios::ChromeBrowserState*)browserState | 57 browserState:(ios::ChromeBrowserState*)browserState |
| 66 tabModel:(TabModel*)tabModel; | 58 tabModel:(TabModel*)tabModel; |
| 67 @end | 59 @end |
| 68 | 60 |
| 69 @implementation TabTest | 61 @implementation TabTest |
| 70 | 62 |
| 71 - (instancetype)initWithWindowName:(NSString*)windowName | 63 - (instancetype)initWithWindowName:(NSString*)windowName |
| 72 lastVisitedTimestamp:(double)lastVisitedTimestamp | 64 lastVisitedTimestamp:(double)lastVisitedTimestamp |
| 73 browserState:(ios::ChromeBrowserState*)browserState | 65 browserState:(ios::ChromeBrowserState*)browserState |
| 74 tabModel:(TabModel*)tabModel { | 66 tabModel:(TabModel*)tabModel { |
| 75 self = [super initWithWindowName:windowName | 67 self = [super initWithWindowName:windowName |
| 76 opener:nil | 68 opener:nil |
| 77 openedByDOM:NO | 69 openedByDOM:NO |
| 78 model:tabModel | 70 model:tabModel |
| 79 browserState:browserState]; | 71 browserState:browserState]; |
| 80 if (self) { | 72 if (self) { |
| 81 id webControllerMock = | 73 id webControllerMock = |
| 82 [OCMockObject niceMockForClass:[CRWWebController class]]; | 74 [OCMockObject niceMockForClass:[CRWWebController class]]; |
| 83 | 75 |
| 84 std::unique_ptr<WebStateImpl> webStateImpl(new WebStateImpl(browserState)); | 76 auto webStateImpl = base::MakeUnique<WebStateImpl>(browserState); |
| 85 webStateImpl->SetWebController(webControllerMock); | 77 webStateImpl->SetWebController(webControllerMock); |
| 86 webStateImpl->GetNavigationManagerImpl().InitializeSession( | 78 webStateImpl->GetNavigationManagerImpl().InitializeSession( |
| 87 windowName, @"opener", NO, -1); | 79 windowName, @"opener", NO, -1); |
| 88 [webStateImpl->GetNavigationManagerImpl().GetSessionController() | 80 [webStateImpl->GetNavigationManagerImpl().GetSessionController() |
| 89 setLastVisitedTimestamp:lastVisitedTimestamp]; | 81 setLastVisitedTimestamp:lastVisitedTimestamp]; |
| 90 | 82 |
| 91 WebStateImpl* webStateImplPtr = webStateImpl.get(); | 83 WebStateImpl* webStateImplPtr = webStateImpl.get(); |
| 92 [[[webControllerMock stub] andReturnValue:OCMOCK_VALUE(webStateImplPtr)] | 84 [[[webControllerMock stub] andReturnValue:OCMOCK_VALUE(webStateImplPtr)] |
| 93 webStateImpl]; | 85 webStateImpl]; |
| 94 BOOL yes = YES; | 86 BOOL yes = YES; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 122 |
| 131 const GURL kURL("https://www.some.url.com"); | 123 const GURL kURL("https://www.some.url.com"); |
| 132 const web::Referrer kEmptyReferrer; | 124 const web::Referrer kEmptyReferrer; |
| 133 const web::Referrer kReferrer(GURL("https//www.some.referer.com"), | 125 const web::Referrer kReferrer(GURL("https//www.some.referer.com"), |
| 134 web::ReferrerPolicyDefault); | 126 web::ReferrerPolicyDefault); |
| 135 const web::Referrer kReferrer2(GURL("https//www.some.referer2.com"), | 127 const web::Referrer kReferrer2(GURL("https//www.some.referer2.com"), |
| 136 web::ReferrerPolicyDefault); | 128 web::ReferrerPolicyDefault); |
| 137 | 129 |
| 138 class TabModelTest : public PlatformTest { | 130 class TabModelTest : public PlatformTest { |
| 139 public: | 131 public: |
| 140 TabModelTest() : web_client_(base::MakeUnique<ChromeWebClient>()) {} | 132 TabModelTest() |
| 141 | 133 : scoped_browser_state_manager_( |
| 142 protected: | 134 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), |
| 143 void SetUp() override { | 135 web_client_(base::MakeUnique<ChromeWebClient>()) { |
| 144 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 136 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 145 PlatformTest::SetUp(); | |
| 146 | 137 |
| 147 TestChromeBrowserState::Builder test_cbs_builder; | 138 TestChromeBrowserState::Builder test_cbs_builder; |
| 148 chrome_browser_state_ = test_cbs_builder.Build(); | 139 chrome_browser_state_ = test_cbs_builder.Build(); |
| 149 | 140 |
| 150 sessionWindow_.reset([[SessionWindowIOS alloc] init]); | 141 session_window_.reset([[SessionWindowIOS alloc] init]); |
| 151 // Create tab model with just a dummy session service so the async state | 142 // Create tab model with just a dummy session service so the async state |
| 152 // saving doesn't trigger unless actually wanted. | 143 // saving doesn't trigger unless actually wanted. |
| 153 base::scoped_nsobject<TestSessionService> test_service( | 144 base::scoped_nsobject<TestSessionService> test_service( |
| 154 [[TestSessionService alloc] init]); | 145 [[TestSessionService alloc] init]); |
| 155 tabModel_.reset([[TabModel alloc] | 146 tab_model_.reset([[TabModel alloc] |
| 156 initWithSessionWindow:sessionWindow_.get() | 147 initWithSessionWindow:session_window_.get() |
| 157 sessionService:test_service | 148 sessionService:test_service |
| 158 browserState:chrome_browser_state_.get()]); | 149 browserState:chrome_browser_state_.get()]); |
| 159 [tabModel_ setWebUsageEnabled:YES]; | 150 [tab_model_ setWebUsageEnabled:YES]; |
| 160 [tabModel_ setPrimary:YES]; | 151 [tab_model_ setPrimary:YES]; |
| 161 tabModelObserver_.reset([[TabModelObserverPong alloc] init]); | 152 tab_model_observer_.reset([[TabModelObserverPong alloc] init]); |
| 162 [tabModel_ addObserver:tabModelObserver_]; | 153 [tab_model_ addObserver:tab_model_observer_]; |
| 163 } | 154 } |
| 164 | 155 |
| 165 void TearDown() override { | 156 ~TabModelTest() override { |
| 166 [tabModel_ removeObserver:tabModelObserver_]; | 157 [tab_model_ removeObserver:tab_model_observer_]; |
| 167 [tabModel_ browserStateDestroyed]; | 158 [tab_model_ browserStateDestroyed]; |
| 168 PlatformTest::TearDown(); | |
| 169 } | 159 } |
| 170 | 160 |
| 161 protected: |
| 171 Tab* CreateTab(NSString* windowName, | 162 Tab* CreateTab(NSString* windowName, |
| 172 double lastVisitedTimestamp) NS_RETURNS_RETAINED { | 163 double lastVisitedTimestamp) NS_RETURNS_RETAINED { |
| 173 return [[TabTest alloc] initWithWindowName:windowName | 164 return [[TabTest alloc] initWithWindowName:windowName |
| 174 lastVisitedTimestamp:lastVisitedTimestamp | 165 lastVisitedTimestamp:lastVisitedTimestamp |
| 175 browserState:chrome_browser_state_.get() | 166 browserState:chrome_browser_state_.get() |
| 176 tabModel:tabModel_.get()]; | 167 tabModel:tab_model_.get()]; |
| 177 } | 168 } |
| 178 | 169 |
| 179 std::unique_ptr<WebStateImpl> CreateWebState(NSString* windowName, | 170 std::unique_ptr<WebStateImpl> CreateWebState(NSString* windowName, |
| 180 NSString* opener, | 171 NSString* opener, |
| 181 NSInteger index) { | 172 NSInteger index) { |
| 182 std::unique_ptr<WebStateImpl> webState( | 173 auto webState = base::MakeUnique<WebStateImpl>(chrome_browser_state_.get()); |
| 183 new WebStateImpl(chrome_browser_state_.get())); | |
| 184 webState->GetNavigationManagerImpl().InitializeSession(windowName, opener, | 174 webState->GetNavigationManagerImpl().InitializeSession(windowName, opener, |
| 185 NO, index); | 175 NO, index); |
| 186 return webState; | 176 return webState; |
| 187 } | 177 } |
| 188 | 178 |
| 189 std::unique_ptr<WebStateImpl> CreateWebState(NSString* windowName) { | 179 std::unique_ptr<WebStateImpl> CreateWebState(NSString* windowName) { |
| 190 return CreateWebState(windowName, @"", -1); | 180 return CreateWebState(windowName, @"", -1); |
| 191 } | 181 } |
| 192 | 182 |
| 193 std::unique_ptr<WebStateImpl> CreateChildWebState(Tab* parent) { | 183 std::unique_ptr<WebStateImpl> CreateChildWebState(Tab* parent) { |
| 194 return CreateWebState([parent windowName], [parent currentSessionID], -1); | 184 return CreateWebState([parent windowName], [parent currentSessionID], -1); |
| 195 } | 185 } |
| 196 | 186 |
| 197 void RestoreSession(SessionWindowIOS* window) { | 187 void RestoreSession(SessionWindowIOS* window) { |
| 198 [tabModel_ restoreSessionWindow:window]; | 188 [tab_model_ restoreSessionWindow:window]; |
| 199 } | 189 } |
| 200 | 190 |
| 201 // Creates a session window with |entries| entries and a |selectedIndex| of 1. | 191 // Creates a session window with |entries| entries and a |selectedIndex| of 1. |
| 202 SessionWindowIOS* CreateSessionWindow(int entries) { | 192 SessionWindowIOS* CreateSessionWindow(int entries) { |
| 203 SessionWindowIOS* window = [[SessionWindowIOS alloc] init]; | 193 SessionWindowIOS* window = [[SessionWindowIOS alloc] init]; |
| 204 for (int i = 0; i < entries; i++) { | 194 for (int i = 0; i < entries; i++) { |
| 205 NSString* windowName = [NSString stringWithFormat:@"window %d", i + 1]; | 195 NSString* windowName = [NSString stringWithFormat:@"window %d", i + 1]; |
| 206 [window addSession:CreateWebState(windowName)]; | 196 [window addSession:CreateWebState(windowName)]; |
| 207 } | 197 } |
| 208 if (entries) | 198 if (entries) |
| 209 [window setSelectedIndex:1]; | 199 [window setSelectedIndex:1]; |
| 210 return window; | 200 return window; |
| 211 } | 201 } |
| 212 | 202 |
| 213 web::TestWebThreadBundle thread_bundle_; | 203 web::TestWebThreadBundle thread_bundle_; |
| 204 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_; |
| 214 web::ScopedTestingWebClient web_client_; | 205 web::ScopedTestingWebClient web_client_; |
| 215 base::scoped_nsobject<SessionWindowIOS> sessionWindow_; | 206 base::scoped_nsobject<SessionWindowIOS> session_window_; |
| 216 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 207 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 217 base::scoped_nsobject<TabModel> tabModel_; | 208 base::scoped_nsobject<TabModel> tab_model_; |
| 218 base::scoped_nsobject<TabModelObserverPong> tabModelObserver_; | 209 base::scoped_nsobject<TabModelObserverPong> tab_model_observer_; |
| 219 base::mac::ScopedNSAutoreleasePool pool_; | 210 base::mac::ScopedNSAutoreleasePool pool_; |
| 220 }; | 211 }; |
| 221 | 212 |
| 222 TEST_F(TabModelTest, IsEmpty) { | 213 TEST_F(TabModelTest, IsEmpty) { |
| 223 EXPECT_EQ([tabModel_ count], 0U); | 214 EXPECT_EQ([tab_model_ count], 0U); |
| 224 EXPECT_TRUE([tabModel_ isEmpty]); | 215 EXPECT_TRUE([tab_model_ isEmpty]); |
| 225 [tabModel_ insertTabWithURL:kURL | 216 [tab_model_ insertTabWithURL:kURL |
| 226 referrer:kReferrer | 217 referrer:kReferrer |
| 227 windowName:@"window 1" | 218 windowName:@"window 1" |
| 228 opener:nil | 219 opener:nil |
| 229 atIndex:0]; | 220 atIndex:0]; |
| 230 ASSERT_EQ(1U, [tabModel_ count]); | 221 ASSERT_EQ(1U, [tab_model_ count]); |
| 231 EXPECT_FALSE([tabModel_ isEmpty]); | 222 EXPECT_FALSE([tab_model_ isEmpty]); |
| 232 } | 223 } |
| 233 | 224 |
| 234 TEST_F(TabModelTest, InsertUrlSingle) { | 225 TEST_F(TabModelTest, InsertUrlSingle) { |
| 235 [tabModel_ insertTabWithURL:kURL | 226 [tab_model_ insertTabWithURL:kURL |
| 236 referrer:kReferrer | 227 referrer:kReferrer |
| 237 windowName:@"window 1" | 228 windowName:@"window 1" |
| 238 opener:nil | 229 opener:nil |
| 239 atIndex:0]; | 230 atIndex:0]; |
| 240 ASSERT_EQ(1U, [tabModel_ count]); | 231 ASSERT_EQ(1U, [tab_model_ count]); |
| 241 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 232 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 242 } | 233 } |
| 243 | 234 |
| 244 TEST_F(TabModelTest, InsertUrlMultiple) { | 235 TEST_F(TabModelTest, InsertUrlMultiple) { |
| 245 [tabModel_ insertTabWithURL:kURL | 236 [tab_model_ insertTabWithURL:kURL |
| 246 referrer:kReferrer | 237 referrer:kReferrer |
| 247 windowName:@"window 1" | 238 windowName:@"window 1" |
| 248 opener:nil | 239 opener:nil |
| 249 atIndex:0]; | 240 atIndex:0]; |
| 250 [tabModel_ insertTabWithURL:kURL | 241 [tab_model_ insertTabWithURL:kURL |
| 251 referrer:kReferrer | 242 referrer:kReferrer |
| 252 windowName:@"window 2" | 243 windowName:@"window 2" |
| 253 opener:nil | 244 opener:nil |
| 254 atIndex:0]; | 245 atIndex:0]; |
| 255 [tabModel_ insertTabWithURL:kURL | 246 [tab_model_ insertTabWithURL:kURL |
| 256 referrer:kReferrer | 247 referrer:kReferrer |
| 257 windowName:@"window 3" | 248 windowName:@"window 3" |
| 258 opener:nil | 249 opener:nil |
| 259 atIndex:1]; | 250 atIndex:1]; |
| 260 | 251 |
| 261 ASSERT_EQ(3U, [tabModel_ count]); | 252 ASSERT_EQ(3U, [tab_model_ count]); |
| 262 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 253 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 263 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 254 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 264 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:2] windowName]); | 255 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:2] windowName]); |
| 265 } | 256 } |
| 266 | 257 |
| 267 TEST_F(TabModelTest, AppendUrlSingle) { | 258 TEST_F(TabModelTest, AppendUrlSingle) { |
| 268 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 259 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 269 ASSERT_EQ(1U, [tabModel_ count]); | 260 ASSERT_EQ(1U, [tab_model_ count]); |
| 270 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 261 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 271 } | 262 } |
| 272 | 263 |
| 273 TEST_F(TabModelTest, AppendUrlMultiple) { | 264 TEST_F(TabModelTest, AppendUrlMultiple) { |
| 274 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 265 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 275 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 266 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 276 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 267 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 277 | 268 |
| 278 ASSERT_EQ(3U, [tabModel_ count]); | 269 ASSERT_EQ(3U, [tab_model_ count]); |
| 279 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 270 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 280 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:1] windowName]); | 271 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:1] windowName]); |
| 281 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:2] windowName]); | 272 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:2] windowName]); |
| 282 } | 273 } |
| 283 | 274 |
| 284 TEST_F(TabModelTest, CloseTabAtIndexBeginning) { | 275 TEST_F(TabModelTest, CloseTabAtIndexBeginning) { |
| 285 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 276 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 286 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 277 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 287 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 278 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 288 | 279 |
| 289 [tabModel_ closeTabAtIndex:0]; | 280 [tab_model_ closeTabAtIndex:0]; |
| 290 | 281 |
| 291 ASSERT_EQ(2U, [tabModel_ count]); | 282 ASSERT_EQ(2U, [tab_model_ count]); |
| 292 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 283 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 293 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 284 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 294 } | 285 } |
| 295 | 286 |
| 296 TEST_F(TabModelTest, CloseTabAtIndexMiddle) { | 287 TEST_F(TabModelTest, CloseTabAtIndexMiddle) { |
| 297 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 288 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 298 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 289 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 299 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 290 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 300 | 291 |
| 301 [tabModel_ closeTabAtIndex:1]; | 292 [tab_model_ closeTabAtIndex:1]; |
| 302 | 293 |
| 303 ASSERT_EQ(2U, [tabModel_ count]); | 294 ASSERT_EQ(2U, [tab_model_ count]); |
| 304 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 295 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 305 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 296 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 306 } | 297 } |
| 307 | 298 |
| 308 TEST_F(TabModelTest, CloseTabAtIndexLast) { | 299 TEST_F(TabModelTest, CloseTabAtIndexLast) { |
| 309 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 300 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 310 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 301 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 311 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 302 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 312 | 303 |
| 313 [tabModel_ closeTabAtIndex:2]; | 304 [tab_model_ closeTabAtIndex:2]; |
| 314 | 305 |
| 315 ASSERT_EQ(2U, [tabModel_ count]); | 306 ASSERT_EQ(2U, [tab_model_ count]); |
| 316 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 307 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 317 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:1] windowName]); | 308 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:1] windowName]); |
| 318 } | 309 } |
| 319 | 310 |
| 320 TEST_F(TabModelTest, CloseTabAtIndexOnlyOne) { | 311 TEST_F(TabModelTest, CloseTabAtIndexOnlyOne) { |
| 321 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 312 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 322 | 313 |
| 323 [tabModel_ closeTabAtIndex:0]; | 314 [tab_model_ closeTabAtIndex:0]; |
| 324 | 315 |
| 325 EXPECT_EQ(0U, [tabModel_ count]); | 316 EXPECT_EQ(0U, [tab_model_ count]); |
| 326 } | 317 } |
| 327 | 318 |
| 328 TEST_F(TabModelTest, RestoreSessionOnNTPTest) { | 319 TEST_F(TabModelTest, RestoreSessionOnNTPTest) { |
| 329 [tabModel_ insertTabWithURL:GURL(kChromeUINewTabURL) | 320 [tab_model_ insertTabWithURL:GURL(kChromeUINewTabURL) |
| 330 referrer:kEmptyReferrer | 321 referrer:kEmptyReferrer |
| 331 windowName:@"old window" | 322 windowName:@"old window" |
| 332 opener:nil | 323 opener:nil |
| 333 atIndex:0]; | 324 atIndex:0]; |
| 334 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); | 325 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); |
| 335 | 326 |
| 336 RestoreSession(window.get()); | 327 RestoreSession(window.get()); |
| 337 ASSERT_EQ(3U, [tabModel_ count]); | 328 ASSERT_EQ(3U, [tab_model_ count]); |
| 338 EXPECT_NSEQ(@"window 2", [[tabModel_ currentTab] windowName]); | 329 EXPECT_NSEQ(@"window 2", [[tab_model_ currentTab] windowName]); |
| 339 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 330 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 340 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:1] windowName]); | 331 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:1] windowName]); |
| 341 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:2] windowName]); | 332 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:2] windowName]); |
| 342 } | 333 } |
| 343 | 334 |
| 344 TEST_F(TabModelTest, RestoreSessionOn2NtpTest) { | 335 TEST_F(TabModelTest, RestoreSessionOn2NtpTest) { |
| 345 [tabModel_ insertTabWithURL:GURL(kChromeUINewTabURL) | 336 [tab_model_ insertTabWithURL:GURL(kChromeUINewTabURL) |
| 346 referrer:kEmptyReferrer | 337 referrer:kEmptyReferrer |
| 347 windowName:@"old window 1" | 338 windowName:@"old window 1" |
| 348 opener:nil | 339 opener:nil |
| 349 atIndex:0]; | 340 atIndex:0]; |
| 350 [tabModel_ insertTabWithURL:GURL(kChromeUINewTabURL) | 341 [tab_model_ insertTabWithURL:GURL(kChromeUINewTabURL) |
| 351 referrer:kEmptyReferrer | 342 referrer:kEmptyReferrer |
| 352 windowName:@"old window 2" | 343 windowName:@"old window 2" |
| 353 opener:nil | 344 opener:nil |
| 354 atIndex:1]; | 345 atIndex:1]; |
| 355 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); | 346 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); |
| 356 | 347 |
| 357 RestoreSession(window.get()); | 348 RestoreSession(window.get()); |
| 358 ASSERT_EQ(5U, [tabModel_ count]); | 349 ASSERT_EQ(5U, [tab_model_ count]); |
| 359 EXPECT_NSEQ(@"window 2", [[tabModel_ currentTab] windowName]); | 350 EXPECT_NSEQ(@"window 2", [[tab_model_ currentTab] windowName]); |
| 360 EXPECT_NSEQ(@"old window 1", [[tabModel_ tabAtIndex:0] windowName]); | 351 EXPECT_NSEQ(@"old window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 361 EXPECT_NSEQ(@"old window 2", [[tabModel_ tabAtIndex:1] windowName]); | 352 EXPECT_NSEQ(@"old window 2", [[tab_model_ tabAtIndex:1] windowName]); |
| 362 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:2] windowName]); | 353 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:2] windowName]); |
| 363 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:3] windowName]); | 354 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:3] windowName]); |
| 364 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:4] windowName]); | 355 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:4] windowName]); |
| 365 } | 356 } |
| 366 | 357 |
| 367 TEST_F(TabModelTest, RestoreSessionOnAnyTest) { | 358 TEST_F(TabModelTest, RestoreSessionOnAnyTest) { |
| 368 [tabModel_ insertTabWithURL:kURL | 359 [tab_model_ insertTabWithURL:kURL |
| 369 referrer:kEmptyReferrer | 360 referrer:kEmptyReferrer |
| 370 windowName:@"old window 1" | 361 windowName:@"old window 1" |
| 371 opener:nil | 362 opener:nil |
| 372 atIndex:0]; | 363 atIndex:0]; |
| 373 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); | 364 base::scoped_nsobject<SessionWindowIOS> window(CreateSessionWindow(3)); |
| 374 | 365 |
| 375 RestoreSession(window.get()); | 366 RestoreSession(window.get()); |
| 376 ASSERT_EQ(4U, [tabModel_ count]); | 367 ASSERT_EQ(4U, [tab_model_ count]); |
| 377 EXPECT_NSEQ(@"window 2", [[tabModel_ currentTab] windowName]); | 368 EXPECT_NSEQ(@"window 2", [[tab_model_ currentTab] windowName]); |
| 378 EXPECT_NSEQ(@"old window 1", [[tabModel_ tabAtIndex:0] windowName]); | 369 EXPECT_NSEQ(@"old window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 379 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:1] windowName]); | 370 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:1] windowName]); |
| 380 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:2] windowName]); | 371 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:2] windowName]); |
| 381 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:3] windowName]); | 372 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:3] windowName]); |
| 382 } | 373 } |
| 383 | 374 |
| 384 TEST_F(TabModelTest, TabForWindowName) { | 375 TEST_F(TabModelTest, TabForWindowName) { |
| 385 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 376 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 386 [tabModel_ addTabWithURL:GURL("https://www.some.url2.com") | 377 [tab_model_ addTabWithURL:GURL("https://www.some.url2.com") |
| 387 referrer:kReferrer2 | 378 referrer:kReferrer2 |
| 388 windowName:@"window 2"]; | 379 windowName:@"window 2"]; |
| 389 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 380 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 390 | 381 |
| 391 Tab* tab = [tabModel_ tabWithWindowName:@"window 2"]; | 382 Tab* tab = [tab_model_ tabWithWindowName:@"window 2"]; |
| 392 | 383 |
| 393 EXPECT_NSEQ([tab windowName], @"window 2"); | 384 EXPECT_NSEQ([tab windowName], @"window 2"); |
| 394 EXPECT_EQ(tab.url, GURL("https://www.some.url2.com/")); | 385 EXPECT_EQ(tab.url, GURL("https://www.some.url2.com/")); |
| 395 } | 386 } |
| 396 | 387 |
| 397 TEST_F(TabModelTest, TabForWindowNameNotFound) { | 388 TEST_F(TabModelTest, TabForWindowNameNotFound) { |
| 398 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 389 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 399 [tabModel_ addTabWithURL:GURL("https://www.some.url2.com") | 390 [tab_model_ addTabWithURL:GURL("https://www.some.url2.com") |
| 400 referrer:kReferrer2 | 391 referrer:kReferrer2 |
| 401 windowName:@"window 2"]; | 392 windowName:@"window 2"]; |
| 402 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 393 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 403 | 394 |
| 404 Tab* tab = [tabModel_ tabWithWindowName:@"window not found"]; | 395 Tab* tab = [tab_model_ tabWithWindowName:@"window not found"]; |
| 405 | 396 |
| 406 EXPECT_EQ(nil, tab); | 397 EXPECT_EQ(nil, tab); |
| 407 } | 398 } |
| 408 | 399 |
| 409 TEST_F(TabModelTest, CloseAllTabs) { | 400 TEST_F(TabModelTest, CloseAllTabs) { |
| 410 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 401 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 411 [tabModel_ addTabWithURL:GURL("https://www.some.url2.com") | 402 [tab_model_ addTabWithURL:GURL("https://www.some.url2.com") |
| 412 referrer:kReferrer2 | 403 referrer:kReferrer2 |
| 413 windowName:@"window 2"]; | 404 windowName:@"window 2"]; |
| 414 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 405 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 415 | 406 |
| 416 [tabModel_ closeAllTabs]; | 407 [tab_model_ closeAllTabs]; |
| 417 | 408 |
| 418 EXPECT_EQ(0U, [tabModel_ count]); | 409 EXPECT_EQ(0U, [tab_model_ count]); |
| 419 } | 410 } |
| 420 | 411 |
| 421 TEST_F(TabModelTest, CloseAllTabsWithNoTabs) { | 412 TEST_F(TabModelTest, CloseAllTabsWithNoTabs) { |
| 422 [tabModel_ closeAllTabs]; | 413 [tab_model_ closeAllTabs]; |
| 423 | 414 |
| 424 EXPECT_EQ(0U, [tabModel_ count]); | 415 EXPECT_EQ(0U, [tab_model_ count]); |
| 425 } | 416 } |
| 426 | 417 |
| 427 TEST_F(TabModelTest, InsertWithSessionController) { | 418 TEST_F(TabModelTest, InsertWithSessionController) { |
| 428 EXPECT_EQ([tabModel_ count], 0U); | 419 EXPECT_EQ([tab_model_ count], 0U); |
| 429 EXPECT_TRUE([tabModel_ isEmpty]); | 420 EXPECT_TRUE([tab_model_ isEmpty]); |
| 430 | 421 |
| 431 Tab* new_tab = | 422 Tab* new_tab = |
| 432 [tabModel_ insertTabWithWebState:CreateWebState(@"window", @"opener", -1) | 423 [tab_model_ insertTabWithWebState:CreateWebState(@"window", @"opener", -1) |
| 433 atIndex:0]; | 424 atIndex:0]; |
| 434 EXPECT_EQ([tabModel_ count], 1U); | 425 EXPECT_EQ([tab_model_ count], 1U); |
| 435 [tabModel_ setCurrentTab:new_tab]; | 426 [tab_model_ setCurrentTab:new_tab]; |
| 436 Tab* current_tab = [tabModel_ currentTab]; | 427 Tab* current_tab = [tab_model_ currentTab]; |
| 437 EXPECT_TRUE(current_tab); | 428 EXPECT_TRUE(current_tab); |
| 438 } | 429 } |
| 439 | 430 |
| 440 TEST_F(TabModelTest, OpenerOfTab) { | 431 TEST_F(TabModelTest, OpenerOfTab) { |
| 441 // Start off with a couple tabs. | 432 // Start off with a couple tabs. |
| 442 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 433 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 443 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 434 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 444 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 435 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 445 | 436 |
| 446 // Create parent tab. | 437 // Create parent tab. |
| 447 Tab* parent_tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 438 Tab* parent_tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 448 atIndex:[tabModel_ count]]; | 439 atIndex:[tab_model_ count]]; |
| 449 // Create child tab. | 440 // Create child tab. |
| 450 Tab* child_tab = | 441 Tab* child_tab = |
| 451 [tabModel_ insertTabWithWebState:CreateChildWebState(parent_tab) | 442 [tab_model_ insertTabWithWebState:CreateChildWebState(parent_tab) |
| 452 atIndex:[tabModel_ count]]; | 443 atIndex:[tab_model_ count]]; |
| 453 // Create another unrelated tab. | 444 // Create another unrelated tab. |
| 454 Tab* another_tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 445 Tab* another_tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 455 atIndex:[tabModel_ count]]; | 446 atIndex:[tab_model_ count]]; |
| 456 | 447 |
| 457 // Create another child of the first tab. | 448 // Create another child of the first tab. |
| 458 Tab* child_tab2 = | 449 Tab* child_tab2 = |
| 459 [tabModel_ insertTabWithWebState:CreateChildWebState(parent_tab) | 450 [tab_model_ insertTabWithWebState:CreateChildWebState(parent_tab) |
| 460 atIndex:[tabModel_ count]]; | 451 atIndex:[tab_model_ count]]; |
| 461 | 452 |
| 462 EXPECT_FALSE([tabModel_ openerOfTab:parent_tab]); | 453 EXPECT_FALSE([tab_model_ openerOfTab:parent_tab]); |
| 463 EXPECT_FALSE([tabModel_ openerOfTab:another_tab]); | 454 EXPECT_FALSE([tab_model_ openerOfTab:another_tab]); |
| 464 EXPECT_EQ(parent_tab, [tabModel_ openerOfTab:child_tab]); | 455 EXPECT_EQ(parent_tab, [tab_model_ openerOfTab:child_tab]); |
| 465 EXPECT_EQ(parent_tab, [tabModel_ openerOfTab:child_tab2]); | 456 EXPECT_EQ(parent_tab, [tab_model_ openerOfTab:child_tab2]); |
| 466 } | 457 } |
| 467 | 458 |
| 468 TEST_F(TabModelTest, OpenerOfTabEmptyModel) { | 459 TEST_F(TabModelTest, OpenerOfTabEmptyModel) { |
| 469 EXPECT_FALSE([tabModel_ openerOfTab:nil]); | 460 EXPECT_FALSE([tab_model_ openerOfTab:nil]); |
| 470 } | 461 } |
| 471 | 462 |
| 472 TEST_F(TabModelTest, OpenersEmptyModel) { | 463 TEST_F(TabModelTest, OpenersEmptyModel) { |
| 473 // Empty model. | 464 // Empty model. |
| 474 EXPECT_TRUE([tabModel_ isEmpty]); | 465 EXPECT_TRUE([tab_model_ isEmpty]); |
| 475 EXPECT_FALSE([tabModel_ nextTabWithOpener:nil afterTab:nil]); | 466 EXPECT_FALSE([tab_model_ nextTabWithOpener:nil afterTab:nil]); |
| 476 EXPECT_FALSE([tabModel_ lastTabWithOpener:nil]); | 467 EXPECT_FALSE([tab_model_ lastTabWithOpener:nil]); |
| 477 EXPECT_FALSE([tabModel_ firstTabWithOpener:nil]); | 468 EXPECT_FALSE([tab_model_ firstTabWithOpener:nil]); |
| 478 } | 469 } |
| 479 | 470 |
| 480 TEST_F(TabModelTest, OpenersNothingOpenedGeneral) { | 471 TEST_F(TabModelTest, OpenersNothingOpenedGeneral) { |
| 481 // Start with a few tabs. | 472 // Start with a few tabs. |
| 482 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 473 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 483 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 474 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 484 | 475 |
| 485 Tab* tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 476 Tab* tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 486 atIndex:[tabModel_ count]]; | 477 atIndex:[tab_model_ count]]; |
| 487 | 478 |
| 488 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 479 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 489 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 480 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 490 | 481 |
| 491 // All should fail since this hasn't opened anything else. | 482 // All should fail since this hasn't opened anything else. |
| 492 EXPECT_FALSE([tabModel_ nextTabWithOpener:tab afterTab:nil]); | 483 EXPECT_FALSE([tab_model_ nextTabWithOpener:tab afterTab:nil]); |
| 493 EXPECT_FALSE([tabModel_ lastTabWithOpener:tab]); | 484 EXPECT_FALSE([tab_model_ lastTabWithOpener:tab]); |
| 494 EXPECT_FALSE([tabModel_ firstTabWithOpener:tab]); | 485 EXPECT_FALSE([tab_model_ firstTabWithOpener:tab]); |
| 495 | 486 |
| 496 // Add more items to the tab, expect the same results. | 487 // Add more items to the tab, expect the same results. |
| 497 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 488 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 498 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 489 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 499 EXPECT_FALSE([tabModel_ nextTabWithOpener:tab afterTab:nil]); | 490 EXPECT_FALSE([tab_model_ nextTabWithOpener:tab afterTab:nil]); |
| 500 EXPECT_FALSE([tabModel_ lastTabWithOpener:tab]); | 491 EXPECT_FALSE([tab_model_ lastTabWithOpener:tab]); |
| 501 EXPECT_FALSE([tabModel_ firstTabWithOpener:tab]); | 492 EXPECT_FALSE([tab_model_ firstTabWithOpener:tab]); |
| 502 } | 493 } |
| 503 | 494 |
| 504 TEST_F(TabModelTest, OpenersNothingOpenedFirst) { | 495 TEST_F(TabModelTest, OpenersNothingOpenedFirst) { |
| 505 // Our tab is first. | 496 // Our tab is first. |
| 506 Tab* tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 497 Tab* tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 507 atIndex:[tabModel_ count]]; | 498 atIndex:[tab_model_ count]]; |
| 508 | 499 |
| 509 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 500 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 510 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 501 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 511 | 502 |
| 512 // All should fail since this hasn't opened anything else. | 503 // All should fail since this hasn't opened anything else. |
| 513 EXPECT_FALSE([tabModel_ nextTabWithOpener:tab afterTab:nil]); | 504 EXPECT_FALSE([tab_model_ nextTabWithOpener:tab afterTab:nil]); |
| 514 EXPECT_FALSE([tabModel_ lastTabWithOpener:tab]); | 505 EXPECT_FALSE([tab_model_ lastTabWithOpener:tab]); |
| 515 EXPECT_FALSE([tabModel_ firstTabWithOpener:tab]); | 506 EXPECT_FALSE([tab_model_ firstTabWithOpener:tab]); |
| 516 } | 507 } |
| 517 | 508 |
| 518 TEST_F(TabModelTest, OpenersNothingOpenedLast) { | 509 TEST_F(TabModelTest, OpenersNothingOpenedLast) { |
| 519 // Our tab is last. | 510 // Our tab is last. |
| 520 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 511 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 521 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 512 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 522 Tab* tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 513 Tab* tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 523 atIndex:[tabModel_ count]]; | 514 atIndex:[tab_model_ count]]; |
| 524 | 515 |
| 525 // All should fail since this hasn't opened anything else. | 516 // All should fail since this hasn't opened anything else. |
| 526 EXPECT_FALSE([tabModel_ nextTabWithOpener:tab afterTab:nil]); | 517 EXPECT_FALSE([tab_model_ nextTabWithOpener:tab afterTab:nil]); |
| 527 EXPECT_FALSE([tabModel_ lastTabWithOpener:tab]); | 518 EXPECT_FALSE([tab_model_ lastTabWithOpener:tab]); |
| 528 EXPECT_FALSE([tabModel_ firstTabWithOpener:tab]); | 519 EXPECT_FALSE([tab_model_ firstTabWithOpener:tab]); |
| 529 } | 520 } |
| 530 | 521 |
| 531 TEST_F(TabModelTest, OpenersChildTabBeforeOpener) { | 522 TEST_F(TabModelTest, OpenersChildTabBeforeOpener) { |
| 532 Tab* parent_tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 523 Tab* parent_tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 533 atIndex:[tabModel_ count]]; | 524 atIndex:[tab_model_ count]]; |
| 534 // Insert child at start | 525 // Insert child at start |
| 535 Tab* child_tab = | 526 Tab* child_tab = |
| 536 [tabModel_ insertTabWithWebState:CreateChildWebState(parent_tab) | 527 [tab_model_ insertTabWithWebState:CreateChildWebState(parent_tab) |
| 537 atIndex:0]; | 528 atIndex:0]; |
| 538 | 529 |
| 539 // Insert a few more between them. | 530 // Insert a few more between them. |
| 540 [tabModel_ insertTabWithWebState:CreateWebState(@"window") atIndex:1]; | 531 [tab_model_ insertTabWithWebState:CreateWebState(@"window") atIndex:1]; |
| 541 [tabModel_ insertTabWithWebState:CreateWebState(@"window") atIndex:1]; | 532 [tab_model_ insertTabWithWebState:CreateWebState(@"window") atIndex:1]; |
| 542 | 533 |
| 543 EXPECT_FALSE([tabModel_ nextTabWithOpener:parent_tab afterTab:nil]); | 534 EXPECT_FALSE([tab_model_ nextTabWithOpener:parent_tab afterTab:nil]); |
| 544 EXPECT_FALSE([tabModel_ lastTabWithOpener:parent_tab]); | 535 EXPECT_FALSE([tab_model_ lastTabWithOpener:parent_tab]); |
| 545 EXPECT_EQ([tabModel_ firstTabWithOpener:parent_tab], child_tab); | 536 EXPECT_EQ([tab_model_ firstTabWithOpener:parent_tab], child_tab); |
| 546 } | 537 } |
| 547 | 538 |
| 548 TEST_F(TabModelTest, OpenersChildTabAfterOpener) { | 539 TEST_F(TabModelTest, OpenersChildTabAfterOpener) { |
| 549 Tab* parent_tab = [tabModel_ insertTabWithWebState:CreateWebState(@"window") | 540 Tab* parent_tab = [tab_model_ insertTabWithWebState:CreateWebState(@"window") |
| 550 atIndex:[tabModel_ count]]; | 541 atIndex:[tab_model_ count]]; |
| 551 | 542 |
| 552 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 543 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 553 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 544 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 554 // Insert two children at end. | 545 // Insert two children at end. |
| 555 Tab* child_tab1 = | 546 Tab* child_tab1 = |
| 556 [tabModel_ insertTabWithWebState:CreateChildWebState(parent_tab) | 547 [tab_model_ insertTabWithWebState:CreateChildWebState(parent_tab) |
| 557 atIndex:[tabModel_ count]]; | 548 atIndex:[tab_model_ count]]; |
| 558 Tab* child_tab2 = | 549 Tab* child_tab2 = |
| 559 [tabModel_ insertTabWithWebState:CreateChildWebState(parent_tab) | 550 [tab_model_ insertTabWithWebState:CreateChildWebState(parent_tab) |
| 560 atIndex:[tabModel_ count]]; | 551 atIndex:[tab_model_ count]]; |
| 561 | 552 |
| 562 EXPECT_EQ([tabModel_ nextTabWithOpener:parent_tab afterTab:nil], child_tab1); | 553 EXPECT_EQ([tab_model_ nextTabWithOpener:parent_tab afterTab:nil], child_tab1); |
| 563 EXPECT_EQ([tabModel_ nextTabWithOpener:parent_tab afterTab:child_tab1], | 554 EXPECT_EQ([tab_model_ nextTabWithOpener:parent_tab afterTab:child_tab1], |
| 564 child_tab2); | 555 child_tab2); |
| 565 EXPECT_EQ([tabModel_ lastTabWithOpener:parent_tab], child_tab2); | 556 EXPECT_EQ([tab_model_ lastTabWithOpener:parent_tab], child_tab2); |
| 566 EXPECT_FALSE([tabModel_ firstTabWithOpener:parent_tab]); | 557 EXPECT_FALSE([tab_model_ firstTabWithOpener:parent_tab]); |
| 567 } | 558 } |
| 568 | 559 |
| 569 TEST_F(TabModelTest, AddWithOrderController) { | 560 TEST_F(TabModelTest, AddWithOrderController) { |
| 570 // Create a few tabs with the controller at the front. | 561 // Create a few tabs with the controller at the front. |
| 571 Tab* parent = | 562 Tab* parent = |
| 572 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 563 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 573 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 564 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 574 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 565 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 575 | 566 |
| 576 // Add a new tab, it should be added behind the parent. | 567 // Add a new tab, it should be added behind the parent. |
| 577 Tab* child = [tabModel_ | 568 Tab* child = [tab_model_ |
| 578 insertOrUpdateTabWithURL:kURL | 569 insertOrUpdateTabWithURL:kURL |
| 579 referrer:kEmptyReferrer | 570 referrer:kEmptyReferrer |
| 580 transition:ui::PAGE_TRANSITION_LINK | 571 transition:ui::PAGE_TRANSITION_LINK |
| 581 windowName:nil | 572 windowName:nil |
| 582 opener:parent | 573 opener:parent |
| 583 openedByDOM:NO | 574 openedByDOM:NO |
| 584 atIndex:TabModelConstants::kTabPositionAutomatically | 575 atIndex:TabModelConstants::kTabPositionAutomatically |
| 585 inBackground:NO]; | 576 inBackground:NO]; |
| 586 EXPECT_EQ([tabModel_ indexOfTab:parent], 0U); | 577 EXPECT_EQ([tab_model_ indexOfTab:parent], 0U); |
| 587 EXPECT_EQ([tabModel_ indexOfTab:child], 1U); | 578 EXPECT_EQ([tab_model_ indexOfTab:child], 1U); |
| 588 | 579 |
| 589 // Add another new tab without a parent, should go at the end. | 580 // Add another new tab without a parent, should go at the end. |
| 590 Tab* tab = [tabModel_ | 581 Tab* tab = [tab_model_ |
| 591 insertOrUpdateTabWithURL:kURL | 582 insertOrUpdateTabWithURL:kURL |
| 592 referrer:kEmptyReferrer | 583 referrer:kEmptyReferrer |
| 593 transition:ui::PAGE_TRANSITION_LINK | 584 transition:ui::PAGE_TRANSITION_LINK |
| 594 windowName:nil | 585 windowName:nil |
| 595 opener:nil | 586 opener:nil |
| 596 openedByDOM:NO | 587 openedByDOM:NO |
| 597 atIndex:TabModelConstants::kTabPositionAutomatically | 588 atIndex:TabModelConstants::kTabPositionAutomatically |
| 598 inBackground:NO]; | 589 inBackground:NO]; |
| 599 EXPECT_EQ([tabModel_ indexOfTab:tab], [tabModel_ count] - 1); | 590 EXPECT_EQ([tab_model_ indexOfTab:tab], [tab_model_ count] - 1); |
| 600 | 591 |
| 601 // Same for a tab that's not opened via a LINK transition. | 592 // Same for a tab that's not opened via a LINK transition. |
| 602 Tab* tab2 = [tabModel_ insertOrUpdateTabWithURL:kURL | 593 Tab* tab2 = [tab_model_ insertOrUpdateTabWithURL:kURL |
| 603 referrer:kEmptyReferrer | 594 referrer:kEmptyReferrer |
| 604 transition:ui::PAGE_TRANSITION_TYPED | 595 transition:ui::PAGE_TRANSITION_TYPED |
| 605 windowName:nil | 596 windowName:nil |
| 606 opener:nil | 597 opener:nil |
| 607 openedByDOM:NO | 598 openedByDOM:NO |
| 608 atIndex:[tabModel_ count] | 599 atIndex:[tab_model_ count] |
| 609 inBackground:NO]; | 600 inBackground:NO]; |
| 610 EXPECT_EQ([tabModel_ indexOfTab:tab2], [tabModel_ count] - 1); | 601 EXPECT_EQ([tab_model_ indexOfTab:tab2], [tab_model_ count] - 1); |
| 611 | 602 |
| 612 // Add a tab in the background. It should appear behind the opening tab. | 603 // Add a tab in the background. It should appear behind the opening tab. |
| 613 Tab* tab3 = [tabModel_ | 604 Tab* tab3 = [tab_model_ |
| 614 insertOrUpdateTabWithURL:kURL | 605 insertOrUpdateTabWithURL:kURL |
| 615 referrer:kEmptyReferrer | 606 referrer:kEmptyReferrer |
| 616 transition:ui::PAGE_TRANSITION_LINK | 607 transition:ui::PAGE_TRANSITION_LINK |
| 617 windowName:nil | 608 windowName:nil |
| 618 opener:tab | 609 opener:tab |
| 619 openedByDOM:NO | 610 openedByDOM:NO |
| 620 atIndex:TabModelConstants::kTabPositionAutomatically | 611 atIndex:TabModelConstants::kTabPositionAutomatically |
| 621 inBackground:YES]; | 612 inBackground:YES]; |
| 622 EXPECT_EQ([tabModel_ indexOfTab:tab3], [tabModel_ indexOfTab:tab] + 1); | 613 EXPECT_EQ([tab_model_ indexOfTab:tab3], [tab_model_ indexOfTab:tab] + 1); |
| 623 | 614 |
| 624 // Add another background tab behind the one we just opened. | 615 // Add another background tab behind the one we just opened. |
| 625 Tab* tab4 = [tabModel_ | 616 Tab* tab4 = [tab_model_ |
| 626 insertOrUpdateTabWithURL:kURL | 617 insertOrUpdateTabWithURL:kURL |
| 627 referrer:kEmptyReferrer | 618 referrer:kEmptyReferrer |
| 628 transition:ui::PAGE_TRANSITION_LINK | 619 transition:ui::PAGE_TRANSITION_LINK |
| 629 windowName:nil | 620 windowName:nil |
| 630 opener:tab3 | 621 opener:tab3 |
| 631 openedByDOM:NO | 622 openedByDOM:NO |
| 632 atIndex:TabModelConstants::kTabPositionAutomatically | 623 atIndex:TabModelConstants::kTabPositionAutomatically |
| 633 inBackground:YES]; | 624 inBackground:YES]; |
| 634 EXPECT_EQ([tabModel_ indexOfTab:tab4], [tabModel_ indexOfTab:tab3] + 1); | 625 EXPECT_EQ([tab_model_ indexOfTab:tab4], [tab_model_ indexOfTab:tab3] + 1); |
| 635 } | 626 } |
| 636 | 627 |
| 637 TEST_F(TabModelTest, AddWithOrderControllerAndGrouping) { | 628 TEST_F(TabModelTest, AddWithOrderControllerAndGrouping) { |
| 638 // Create a few tabs with the controller at the front. | 629 // Create a few tabs with the controller at the front. |
| 639 Tab* parent = | 630 Tab* parent = |
| 640 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 631 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 641 // Force the history to update, as it is used to determine grouping. | 632 // Force the history to update, as it is used to determine grouping. |
| 642 ASSERT_TRUE([parent navigationManager]); | 633 ASSERT_TRUE([parent navigationManager]); |
| 643 [[parent navigationManager]->GetSessionController() commitPendingEntry]; | 634 [[parent navigationManager]->GetSessionController() commitPendingEntry]; |
| 644 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 635 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 645 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 636 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 646 | 637 |
| 647 ASSERT_TRUE(chrome_browser_state_->CreateHistoryService(true)); | 638 ASSERT_TRUE(chrome_browser_state_->CreateHistoryService(true)); |
| 648 | 639 |
| 649 // Add a new tab, it should be added behind the parent. | 640 // Add a new tab, it should be added behind the parent. |
| 650 Tab* child1 = [tabModel_ | 641 Tab* child1 = [tab_model_ |
| 651 insertOrUpdateTabWithURL:kURL | 642 insertOrUpdateTabWithURL:kURL |
| 652 referrer:kEmptyReferrer | 643 referrer:kEmptyReferrer |
| 653 transition:ui::PAGE_TRANSITION_LINK | 644 transition:ui::PAGE_TRANSITION_LINK |
| 654 windowName:nil | 645 windowName:nil |
| 655 opener:parent | 646 opener:parent |
| 656 openedByDOM:NO | 647 openedByDOM:NO |
| 657 atIndex:TabModelConstants::kTabPositionAutomatically | 648 atIndex:TabModelConstants::kTabPositionAutomatically |
| 658 inBackground:NO]; | 649 inBackground:NO]; |
| 659 EXPECT_EQ([tabModel_ indexOfTab:parent], 0U); | 650 EXPECT_EQ([tab_model_ indexOfTab:parent], 0U); |
| 660 EXPECT_EQ([tabModel_ indexOfTab:child1], 1U); | 651 EXPECT_EQ([tab_model_ indexOfTab:child1], 1U); |
| 661 | 652 |
| 662 // Add a second child tab in the background. It should be added behind the | 653 // Add a second child tab in the background. It should be added behind the |
| 663 // first child. | 654 // first child. |
| 664 Tab* child2 = [tabModel_ | 655 Tab* child2 = [tab_model_ |
| 665 insertOrUpdateTabWithURL:kURL | 656 insertOrUpdateTabWithURL:kURL |
| 666 referrer:kEmptyReferrer | 657 referrer:kEmptyReferrer |
| 667 transition:ui::PAGE_TRANSITION_LINK | 658 transition:ui::PAGE_TRANSITION_LINK |
| 668 windowName:nil | 659 windowName:nil |
| 669 opener:parent | 660 opener:parent |
| 670 openedByDOM:NO | 661 openedByDOM:NO |
| 671 atIndex:TabModelConstants::kTabPositionAutomatically | 662 atIndex:TabModelConstants::kTabPositionAutomatically |
| 672 inBackground:YES]; | 663 inBackground:YES]; |
| 673 EXPECT_EQ([tabModel_ indexOfTab:child2], 2U); | 664 EXPECT_EQ([tab_model_ indexOfTab:child2], 2U); |
| 674 | 665 |
| 675 // Navigate the parent tab to a new URL. It should not change any ordering. | 666 // Navigate the parent tab to a new URL. It should not change any ordering. |
| 676 web::NavigationManager::WebLoadParams parent_params( | 667 web::NavigationManager::WebLoadParams parent_params( |
| 677 GURL("http://www.espn.com")); | 668 GURL("http://www.espn.com")); |
| 678 parent_params.transition_type = ui::PAGE_TRANSITION_TYPED; | 669 parent_params.transition_type = ui::PAGE_TRANSITION_TYPED; |
| 679 [[parent webController] loadWithParams:parent_params]; | 670 [[parent webController] loadWithParams:parent_params]; |
| 680 ASSERT_TRUE([parent navigationManager]); | 671 ASSERT_TRUE([parent navigationManager]); |
| 681 [[parent navigationManager]->GetSessionController() commitPendingEntry]; | 672 [[parent navigationManager]->GetSessionController() commitPendingEntry]; |
| 682 EXPECT_EQ([tabModel_ indexOfTab:parent], 0U); | 673 EXPECT_EQ([tab_model_ indexOfTab:parent], 0U); |
| 683 | 674 |
| 684 // Add a new tab. It should be added behind the parent. It should not be added | 675 // Add a new tab. It should be added behind the parent. It should not be added |
| 685 // after the previous two children. | 676 // after the previous two children. |
| 686 Tab* child3 = [tabModel_ | 677 Tab* child3 = [tab_model_ |
| 687 insertOrUpdateTabWithURL:kURL | 678 insertOrUpdateTabWithURL:kURL |
| 688 referrer:kEmptyReferrer | 679 referrer:kEmptyReferrer |
| 689 transition:ui::PAGE_TRANSITION_LINK | 680 transition:ui::PAGE_TRANSITION_LINK |
| 690 windowName:nil | 681 windowName:nil |
| 691 opener:parent | 682 opener:parent |
| 692 openedByDOM:NO | 683 openedByDOM:NO |
| 693 atIndex:TabModelConstants::kTabPositionAutomatically | 684 atIndex:TabModelConstants::kTabPositionAutomatically |
| 694 inBackground:NO]; | 685 inBackground:NO]; |
| 695 EXPECT_EQ([tabModel_ indexOfTab:child3], 1U); | 686 EXPECT_EQ([tab_model_ indexOfTab:child3], 1U); |
| 696 | 687 |
| 697 // Add a fourt child tab in the background. It should be added behind the | 688 // Add a fourt child tab in the background. It should be added behind the |
| 698 // third child. | 689 // third child. |
| 699 Tab* child4 = [tabModel_ | 690 Tab* child4 = [tab_model_ |
| 700 insertOrUpdateTabWithURL:kURL | 691 insertOrUpdateTabWithURL:kURL |
| 701 referrer:kEmptyReferrer | 692 referrer:kEmptyReferrer |
| 702 transition:ui::PAGE_TRANSITION_LINK | 693 transition:ui::PAGE_TRANSITION_LINK |
| 703 windowName:nil | 694 windowName:nil |
| 704 opener:parent | 695 opener:parent |
| 705 openedByDOM:NO | 696 openedByDOM:NO |
| 706 atIndex:TabModelConstants::kTabPositionAutomatically | 697 atIndex:TabModelConstants::kTabPositionAutomatically |
| 707 inBackground:YES]; | 698 inBackground:YES]; |
| 708 EXPECT_EQ([tabModel_ indexOfTab:child4], 2U); | 699 EXPECT_EQ([tab_model_ indexOfTab:child4], 2U); |
| 709 | 700 |
| 710 // The first two children should have been moved to the right. | 701 // The first two children should have been moved to the right. |
| 711 EXPECT_EQ([tabModel_ indexOfTab:child1], 3U); | 702 EXPECT_EQ([tab_model_ indexOfTab:child1], 3U); |
| 712 EXPECT_EQ([tabModel_ indexOfTab:child2], 4U); | 703 EXPECT_EQ([tab_model_ indexOfTab:child2], 4U); |
| 713 | 704 |
| 714 // Now add a non-owned tab and make sure it is added at the end. | 705 // Now add a non-owned tab and make sure it is added at the end. |
| 715 Tab* nonChild = | 706 Tab* nonChild = |
| 716 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 707 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 717 EXPECT_EQ([tabModel_ indexOfTab:nonChild], [tabModel_ count] - 1); | 708 EXPECT_EQ([tab_model_ indexOfTab:nonChild], [tab_model_ count] - 1); |
| 718 } | 709 } |
| 719 | 710 |
| 720 TEST_F(TabModelTest, AddWithLinkTransitionAndIndex) { | 711 TEST_F(TabModelTest, AddWithLinkTransitionAndIndex) { |
| 721 // Create a few tabs with the controller at the front. | 712 // Create a few tabs with the controller at the front. |
| 722 Tab* parent = | 713 Tab* parent = |
| 723 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 714 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 724 // Force the history to update, as it is used to determine grouping. | 715 // Force the history to update, as it is used to determine grouping. |
| 725 ASSERT_TRUE([parent navigationManager]); | 716 ASSERT_TRUE([parent navigationManager]); |
| 726 [[parent navigationManager]->GetSessionController() commitPendingEntry]; | 717 [[parent navigationManager]->GetSessionController() commitPendingEntry]; |
| 727 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 718 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 728 [tabModel_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; | 719 [tab_model_ addTabWithURL:kURL referrer:kEmptyReferrer windowName:nil]; |
| 729 | 720 |
| 730 ASSERT_TRUE(chrome_browser_state_->CreateHistoryService(true)); | 721 ASSERT_TRUE(chrome_browser_state_->CreateHistoryService(true)); |
| 731 | 722 |
| 732 // Add a new tab, it should be added before the parent since the index | 723 // Add a new tab, it should be added before the parent since the index |
| 733 // parameter has been specified with a valid value. | 724 // parameter has been specified with a valid value. |
| 734 Tab* child1 = [tabModel_ insertOrUpdateTabWithURL:kURL | 725 Tab* child1 = [tab_model_ insertOrUpdateTabWithURL:kURL |
| 735 referrer:kEmptyReferrer | 726 referrer:kEmptyReferrer |
| 736 transition:ui::PAGE_TRANSITION_LINK | 727 transition:ui::PAGE_TRANSITION_LINK |
| 737 windowName:nil | 728 windowName:nil |
| 738 opener:parent | 729 opener:parent |
| 739 openedByDOM:NO | 730 openedByDOM:NO |
| 740 atIndex:0 | 731 atIndex:0 |
| 741 inBackground:NO]; | 732 inBackground:NO]; |
| 742 EXPECT_EQ([tabModel_ indexOfTab:parent], 1U); | 733 EXPECT_EQ([tab_model_ indexOfTab:parent], 1U); |
| 743 EXPECT_EQ([tabModel_ indexOfTab:child1], 0U); | 734 EXPECT_EQ([tab_model_ indexOfTab:child1], 0U); |
| 744 | 735 |
| 745 // Add a new tab, it should be added at the beginning of the stack because | 736 // Add a new tab, it should be added at the beginning of the stack because |
| 746 // the index parameter has been specified with a valid value. | 737 // the index parameter has been specified with a valid value. |
| 747 Tab* child2 = [tabModel_ insertOrUpdateTabWithURL:kURL | 738 Tab* child2 = [tab_model_ insertOrUpdateTabWithURL:kURL |
| 748 referrer:kEmptyReferrer | 739 referrer:kEmptyReferrer |
| 749 transition:ui::PAGE_TRANSITION_LINK | 740 transition:ui::PAGE_TRANSITION_LINK |
| 750 windowName:nil | 741 windowName:nil |
| 751 opener:parent | 742 opener:parent |
| 752 openedByDOM:NO | 743 openedByDOM:NO |
| 753 atIndex:0 | 744 atIndex:0 |
| 754 inBackground:NO]; | 745 inBackground:NO]; |
| 755 EXPECT_EQ([tabModel_ indexOfTab:parent], 2U); | 746 EXPECT_EQ([tab_model_ indexOfTab:parent], 2U); |
| 756 EXPECT_EQ([tabModel_ indexOfTab:child1], 1U); | 747 EXPECT_EQ([tab_model_ indexOfTab:child1], 1U); |
| 757 EXPECT_EQ([tabModel_ indexOfTab:child2], 0U); | 748 EXPECT_EQ([tab_model_ indexOfTab:child2], 0U); |
| 758 | 749 |
| 759 // Add a new tab, it should be added at position 1 because the index parameter | 750 // Add a new tab, it should be added at position 1 because the index parameter |
| 760 // has been specified with a valid value. | 751 // has been specified with a valid value. |
| 761 Tab* child3 = [tabModel_ insertOrUpdateTabWithURL:kURL | 752 Tab* child3 = [tab_model_ insertOrUpdateTabWithURL:kURL |
| 762 referrer:kEmptyReferrer | 753 referrer:kEmptyReferrer |
| 763 transition:ui::PAGE_TRANSITION_LINK | 754 transition:ui::PAGE_TRANSITION_LINK |
| 764 windowName:nil | 755 windowName:nil |
| 765 opener:parent | 756 opener:parent |
| 766 openedByDOM:NO | 757 openedByDOM:NO |
| 767 atIndex:1 | 758 atIndex:1 |
| 768 inBackground:NO]; | 759 inBackground:NO]; |
| 769 EXPECT_EQ([tabModel_ indexOfTab:parent], 3U); | 760 EXPECT_EQ([tab_model_ indexOfTab:parent], 3U); |
| 770 EXPECT_EQ([tabModel_ indexOfTab:child1], 2U); | 761 EXPECT_EQ([tab_model_ indexOfTab:child1], 2U); |
| 771 EXPECT_EQ([tabModel_ indexOfTab:child3], 1U); | 762 EXPECT_EQ([tab_model_ indexOfTab:child3], 1U); |
| 772 EXPECT_EQ([tabModel_ indexOfTab:child2], 0U); | 763 EXPECT_EQ([tab_model_ indexOfTab:child2], 0U); |
| 773 } | 764 } |
| 774 | 765 |
| 775 TEST_F(TabModelTest, MoveTabs) { | 766 TEST_F(TabModelTest, MoveTabs) { |
| 776 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 767 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 777 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 768 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 778 [tabModel_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 769 [tab_model_ addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 779 | 770 |
| 780 // Basic sanity checks before moving on. | 771 // Basic sanity checks before moving on. |
| 781 ASSERT_EQ(3U, [tabModel_ count]); | 772 ASSERT_EQ(3U, [tab_model_ count]); |
| 782 ASSERT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:0] windowName]); | 773 ASSERT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:0] windowName]); |
| 783 ASSERT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:1] windowName]); | 774 ASSERT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:1] windowName]); |
| 784 ASSERT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:2] windowName]); | 775 ASSERT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:2] windowName]); |
| 785 | 776 |
| 786 // Move a tab from index 1 to index 0 (move tab left by one). | 777 // Move a tab from index 1 to index 0 (move tab left by one). |
| 787 [tabModelObserver_ setTabMovedWasCalled:NO]; | 778 [tab_model_observer_ setTabMovedWasCalled:NO]; |
| 788 [tabModel_ moveTab:[tabModel_ tabAtIndex:1] toIndex:0]; | 779 [tab_model_ moveTab:[tab_model_ tabAtIndex:1] toIndex:0]; |
| 789 ASSERT_EQ(3U, [tabModel_ count]); | 780 ASSERT_EQ(3U, [tab_model_ count]); |
| 790 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 781 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 791 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:1] windowName]); | 782 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:1] windowName]); |
| 792 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:2] windowName]); | 783 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:2] windowName]); |
| 793 EXPECT_TRUE([tabModelObserver_ tabMovedWasCalled]); | 784 EXPECT_TRUE([tab_model_observer_ tabMovedWasCalled]); |
| 794 | 785 |
| 795 // Move a tab from index 1 to index 2 (move tab right by one). | 786 // Move a tab from index 1 to index 2 (move tab right by one). |
| 796 [tabModelObserver_ setTabMovedWasCalled:NO]; | 787 [tab_model_observer_ setTabMovedWasCalled:NO]; |
| 797 [tabModel_ moveTab:[tabModel_ tabAtIndex:1] toIndex:2]; | 788 [tab_model_ moveTab:[tab_model_ tabAtIndex:1] toIndex:2]; |
| 798 ASSERT_EQ(3U, [tabModel_ count]); | 789 ASSERT_EQ(3U, [tab_model_ count]); |
| 799 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 790 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 800 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 791 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 801 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:2] windowName]); | 792 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:2] windowName]); |
| 802 EXPECT_TRUE([tabModelObserver_ tabMovedWasCalled]); | 793 EXPECT_TRUE([tab_model_observer_ tabMovedWasCalled]); |
| 803 | 794 |
| 804 // Move a tab from index 0 to index 2 (move tab right by more than one). | 795 // Move a tab from index 0 to index 2 (move tab right by more than one). |
| 805 [tabModelObserver_ setTabMovedWasCalled:NO]; | 796 [tab_model_observer_ setTabMovedWasCalled:NO]; |
| 806 [tabModel_ moveTab:[tabModel_ tabAtIndex:0] toIndex:2]; | 797 [tab_model_ moveTab:[tab_model_ tabAtIndex:0] toIndex:2]; |
| 807 ASSERT_EQ(3U, [tabModel_ count]); | 798 ASSERT_EQ(3U, [tab_model_ count]); |
| 808 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:0] windowName]); | 799 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:0] windowName]); |
| 809 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:1] windowName]); | 800 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:1] windowName]); |
| 810 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:2] windowName]); | 801 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:2] windowName]); |
| 811 EXPECT_TRUE([tabModelObserver_ tabMovedWasCalled]); | 802 EXPECT_TRUE([tab_model_observer_ tabMovedWasCalled]); |
| 812 | 803 |
| 813 // Move a tab from index 2 to index 0 (move tab left by more than one). | 804 // Move a tab from index 2 to index 0 (move tab left by more than one). |
| 814 [tabModelObserver_ setTabMovedWasCalled:NO]; | 805 [tab_model_observer_ setTabMovedWasCalled:NO]; |
| 815 [tabModel_ moveTab:[tabModel_ tabAtIndex:2] toIndex:0]; | 806 [tab_model_ moveTab:[tab_model_ tabAtIndex:2] toIndex:0]; |
| 816 ASSERT_EQ(3U, [tabModel_ count]); | 807 ASSERT_EQ(3U, [tab_model_ count]); |
| 817 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 808 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 818 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 809 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 819 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:2] windowName]); | 810 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:2] windowName]); |
| 820 EXPECT_TRUE([tabModelObserver_ tabMovedWasCalled]); | 811 EXPECT_TRUE([tab_model_observer_ tabMovedWasCalled]); |
| 821 | 812 |
| 822 // Move a tab from index 2 to index 2 (move tab to the same index). | 813 // Move a tab from index 2 to index 2 (move tab to the same index). |
| 823 [tabModelObserver_ setTabMovedWasCalled:NO]; | 814 [tab_model_observer_ setTabMovedWasCalled:NO]; |
| 824 [tabModel_ moveTab:[tabModel_ tabAtIndex:2] toIndex:2]; | 815 [tab_model_ moveTab:[tab_model_ tabAtIndex:2] toIndex:2]; |
| 825 ASSERT_EQ(3U, [tabModel_ count]); | 816 ASSERT_EQ(3U, [tab_model_ count]); |
| 826 EXPECT_NSEQ(@"window 2", [[tabModel_ tabAtIndex:0] windowName]); | 817 EXPECT_NSEQ(@"window 2", [[tab_model_ tabAtIndex:0] windowName]); |
| 827 EXPECT_NSEQ(@"window 3", [[tabModel_ tabAtIndex:1] windowName]); | 818 EXPECT_NSEQ(@"window 3", [[tab_model_ tabAtIndex:1] windowName]); |
| 828 EXPECT_NSEQ(@"window 1", [[tabModel_ tabAtIndex:2] windowName]); | 819 EXPECT_NSEQ(@"window 1", [[tab_model_ tabAtIndex:2] windowName]); |
| 829 EXPECT_FALSE([tabModelObserver_ tabMovedWasCalled]); | 820 EXPECT_FALSE([tab_model_observer_ tabMovedWasCalled]); |
| 830 } | 821 } |
| 831 | 822 |
| 832 TEST_F(TabModelTest, SetParentModel) { | 823 TEST_F(TabModelTest, SetParentModel) { |
| 833 // Create a tab without a parent model and make sure it doesn't crash. Then | 824 // Create a tab without a parent model and make sure it doesn't crash. Then |
| 834 // set its parent TabModel and make sure that works as well. | 825 // set its parent TabModel and make sure that works as well. |
| 835 base::scoped_nsobject<TabTest> tab([[TabTest alloc] | 826 base::scoped_nsobject<TabTest> tab([[TabTest alloc] |
| 836 initWithWindowName:@"parentless" | 827 initWithWindowName:@"parentless" |
| 837 lastVisitedTimestamp:100 | 828 lastVisitedTimestamp:100 |
| 838 browserState:chrome_browser_state_.get() | 829 browserState:chrome_browser_state_.get() |
| 839 tabModel:nil]); | 830 tabModel:nil]); |
| 840 EXPECT_TRUE([tab parentTabModel] == nil); | 831 EXPECT_TRUE([tab parentTabModel] == nil); |
| 841 [tabModel_ insertTab:tab atIndex:0]; | 832 [tab_model_ insertTab:tab atIndex:0]; |
| 842 [tab setParentTabModel:tabModel_.get()]; | 833 [tab setParentTabModel:tab_model_.get()]; |
| 843 EXPECT_FALSE([tab parentTabModel] == nil); | 834 EXPECT_FALSE([tab parentTabModel] == nil); |
| 844 [tabModel_ closeTabAtIndex:0]; | 835 [tab_model_ closeTabAtIndex:0]; |
| 845 } | 836 } |
| 846 | 837 |
| 847 TEST_F(TabModelTest, PersistSelectionChange) { | 838 TEST_F(TabModelTest, PersistSelectionChange) { |
| 839 TestChromeBrowserState::Builder test_cbs_builder; |
| 840 auto chrome_browser_state = test_cbs_builder.Build(); |
| 841 |
| 848 NSString* stashPath = | 842 NSString* stashPath = |
| 849 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); | 843 base::SysUTF8ToNSString(chrome_browser_state->GetStatePath().value()); |
| 850 | 844 |
| 851 base::scoped_nsobject<TabModel> model([[TabModel alloc] | 845 base::scoped_nsobject<TabModel> model([[TabModel alloc] |
| 852 initWithSessionWindow:sessionWindow_.get() | 846 initWithSessionWindow:session_window_.get() |
| 853 sessionService:[SessionServiceIOS sharedService] | 847 sessionService:[SessionServiceIOS sharedService] |
| 854 browserState:chrome_browser_state_.get()]); | 848 browserState:chrome_browser_state.get()]); |
| 855 | 849 |
| 856 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 850 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 857 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 851 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 858 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 852 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 859 | 853 |
| 860 ASSERT_EQ(3U, [model count]); | 854 ASSERT_EQ(3U, [model count]); |
| 861 model.get().currentTab = [model tabAtIndex:1]; | 855 model.get().currentTab = [model tabAtIndex:1]; |
| 862 // Force state to flush to disk on the main thread so it can be immediately | 856 // Force state to flush to disk on the main thread so it can be immediately |
| 863 // tested below. | 857 // tested below. |
| 864 SessionWindowIOS* window = [model windowForSavingSession]; | 858 SessionWindowIOS* window = [model windowForSavingSession]; |
| 865 [[SessionServiceIOS sharedService] performSaveWindow:window | 859 [[SessionServiceIOS sharedService] performSaveWindow:window |
| 866 toDirectory:stashPath]; | 860 toDirectory:stashPath]; |
| 867 [model browserStateDestroyed]; | 861 [model browserStateDestroyed]; |
| 868 model.reset(); | 862 model.reset(); |
| 869 base::RunLoop().RunUntilIdle(); | 863 base::RunLoop().RunUntilIdle(); |
| 870 | 864 |
| 871 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] | 865 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] |
| 872 loadWindowForBrowserState:chrome_browser_state_.get()]; | 866 loadWindowForBrowserState:chrome_browser_state.get()]; |
| 873 | 867 |
| 874 // Create tab model from saved session. | 868 // Create tab model from saved session. |
| 875 base::scoped_nsobject<TestSessionService> test_service( | 869 base::scoped_nsobject<TestSessionService> test_service( |
| 876 [[TestSessionService alloc] init]); | 870 [[TestSessionService alloc] init]); |
| 877 | 871 |
| 878 model.reset([[TabModel alloc] | 872 model.reset([[TabModel alloc] |
| 879 initWithSessionWindow:sessionWindow | 873 initWithSessionWindow:sessionWindow |
| 880 sessionService:test_service | 874 sessionService:test_service |
| 881 browserState:chrome_browser_state_.get()]); | 875 browserState:chrome_browser_state.get()]); |
| 882 EXPECT_EQ(model.get().currentTab, [model tabAtIndex:1]); | 876 EXPECT_EQ(model.get().currentTab, [model tabAtIndex:1]); |
| 883 [model browserStateDestroyed]; | 877 [model browserStateDestroyed]; |
| 884 | 878 |
| 885 // Clean up. | 879 // Clean up. |
| 886 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath | 880 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath |
| 887 error:nullptr]); | 881 error:nullptr]); |
| 888 } | 882 } |
| 889 | 883 |
| 890 } // anonymous namespace | 884 } // anonymous namespace |
| OLD | NEW |