| 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/files/file_path.h" |
| 8 #include "base/mac/scoped_nsautorelease_pool.h" | 8 #include "base/mac/scoped_nsautorelease_pool.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 TabModelTest() | 77 TabModelTest() |
| 78 : scoped_browser_state_manager_( | 78 : scoped_browser_state_manager_( |
| 79 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), | 79 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), |
| 80 web_client_(base::MakeUnique<ChromeWebClient>()) { | 80 web_client_(base::MakeUnique<ChromeWebClient>()) { |
| 81 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 81 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 82 | 82 |
| 83 TestChromeBrowserState::Builder test_cbs_builder; | 83 TestChromeBrowserState::Builder test_cbs_builder; |
| 84 chrome_browser_state_ = test_cbs_builder.Build(); | 84 chrome_browser_state_ = test_cbs_builder.Build(); |
| 85 | 85 |
| 86 session_window_.reset([[SessionWindowIOS alloc] init]); | 86 session_window_.reset([[SessionWindowIOS alloc] init]); |
| 87 |
| 87 // Create tab model with just a dummy session service so the async state | 88 // Create tab model with just a dummy session service so the async state |
| 88 // saving doesn't trigger unless actually wanted. | 89 // saving doesn't trigger unless actually wanted. |
| 89 base::scoped_nsobject<TestSessionService> test_service( | 90 SetTabModel( |
| 90 [[TestSessionService alloc] init]); | 91 CreateTabModel([[[TestSessionService alloc] init] autorelease], nil)); |
| 91 tab_model_.reset([[TabModel alloc] | |
| 92 initWithSessionWindow:session_window_.get() | |
| 93 sessionService:test_service | |
| 94 browserState:chrome_browser_state_.get()]); | |
| 95 [tab_model_ setWebUsageEnabled:NO]; | |
| 96 [tab_model_ setPrimary:YES]; | |
| 97 } | 92 } |
| 98 | 93 |
| 99 ~TabModelTest() override { | 94 ~TabModelTest() override { |
| 100 [tab_model_ browserStateDestroyed]; | 95 [tab_model_ browserStateDestroyed]; |
| 101 } | 96 } |
| 102 | 97 |
| 98 void SetTabModel(base::scoped_nsobject<TabModel> tab_model) { |
| 99 if (tab_model_) { |
| 100 @autoreleasepool { |
| 101 [tab_model_ browserStateDestroyed]; |
| 102 tab_model_.reset(); |
| 103 } |
| 104 } |
| 105 |
| 106 tab_model_ = tab_model; |
| 107 } |
| 108 |
| 109 base::scoped_nsobject<TabModel> CreateTabModel( |
| 110 SessionServiceIOS* session_service, |
| 111 SessionWindowIOS* session_window) { |
| 112 base::scoped_nsobject<TabModel> tab_model([[TabModel alloc] |
| 113 initWithSessionWindow:session_window |
| 114 sessionService:session_service |
| 115 browserState:chrome_browser_state_.get()]); |
| 116 [tab_model setWebUsageEnabled:NO]; |
| 117 [tab_model setPrimary:YES]; |
| 118 return tab_model; |
| 119 } |
| 120 |
| 103 protected: | 121 protected: |
| 104 // Creates a session window with entries named "restored window 1", | 122 // Creates a session window with entries named "restored window 1", |
| 105 // "restored window 2" and "restored window 3" and the second entry | 123 // "restored window 2" and "restored window 3" and the second entry |
| 106 // marked as selected. | 124 // marked as selected. |
| 107 base::scoped_nsobject<SessionWindowIOS> CreateSessionWindow() { | 125 base::scoped_nsobject<SessionWindowIOS> CreateSessionWindow() { |
| 108 NSMutableArray<CRWSessionStorage*>* sessions = [NSMutableArray array]; | 126 NSMutableArray<CRWSessionStorage*>* sessions = [NSMutableArray array]; |
| 109 for (int i = 0; i < 3; i++) { | 127 for (int i = 0; i < 3; i++) { |
| 110 CRWSessionStorage* session_storage = | 128 CRWSessionStorage* session_storage = |
| 111 [[[CRWSessionStorage alloc] init] autorelease]; | 129 [[[CRWSessionStorage alloc] init] autorelease]; |
| 112 [sessions addObject:session_storage]; | 130 [sessions addObject:session_storage]; |
| (...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1084 | 1102 |
| 1085 web::WebState* web_state_ptr = web_state.get(); | 1103 web::WebState* web_state_ptr = web_state.get(); |
| 1086 [tab_model_ webStateList]->InsertWebState(0, std::move(web_state)); | 1104 [tab_model_ webStateList]->InsertWebState(0, std::move(web_state)); |
| 1087 EXPECT_NSNE(nil, LegacyTabHelper::GetTabForWebState(web_state_ptr)); | 1105 EXPECT_NSNE(nil, LegacyTabHelper::GetTabForWebState(web_state_ptr)); |
| 1088 } | 1106 } |
| 1089 | 1107 |
| 1090 TEST_F(TabModelTest, PersistSelectionChange) { | 1108 TEST_F(TabModelTest, PersistSelectionChange) { |
| 1091 NSString* stashPath = | 1109 NSString* stashPath = |
| 1092 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); | 1110 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); |
| 1093 | 1111 |
| 1112 // Reset the TabModel with a custom SessionServiceIOS (to control whether |
| 1113 // data is saved to disk). |
| 1114 base::scoped_nsobject<TestSessionService> test_session_service( |
| 1115 [[TestSessionService alloc] init]); |
| 1116 SetTabModel(CreateTabModel(test_session_service.get(), nil)); |
| 1117 |
| 1094 [tab_model_ insertTabWithURL:GURL(kURL1) | 1118 [tab_model_ insertTabWithURL:GURL(kURL1) |
| 1095 referrer:web::Referrer() | 1119 referrer:web::Referrer() |
| 1096 transition:ui::PAGE_TRANSITION_TYPED | 1120 transition:ui::PAGE_TRANSITION_TYPED |
| 1097 opener:nil | 1121 opener:nil |
| 1098 openedByDOM:NO | 1122 openedByDOM:NO |
| 1099 atIndex:[tab_model_ count] | 1123 atIndex:[tab_model_ count] |
| 1100 inBackground:NO]; | 1124 inBackground:NO]; |
| 1101 [tab_model_ insertTabWithURL:GURL(kURL1) | 1125 [tab_model_ insertTabWithURL:GURL(kURL1) |
| 1102 referrer:web::Referrer() | 1126 referrer:web::Referrer() |
| 1103 transition:ui::PAGE_TRANSITION_TYPED | 1127 transition:ui::PAGE_TRANSITION_TYPED |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1117 [tab_model_ setCurrentTab:[tab_model_ tabAtIndex:1]]; | 1141 [tab_model_ setCurrentTab:[tab_model_ tabAtIndex:1]]; |
| 1118 | 1142 |
| 1119 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); | 1143 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); |
| 1120 EXPECT_EQ([tab_model_ tabAtIndex:1], | 1144 EXPECT_EQ([tab_model_ tabAtIndex:1], |
| 1121 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); | 1145 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); |
| 1122 EXPECT_EQ([tab_model_ tabAtIndex:2], | 1146 EXPECT_EQ([tab_model_ tabAtIndex:2], |
| 1123 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); | 1147 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); |
| 1124 | 1148 |
| 1125 // Force state to flush to disk on the main thread so it can be immediately | 1149 // Force state to flush to disk on the main thread so it can be immediately |
| 1126 // tested below. | 1150 // tested below. |
| 1127 SessionWindowIOS* window = [tab_model_ windowForSavingSession]; | 1151 [test_session_service setPerformIO:YES]; |
| 1128 [[SessionServiceIOS sharedService] performSaveWindow:window | 1152 [tab_model_ saveSessionImmediately:YES]; |
| 1129 toDirectory:stashPath]; | 1153 [test_session_service setPerformIO:NO]; |
| 1130 [tab_model_ browserStateDestroyed]; | |
| 1131 tab_model_.reset(); | |
| 1132 | 1154 |
| 1133 // Restoring TabModel session sends asynchronous tasks to IO thread, wait | 1155 NSString* state_path = base::SysUTF8ToNSString( |
| 1134 // for them to complete after destroying the TabModel. | 1156 chrome_browser_state_->GetStatePath().AsUTF8Unsafe()); |
| 1135 base::RunLoop().RunUntilIdle(); | 1157 NSString* session_path = |
| 1136 | 1158 [test_session_service sessionPathForDirectory:state_path]; |
| 1137 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] | 1159 SessionWindowIOS* session_window = |
| 1138 loadWindowForBrowserState:chrome_browser_state_.get()]; | 1160 [test_session_service loadSessionWindowFromPath:session_path]; |
| 1139 | 1161 |
| 1140 // Create tab model from saved session. | 1162 // Create tab model from saved session. |
| 1141 base::scoped_nsobject<TestSessionService> test_service( | 1163 SetTabModel(CreateTabModel(test_session_service.get(), session_window)); |
| 1142 [[TestSessionService alloc] init]); | |
| 1143 | 1164 |
| 1144 tab_model_.reset([[TabModel alloc] | |
| 1145 initWithSessionWindow:sessionWindow | |
| 1146 sessionService:test_service | |
| 1147 browserState:chrome_browser_state_.get()]); | |
| 1148 ASSERT_EQ(3u, [tab_model_ count]); | 1165 ASSERT_EQ(3u, [tab_model_ count]); |
| 1149 | 1166 |
| 1150 EXPECT_EQ([tab_model_ tabAtIndex:1], [tab_model_ currentTab]); | 1167 EXPECT_EQ([tab_model_ tabAtIndex:1], [tab_model_ currentTab]); |
| 1151 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); | 1168 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); |
| 1152 EXPECT_EQ([tab_model_ tabAtIndex:1], | 1169 EXPECT_EQ([tab_model_ tabAtIndex:1], |
| 1153 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); | 1170 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); |
| 1154 EXPECT_EQ([tab_model_ tabAtIndex:2], | 1171 EXPECT_EQ([tab_model_ tabAtIndex:2], |
| 1155 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); | 1172 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); |
| 1156 | 1173 |
| 1157 // Clean up. | 1174 // Clean up. |
| 1158 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath | 1175 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath |
| 1159 error:nullptr]); | 1176 error:nullptr]); |
| 1160 } | 1177 } |
| 1161 | 1178 |
| 1162 } // anonymous namespace | 1179 } // anonymous namespace |
| OLD | NEW |