Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: ios/chrome/browser/tabs/tab_model_unittest.mm

Issue 2810743002: [ios] Refactor SessionServiceIOS to remove dependency on BrowserState. (Closed)
Patch Set: Fix a typo in SessionServiceIOS initializer's comment. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/tabs/tab_model.mm ('k') | ios/chrome/browser/test/perf_test_with_bvc_ios.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 24 matching lines...) Expand all
35 #include "ios/web/public/test/test_web_thread_bundle.h" 35 #include "ios/web/public/test/test_web_thread_bundle.h"
36 #include "ios/web/public/web_thread.h" 36 #include "ios/web/public/web_thread.h"
37 #import "ios/web/web_state/ui/crw_web_controller.h" 37 #import "ios/web/web_state/ui/crw_web_controller.h"
38 #import "ios/web/web_state/web_state_impl.h" 38 #import "ios/web/web_state/web_state_impl.h"
39 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "testing/gtest_mac.h" 40 #include "testing/gtest_mac.h"
41 #include "testing/platform_test.h" 41 #include "testing/platform_test.h"
42 #import "third_party/ocmock/OCMock/OCMock.h" 42 #import "third_party/ocmock/OCMock/OCMock.h"
43 #include "third_party/ocmock/gtest_support.h" 43 #include "third_party/ocmock/gtest_support.h"
44 44
45 @interface TabModel (VisibleForTesting)
46 - (SessionWindowIOS*)windowForSavingSession;
47 @end
48
49 // Defines a TabModelObserver for use in unittests. This class can be used to 45 // Defines a TabModelObserver for use in unittests. This class can be used to
50 // test if an observer method was called or not. 46 // test if an observer method was called or not.
51 @interface TabModelObserverPong : NSObject<TabModelObserver> { 47 @interface TabModelObserverPong : NSObject<TabModelObserver> {
52 // TODO(crbug.com/661989): Add tests for the other observer methods. 48 // TODO(crbug.com/661989): Add tests for the other observer methods.
53 BOOL tabMovedWasCalled_; 49 BOOL tabMovedWasCalled_;
54 } 50 }
55 @property(nonatomic, assign) BOOL tabMovedWasCalled; 51 @property(nonatomic, assign) BOOL tabMovedWasCalled;
56 @end 52 @end
57 53
58 @implementation TabModelObserverPong 54 @implementation TabModelObserverPong
(...skipping 18 matching lines...) Expand all
77 TabModelTest() 73 TabModelTest()
78 : scoped_browser_state_manager_( 74 : scoped_browser_state_manager_(
79 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), 75 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())),
80 web_client_(base::MakeUnique<ChromeWebClient>()) { 76 web_client_(base::MakeUnique<ChromeWebClient>()) {
81 DCHECK_CURRENTLY_ON(web::WebThread::UI); 77 DCHECK_CURRENTLY_ON(web::WebThread::UI);
82 78
83 TestChromeBrowserState::Builder test_cbs_builder; 79 TestChromeBrowserState::Builder test_cbs_builder;
84 chrome_browser_state_ = test_cbs_builder.Build(); 80 chrome_browser_state_ = test_cbs_builder.Build();
85 81
86 session_window_.reset([[SessionWindowIOS alloc] init]); 82 session_window_.reset([[SessionWindowIOS alloc] init]);
83
87 // Create tab model with just a dummy session service so the async state 84 // Create tab model with just a dummy session service so the async state
88 // saving doesn't trigger unless actually wanted. 85 // saving doesn't trigger unless actually wanted.
89 base::scoped_nsobject<TestSessionService> test_service( 86 SetTabModel(
90 [[TestSessionService alloc] init]); 87 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 } 88 }
98 89
99 ~TabModelTest() override { 90 ~TabModelTest() override {
100 [tab_model_ browserStateDestroyed]; 91 [tab_model_ browserStateDestroyed];
101 } 92 }
102 93
94 void SetTabModel(base::scoped_nsobject<TabModel> tab_model) {
95 if (tab_model_) {
96 @autoreleasepool {
97 [tab_model_ browserStateDestroyed];
98 tab_model_.reset();
99 }
100 }
101
102 tab_model_ = tab_model;
103 }
104
105 base::scoped_nsobject<TabModel> CreateTabModel(
106 SessionServiceIOS* session_service,
107 SessionWindowIOS* session_window) {
108 base::scoped_nsobject<TabModel> tab_model([[TabModel alloc]
109 initWithSessionWindow:session_window
110 sessionService:session_service
111 browserState:chrome_browser_state_.get()]);
112 [tab_model setWebUsageEnabled:NO];
113 [tab_model setPrimary:YES];
114 return tab_model;
115 }
116
103 protected: 117 protected:
104 // Creates a session window with entries named "restored window 1", 118 // Creates a session window with entries named "restored window 1",
105 // "restored window 2" and "restored window 3" and the second entry 119 // "restored window 2" and "restored window 3" and the second entry
106 // marked as selected. 120 // marked as selected.
107 base::scoped_nsobject<SessionWindowIOS> CreateSessionWindow() { 121 base::scoped_nsobject<SessionWindowIOS> CreateSessionWindow() {
108 NSMutableArray<CRWSessionStorage*>* sessions = [NSMutableArray array]; 122 NSMutableArray<CRWSessionStorage*>* sessions = [NSMutableArray array];
109 for (int i = 0; i < 3; i++) { 123 for (int i = 0; i < 3; i++) {
110 CRWSessionStorage* session_storage = 124 CRWSessionStorage* session_storage =
111 [[[CRWSessionStorage alloc] init] autorelease]; 125 [[[CRWSessionStorage alloc] init] autorelease];
112 [sessions addObject:session_storage]; 126 [sessions addObject:session_storage];
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 1098
1085 web::WebState* web_state_ptr = web_state.get(); 1099 web::WebState* web_state_ptr = web_state.get();
1086 [tab_model_ webStateList]->InsertWebState(0, std::move(web_state)); 1100 [tab_model_ webStateList]->InsertWebState(0, std::move(web_state));
1087 EXPECT_NSNE(nil, LegacyTabHelper::GetTabForWebState(web_state_ptr)); 1101 EXPECT_NSNE(nil, LegacyTabHelper::GetTabForWebState(web_state_ptr));
1088 } 1102 }
1089 1103
1090 TEST_F(TabModelTest, PersistSelectionChange) { 1104 TEST_F(TabModelTest, PersistSelectionChange) {
1091 NSString* stashPath = 1105 NSString* stashPath =
1092 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); 1106 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value());
1093 1107
1108 // Reset the TabModel with a custom SessionServiceIOS (to control whether
1109 // data is saved to disk).
1110 base::scoped_nsobject<TestSessionService> test_session_service(
1111 [[TestSessionService alloc] init]);
1112 SetTabModel(CreateTabModel(test_session_service.get(), nil));
1113
1094 [tab_model_ insertTabWithURL:GURL(kURL1) 1114 [tab_model_ insertTabWithURL:GURL(kURL1)
1095 referrer:web::Referrer() 1115 referrer:web::Referrer()
1096 transition:ui::PAGE_TRANSITION_TYPED 1116 transition:ui::PAGE_TRANSITION_TYPED
1097 opener:nil 1117 opener:nil
1098 openedByDOM:NO 1118 openedByDOM:NO
1099 atIndex:[tab_model_ count] 1119 atIndex:[tab_model_ count]
1100 inBackground:NO]; 1120 inBackground:NO];
1101 [tab_model_ insertTabWithURL:GURL(kURL1) 1121 [tab_model_ insertTabWithURL:GURL(kURL1)
1102 referrer:web::Referrer() 1122 referrer:web::Referrer()
1103 transition:ui::PAGE_TRANSITION_TYPED 1123 transition:ui::PAGE_TRANSITION_TYPED
(...skipping 13 matching lines...) Expand all
1117 [tab_model_ setCurrentTab:[tab_model_ tabAtIndex:1]]; 1137 [tab_model_ setCurrentTab:[tab_model_ tabAtIndex:1]];
1118 1138
1119 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); 1139 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]);
1120 EXPECT_EQ([tab_model_ tabAtIndex:1], 1140 EXPECT_EQ([tab_model_ tabAtIndex:1],
1121 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); 1141 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]);
1122 EXPECT_EQ([tab_model_ tabAtIndex:2], 1142 EXPECT_EQ([tab_model_ tabAtIndex:2],
1123 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); 1143 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]);
1124 1144
1125 // Force state to flush to disk on the main thread so it can be immediately 1145 // Force state to flush to disk on the main thread so it can be immediately
1126 // tested below. 1146 // tested below.
1127 SessionWindowIOS* window = [tab_model_ windowForSavingSession]; 1147 [test_session_service setPerformIO:YES];
1128 [[SessionServiceIOS sharedService] performSaveWindow:window 1148 [tab_model_ saveSessionImmediately:YES];
1129 toDirectory:stashPath]; 1149 [test_session_service setPerformIO:NO];
1130 [tab_model_ browserStateDestroyed];
1131 tab_model_.reset();
1132 1150
1133 // Restoring TabModel session sends asynchronous tasks to IO thread, wait 1151 NSString* state_path = base::SysUTF8ToNSString(
1134 // for them to complete after destroying the TabModel. 1152 chrome_browser_state_->GetStatePath().AsUTF8Unsafe());
1135 base::RunLoop().RunUntilIdle(); 1153 SessionWindowIOS* session_window =
1136 1154 [test_session_service loadSessionWindowFromDirectory:state_path];
1137 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService]
1138 loadWindowForBrowserState:chrome_browser_state_.get()];
1139 1155
1140 // Create tab model from saved session. 1156 // Create tab model from saved session.
1141 base::scoped_nsobject<TestSessionService> test_service( 1157 SetTabModel(CreateTabModel(test_session_service.get(), session_window));
1142 [[TestSessionService alloc] init]);
1143 1158
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]); 1159 ASSERT_EQ(3u, [tab_model_ count]);
1149 1160
1150 EXPECT_EQ([tab_model_ tabAtIndex:1], [tab_model_ currentTab]); 1161 EXPECT_EQ([tab_model_ tabAtIndex:1], [tab_model_ currentTab]);
1151 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]); 1162 EXPECT_EQ(nil, [tab_model_ openerOfTab:[tab_model_ tabAtIndex:1]]);
1152 EXPECT_EQ([tab_model_ tabAtIndex:1], 1163 EXPECT_EQ([tab_model_ tabAtIndex:1],
1153 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]); 1164 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:2]]);
1154 EXPECT_EQ([tab_model_ tabAtIndex:2], 1165 EXPECT_EQ([tab_model_ tabAtIndex:2],
1155 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]); 1166 [tab_model_ openerOfTab:[tab_model_ tabAtIndex:0]]);
1156 1167
1157 // Clean up. 1168 // Clean up.
1158 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath 1169 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath
1159 error:nullptr]); 1170 error:nullptr]);
1160 } 1171 }
1161 1172
1162 } // anonymous namespace 1173 } // anonymous namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/tabs/tab_model.mm ('k') | ios/chrome/browser/test/perf_test_with_bvc_ios.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698