| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/test/scoped_task_environment.h" | 15 #include "base/test/scoped_task_environment.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "ios/chrome/browser/chrome_paths.h" | 17 #include "ios/chrome/browser/chrome_paths.h" |
| 18 #import "ios/chrome/browser/sessions/session_ios.h" |
| 18 #import "ios/chrome/browser/sessions/session_service_ios.h" | 19 #import "ios/chrome/browser/sessions/session_service_ios.h" |
| 19 #import "ios/chrome/browser/sessions/session_window_ios.h" | 20 #import "ios/chrome/browser/sessions/session_window_ios.h" |
| 20 #import "ios/web/public/crw_session_storage.h" | 21 #import "ios/web/public/crw_session_storage.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "testing/gtest_mac.h" | 23 #include "testing/gtest_mac.h" |
| 23 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 24 | 25 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." | 27 #error "This file requires ARC support." |
| 27 #endif | 28 #endif |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 return nil; | 62 return nil; |
| 62 | 63 |
| 63 session_path = session_path.Append(FILE_PATH_LITERAL("sessions")); | 64 session_path = session_path.Append(FILE_PATH_LITERAL("sessions")); |
| 64 session_path = session_path.Append(filename); | 65 session_path = session_path.Append(filename); |
| 65 if (!base::PathExists(session_path)) | 66 if (!base::PathExists(session_path)) |
| 66 return nil; | 67 return nil; |
| 67 | 68 |
| 68 return base::SysUTF8ToNSString(session_path.AsUTF8Unsafe()); | 69 return base::SysUTF8ToNSString(session_path.AsUTF8Unsafe()); |
| 69 } | 70 } |
| 70 | 71 |
| 71 // Create a SessionWindowIOS with |tab_count| tabs. | 72 // Create a SessionIOS corresponding to |window_count| windows each with |
| 72 SessionWindowIOS* CreateSessionWindow(NSUInteger tab_count) { | 73 // |tab_count| tabs. |
| 73 NSMutableArray<CRWSessionStorage*>* tabs = [NSMutableArray array]; | 74 SessionIOS* CreateSession(NSUInteger window_count, NSUInteger tab_count) { |
| 74 while (tabs.count < tab_count) { | 75 NSMutableArray<SessionWindowIOS*>* windows = [NSMutableArray array]; |
| 75 [tabs addObject:[[CRWSessionStorage alloc] init]]; | 76 while (windows.count < window_count) { |
| 77 NSMutableArray<CRWSessionStorage*>* tabs = [NSMutableArray array]; |
| 78 while (tabs.count < tab_count) { |
| 79 [tabs addObject:[[CRWSessionStorage alloc] init]]; |
| 80 } |
| 81 [windows addObject:[[SessionWindowIOS alloc] |
| 82 initWithSessions:[tabs copy] |
| 83 selectedIndex:(tabs.count ? tabs.count - 1 |
| 84 : NSNotFound)]]; |
| 76 } | 85 } |
| 77 return [[SessionWindowIOS alloc] | 86 return [[SessionIOS alloc] initWithWindows:[windows copy]]; |
| 78 initWithSessions:[tabs copy] | |
| 79 selectedIndex:(tabs.count ? tabs.count - 1 : NSNotFound)]; | |
| 80 } | 87 } |
| 81 | 88 |
| 82 SessionServiceIOS* session_service() { return session_service_; } | 89 SessionServiceIOS* session_service() { return session_service_; } |
| 83 | 90 |
| 84 NSString* directory() { return directory_; } | 91 NSString* directory() { return directory_; } |
| 85 | 92 |
| 86 private: | 93 private: |
| 87 base::ScopedTempDir scoped_temp_directory_; | 94 base::ScopedTempDir scoped_temp_directory_; |
| 88 base::test::ScopedTaskEnvironment scoped_task_environment_; | 95 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 89 SessionServiceIOS* session_service_; | 96 SessionServiceIOS* session_service_; |
| 90 NSString* directory_; | 97 NSString* directory_; |
| 91 | 98 |
| 92 DISALLOW_COPY_AND_ASSIGN(SessionServiceTest); | 99 DISALLOW_COPY_AND_ASSIGN(SessionServiceTest); |
| 93 }; | 100 }; |
| 94 | 101 |
| 95 TEST_F(SessionServiceTest, SessionPathForDirectory) { | 102 TEST_F(SessionServiceTest, SessionPathForDirectory) { |
| 96 EXPECT_NSEQ(@"session.plist", | 103 EXPECT_NSEQ(@"session.plist", |
| 97 [SessionServiceIOS sessionPathForDirectory:@""]); | 104 [SessionServiceIOS sessionPathForDirectory:@""]); |
| 98 } | 105 } |
| 99 | 106 |
| 100 TEST_F(SessionServiceTest, SaveSessionWindowToDirectory) { | 107 TEST_F(SessionServiceTest, SaveSessionWindowToPath) { |
| 101 [session_service() saveSessionWindow:CreateSessionWindow(0u) | 108 [session_service() saveSession:CreateSession(0u, 0u) |
| 102 directory:directory() | 109 directory:directory() |
| 103 immediately:YES]; | 110 immediately:YES]; |
| 104 | 111 |
| 105 // Even if immediately is YES, the file is created by a task on the task | 112 // Even if |immediately| is YES, the file is created by a task on the task |
| 106 // runner passed to SessionServiceIOS initializer (which is the current | 113 // runner passed to SessionServiceIOS initializer (which is the current |
| 107 // thread task runner during test). Wait for the task to complete. | 114 // thread task runner during test). Wait for the task to complete. |
| 108 base::RunLoop().RunUntilIdle(); | 115 base::RunLoop().RunUntilIdle(); |
| 109 | 116 |
| 110 NSFileManager* file_manager = [NSFileManager defaultManager]; | 117 NSFileManager* file_manager = [NSFileManager defaultManager]; |
| 111 EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); | 118 EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); |
| 112 } | 119 } |
| 113 | 120 |
| 114 TEST_F(SessionServiceTest, SaveSessionWindowToExistingDirectory) { | 121 TEST_F(SessionServiceTest, SaveSessionWindowToPathDirectoryExists) { |
| 115 ASSERT_TRUE([[NSFileManager defaultManager] createDirectoryAtPath:directory() | 122 ASSERT_TRUE([[NSFileManager defaultManager] createDirectoryAtPath:directory() |
| 116 withIntermediateDirectories:YES | 123 withIntermediateDirectories:YES |
| 117 attributes:nil | 124 attributes:nil |
| 118 error:nullptr]); | 125 error:nullptr]); |
| 119 | 126 |
| 120 [session_service() saveSessionWindow:CreateSessionWindow(0u) | 127 [session_service() saveSession:CreateSession(0u, 0u) |
| 121 directory:directory() | 128 directory:directory() |
| 122 immediately:YES]; | 129 immediately:YES]; |
| 123 | 130 |
| 124 // Even if immediately is YES, the file is created by a task on the task | 131 // Even if |immediately| is YES, the file is created by a task on the task |
| 125 // runner passed to SessionServiceIOS initializer (which is the current | 132 // runner passed to SessionServiceIOS initializer (which is the current |
| 126 // thread task runner during test). Wait for the task to complete. | 133 // thread task runner during test). Wait for the task to complete. |
| 127 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
| 128 | 135 |
| 129 NSFileManager* file_manager = [NSFileManager defaultManager]; | 136 NSFileManager* file_manager = [NSFileManager defaultManager]; |
| 130 EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); | 137 EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); |
| 131 } | 138 } |
| 132 | 139 |
| 133 TEST_F(SessionServiceTest, LoadSessionWindowFromDirectoryNoFile) { | 140 TEST_F(SessionServiceTest, LoadSessionFromDirectoryNoFile) { |
| 134 SessionWindowIOS* session_window = | 141 SessionIOS* session = |
| 135 [session_service() loadSessionWindowFromDirectory:directory()]; | 142 [session_service() loadSessionFromDirectory:directory()]; |
| 136 EXPECT_TRUE(session_window == nil); | 143 EXPECT_TRUE(session == nil); |
| 137 } | 144 } |
| 138 | 145 |
| 139 TEST_F(SessionServiceTest, LoadSessionWindowFromDirectory) { | 146 TEST_F(SessionServiceTest, LoadSessionFromDirectory) { |
| 140 [session_service() saveSessionWindow:CreateSessionWindow(1u) | 147 [session_service() saveSession:CreateSession(2u, 1u) |
| 141 directory:directory() | 148 directory:directory() |
| 142 immediately:YES]; | 149 immediately:YES]; |
| 143 | 150 |
| 144 // Even if immediately is YES, the file is created by a task on the task | 151 // Even if |immediately| is YES, the file is created by a task on the task |
| 145 // runner passed to SessionServiceIOS initializer (which is the current | 152 // runner passed to SessionServiceIOS initializer (which is the current |
| 146 // thread task runner during test). Wait for the task to complete. | 153 // thread task runner during test). Wait for the task to complete. |
| 147 base::RunLoop().RunUntilIdle(); | 154 base::RunLoop().RunUntilIdle(); |
| 148 | 155 |
| 149 SessionWindowIOS* session_window = | 156 SessionIOS* session = |
| 150 [session_service() loadSessionWindowFromDirectory:directory()]; | 157 [session_service() loadSessionFromDirectory:directory()]; |
| 151 EXPECT_EQ(1u, session_window.sessions.count); | 158 EXPECT_EQ(2u, session.sessionWindows.count); |
| 152 EXPECT_EQ(0u, session_window.selectedIndex); | 159 for (SessionWindowIOS* sessionWindow in session.sessionWindows) { |
| 160 EXPECT_EQ(1u, sessionWindow.sessions.count); |
| 161 EXPECT_EQ(0u, sessionWindow.selectedIndex); |
| 162 } |
| 153 } | 163 } |
| 154 | 164 |
| 155 TEST_F(SessionServiceTest, LoadSessionWindowFromPath) { | 165 TEST_F(SessionServiceTest, LoadSessionFromPath) { |
| 156 [session_service() saveSessionWindow:CreateSessionWindow(1u) | 166 [session_service() saveSession:CreateSession(2u, 1u) |
| 157 directory:directory() | 167 directory:directory() |
| 158 immediately:YES]; | 168 immediately:YES]; |
| 159 | 169 |
| 160 // Even if immediately is YES, the file is created by a task on the task | 170 // Even if |immediately| is YES, the file is created by a task on the task |
| 161 // runner passed to SessionServiceIOS initializer (which is the current | 171 // runner passed to SessionServiceIOS initializer (which is the current |
| 162 // thread task runner during test). Wait for the task to complete. | 172 // thread task runner during test). Wait for the task to complete. |
| 163 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 164 | 174 |
| 165 NSString* session_path = | 175 NSString* session_path = |
| 166 [SessionServiceIOS sessionPathForDirectory:directory()]; | 176 [SessionServiceIOS sessionPathForDirectory:directory()]; |
| 167 NSString* renamed_path = [session_path stringByAppendingPathExtension:@"bak"]; | 177 NSString* renamed_path = [session_path stringByAppendingPathExtension:@"bak"]; |
| 168 ASSERT_NSNE(session_path, renamed_path); | 178 ASSERT_NSNE(session_path, renamed_path); |
| 169 | 179 |
| 170 // Rename the file. | 180 // Rename the file. |
| 171 ASSERT_TRUE([[NSFileManager defaultManager] moveItemAtPath:session_path | 181 ASSERT_TRUE([[NSFileManager defaultManager] moveItemAtPath:session_path |
| 172 toPath:renamed_path | 182 toPath:renamed_path |
| 173 error:nil]); | 183 error:nil]); |
| 174 | 184 |
| 175 SessionWindowIOS* session_window = | 185 SessionIOS* session = [session_service() loadSessionFromPath:renamed_path]; |
| 176 [session_service() loadSessionWindowFromPath:renamed_path]; | 186 EXPECT_EQ(2u, session.sessionWindows.count); |
| 177 EXPECT_EQ(1u, session_window.sessions.count); | 187 for (SessionWindowIOS* sessionWindow in session.sessionWindows) { |
| 178 EXPECT_EQ(0u, session_window.selectedIndex); | 188 EXPECT_EQ(1u, sessionWindow.sessions.count); |
| 189 EXPECT_EQ(0u, sessionWindow.selectedIndex); |
| 190 } |
| 179 } | 191 } |
| 180 | 192 |
| 181 TEST_F(SessionServiceTest, LoadCorruptedSessionWindow) { | 193 TEST_F(SessionServiceTest, LoadCorruptedSession) { |
| 182 NSString* session_path = | 194 NSString* session_path = |
| 183 SessionPathForTestData(FILE_PATH_LITERAL("corrupted.plist")); | 195 SessionPathForTestData(FILE_PATH_LITERAL("corrupted.plist")); |
| 184 ASSERT_NSNE(nil, session_path); | 196 ASSERT_NSNE(nil, session_path); |
| 185 SessionWindowIOS* session_window = | 197 SessionIOS* session = [session_service() loadSessionFromPath:session_path]; |
| 186 [session_service() loadSessionWindowFromPath:session_path]; | 198 EXPECT_TRUE(session == nil); |
| 187 EXPECT_TRUE(session_window == nil); | |
| 188 } | 199 } |
| 189 | 200 |
| 190 // TODO(crbug.com/661633): remove this once M67 has shipped (i.e. once more | 201 // TODO(crbug.com/661633): remove this once M67 has shipped (i.e. once more |
| 191 // than a year has passed since the introduction of the compatibility code). | 202 // than a year has passed since the introduction of the compatibility code). |
| 192 TEST_F(SessionServiceTest, LoadM57Session) { | 203 TEST_F(SessionServiceTest, LoadM57Session) { |
| 193 NSString* session_path = | 204 NSString* session_path = |
| 194 SessionPathForTestData(FILE_PATH_LITERAL("session_m57.plist")); | 205 SessionPathForTestData(FILE_PATH_LITERAL("session_m57.plist")); |
| 195 ASSERT_NSNE(nil, session_path); | 206 ASSERT_NSNE(nil, session_path); |
| 196 SessionWindowIOS* session_window = | 207 SessionIOS* session = [session_service() loadSessionFromPath:session_path]; |
| 197 [session_service() loadSessionWindowFromPath:session_path]; | 208 EXPECT_EQ(1u, session.sessionWindows.count); |
| 198 EXPECT_TRUE(session_window != nil); | |
| 199 } | 209 } |
| 200 | 210 |
| 201 // TODO(crbug.com/661633): remove this once M68 has shipped (i.e. once more | 211 // TODO(crbug.com/661633): remove this once M68 has shipped (i.e. once more |
| 202 // than a year has passed since the introduction of the compatibility code). | 212 // than a year has passed since the introduction of the compatibility code). |
| 203 TEST_F(SessionServiceTest, LoadM58Session) { | 213 TEST_F(SessionServiceTest, LoadM58Session) { |
| 204 NSString* session_path = | 214 NSString* session_path = |
| 205 SessionPathForTestData(FILE_PATH_LITERAL("session_m58.plist")); | 215 SessionPathForTestData(FILE_PATH_LITERAL("session_m58.plist")); |
| 206 ASSERT_NSNE(nil, session_path); | 216 ASSERT_NSNE(nil, session_path); |
| 207 SessionWindowIOS* session_window = | 217 SessionIOS* session = [session_service() loadSessionFromPath:session_path]; |
| 208 [session_service() loadSessionWindowFromPath:session_path]; | 218 EXPECT_EQ(1u, session.sessionWindows.count); |
| 209 EXPECT_TRUE(session_window != nil); | |
| 210 } | 219 } |
| 211 | 220 |
| 212 } // anonymous namespace | 221 } // anonymous namespace |
| OLD | NEW |