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