| 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/mac/scoped_nsobject.h" | 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 16 #include "ios/chrome/browser/chrome_paths.h" | 16 #include "ios/chrome/browser/chrome_paths.h" |
| 17 #import "ios/chrome/browser/sessions/session_service.h" | 17 #import "ios/chrome/browser/sessions/session_service.h" |
| 18 #import "ios/chrome/browser/sessions/session_window.h" | 18 #import "ios/chrome/browser/sessions/session_window.h" |
| 19 #include "ios/web/public/navigation_item.h" | 19 #include "ios/web/public/navigation_item.h" |
| 20 #import "ios/web/public/navigation_manager.h" | 20 #import "ios/web/public/navigation_manager.h" |
| 21 #include "ios/web/public/test/test_web_thread_bundle.h" | 21 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "testing/gtest_mac.h" | 23 #include "testing/gtest_mac.h" |
| 24 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 25 #import "third_party/ocmock/OCMock/OCMock.h" | 25 #import "third_party/ocmock/OCMock/OCMock.h" |
| 26 | 26 |
| 27 @interface SessionServiceIOS (Testing) | |
| 28 - (void)performSaveWindow:(SessionWindowIOS*)window | |
| 29 toDirectory:(NSString*)directory; | |
| 30 @end | |
| 31 | |
| 32 namespace { | 27 namespace { |
| 33 | 28 |
| 34 // Fixture Class. Takes care of deleting the directory used to store test data. | 29 // Fixture Class. Takes care of deleting the directory used to store test data. |
| 35 class SessionServiceTest : public PlatformTest { | 30 class SessionServiceTest : public PlatformTest { |
| 36 private: | 31 public: |
| 37 base::ScopedTempDir test_dir_; | 32 SessionServiceTest() = default; |
| 33 ~SessionServiceTest() override = default; |
| 38 | 34 |
| 39 protected: | 35 protected: |
| 40 void SetUp() override { | 36 void SetUp() override { |
| 37 PlatformTest::SetUp(); |
| 41 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 38 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 42 // directoryName_ = [NSString | |
| 43 // stringWithCString:test_dir_.path().value().c_str() | |
| 44 // encoding:NSASCIIStringEncoding]; | |
| 45 | |
| 46 TestChromeBrowserState::Builder test_cbs_builder; | 39 TestChromeBrowserState::Builder test_cbs_builder; |
| 47 test_cbs_builder.SetPath(test_dir_.GetPath()); | 40 test_cbs_builder.SetPath(test_dir_.GetPath()); |
| 48 chrome_browser_state_ = test_cbs_builder.Build(); | 41 chrome_browser_state_ = test_cbs_builder.Build(); |
| 49 directoryName_ = | 42 directory_name_.reset([base::SysUTF8ToNSString( |
| 50 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); | 43 chrome_browser_state_->GetStatePath().value()) copy]); |
| 51 } | 44 } |
| 52 | 45 |
| 53 void TearDown() override {} | |
| 54 | |
| 55 // Helper function to load a SessionWindowIOS from a given testdata | 46 // Helper function to load a SessionWindowIOS from a given testdata |
| 56 // |filename|. Returns nil if there was an error loading the session. | 47 // |filename|. Returns nil if there was an error loading the session. |
| 57 SessionWindowIOS* LoadSessionFromTestDataFile( | 48 SessionWindowIOS* LoadSessionFromTestDataFile( |
| 58 const base::FilePath::StringType& filename) { | 49 const base::FilePath::StringType& filename) { |
| 59 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 50 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 60 base::FilePath plist_path; | 51 base::FilePath plist_path; |
| 61 bool success = PathService::Get(ios::DIR_TEST_DATA, &plist_path); | 52 bool success = PathService::Get(ios::DIR_TEST_DATA, &plist_path); |
| 62 EXPECT_TRUE(success); | 53 EXPECT_TRUE(success); |
| 63 if (!success) { | 54 if (!success) { |
| 64 return nil; | 55 return nil; |
| 65 } | 56 } |
| 66 | 57 |
| 67 plist_path = plist_path.AppendASCII("sessions"); | 58 plist_path = plist_path.AppendASCII("sessions"); |
| 68 plist_path = plist_path.Append(filename); | 59 plist_path = plist_path.Append(filename); |
| 69 EXPECT_TRUE(base::PathExists(plist_path)); | 60 EXPECT_TRUE(base::PathExists(plist_path)); |
| 70 | 61 |
| 71 NSString* path = base::SysUTF8ToNSString(plist_path.value()); | 62 NSString* path = base::SysUTF8ToNSString(plist_path.value()); |
| 72 return [service loadWindowFromPath:path | 63 return [service loadWindowFromPath:path |
| 73 forBrowserState:chrome_browser_state_.get()]; | 64 forBrowserState:chrome_browser_state_.get()]; |
| 74 } | 65 } |
| 75 | 66 |
| 76 NSString* directoryName_; | 67 ios::ChromeBrowserState* chrome_browser_state() { |
| 68 return chrome_browser_state_.get(); |
| 69 } |
| 70 |
| 71 NSString* directory_name() { return directory_name_.get(); } |
| 72 |
| 73 private: |
| 74 base::ScopedTempDir test_dir_; |
| 77 web::TestWebThreadBundle thread_bundle_; | 75 web::TestWebThreadBundle thread_bundle_; |
| 78 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 76 std::unique_ptr<ios::ChromeBrowserState> chrome_browser_state_; |
| 77 base::scoped_nsobject<NSString> directory_name_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(SessionServiceTest); |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 TEST_F(SessionServiceTest, Singleton) { | 82 TEST_F(SessionServiceTest, Singleton) { |
| 82 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 83 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 83 EXPECT_TRUE(service != nil); | 84 EXPECT_TRUE(service != nil); |
| 84 | 85 |
| 85 SessionServiceIOS* anotherService = [SessionServiceIOS sharedService]; | 86 SessionServiceIOS* another_service = [SessionServiceIOS sharedService]; |
| 86 EXPECT_TRUE(anotherService != nil); | 87 EXPECT_TRUE(another_service != nil); |
| 87 | 88 |
| 88 EXPECT_TRUE(service == anotherService); | 89 EXPECT_TRUE(service == another_service); |
| 89 } | 90 } |
| 90 | 91 |
| 91 TEST_F(SessionServiceTest, SaveWindowToDirectory) { | 92 TEST_F(SessionServiceTest, SaveWindowToDirectory) { |
| 92 id sessionWindowMock = | 93 id session_window_mock = |
| 93 [OCMockObject niceMockForClass:[SessionWindowIOS class]]; | 94 [OCMockObject niceMockForClass:[SessionWindowIOS class]]; |
| 94 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 95 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 95 [service performSaveWindow:sessionWindowMock toDirectory:directoryName_]; | 96 [service performSaveWindow:session_window_mock toDirectory:directory_name()]; |
| 96 | 97 |
| 97 NSFileManager* fileManager = [NSFileManager defaultManager]; | 98 NSFileManager* file_manager = [NSFileManager defaultManager]; |
| 98 EXPECT_TRUE([fileManager removeItemAtPath:directoryName_ error:NULL]); | 99 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]); |
| 99 } | 100 } |
| 100 | 101 |
| 101 TEST_F(SessionServiceTest, SaveWindowToDirectoryAlreadyExistent) { | 102 TEST_F(SessionServiceTest, SaveWindowToDirectoryAlreadyExistent) { |
| 102 id sessionWindowMock = | 103 id session_window_mock = |
| 103 [OCMockObject niceMockForClass:[SessionWindowIOS class]]; | 104 [OCMockObject niceMockForClass:[SessionWindowIOS class]]; |
| 104 EXPECT_TRUE([[NSFileManager defaultManager] | 105 EXPECT_TRUE([[NSFileManager defaultManager] |
| 105 createDirectoryAtPath:directoryName_ | 106 createDirectoryAtPath:directory_name() |
| 106 withIntermediateDirectories:YES | 107 withIntermediateDirectories:YES |
| 107 attributes:nil | 108 attributes:nil |
| 108 error:NULL]); | 109 error:nullptr]); |
| 109 | 110 |
| 110 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 111 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 111 [service performSaveWindow:sessionWindowMock toDirectory:directoryName_]; | 112 [service performSaveWindow:session_window_mock toDirectory:directory_name()]; |
| 112 | 113 |
| 113 NSFileManager* fileManager = [NSFileManager defaultManager]; | 114 NSFileManager* file_manager = [NSFileManager defaultManager]; |
| 114 EXPECT_TRUE([fileManager removeItemAtPath:directoryName_ error:NULL]); | 115 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]); |
| 115 } | 116 } |
| 116 | 117 |
| 117 TEST_F(SessionServiceTest, LoadEmptyWindowFromDirectory) { | 118 TEST_F(SessionServiceTest, LoadEmptyWindowFromDirectory) { |
| 118 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 119 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 119 SessionWindowIOS* sessionWindow = | 120 SessionWindowIOS* session_window = |
| 120 [service loadWindowForBrowserState:chrome_browser_state_.get()]; | 121 [service loadWindowForBrowserState:chrome_browser_state()]; |
| 121 EXPECT_TRUE(sessionWindow == nil); | 122 EXPECT_TRUE(session_window == nil); |
| 122 } | 123 } |
| 123 | 124 |
| 124 TEST_F(SessionServiceTest, LoadWindowFromDirectory) { | 125 TEST_F(SessionServiceTest, LoadWindowFromDirectory) { |
| 125 SessionServiceIOS* service = [SessionServiceIOS sharedService]; | 126 SessionServiceIOS* service = [SessionServiceIOS sharedService]; |
| 126 base::scoped_nsobject<SessionWindowIOS> origSessionWindow( | 127 base::scoped_nsobject<SessionWindowIOS> origSessionWindow( |
| 127 [[SessionWindowIOS alloc] init]); | 128 [[SessionWindowIOS alloc] init]); |
| 128 [service performSaveWindow:origSessionWindow toDirectory:directoryName_]; | 129 [service performSaveWindow:origSessionWindow toDirectory:directory_name()]; |
| 129 | 130 |
| 130 SessionWindowIOS* sessionWindow = | 131 SessionWindowIOS* session_window = |
| 131 [service loadWindowForBrowserState:chrome_browser_state_.get()]; | 132 [service loadWindowForBrowserState:chrome_browser_state()]; |
| 132 EXPECT_TRUE(sessionWindow != nil); | 133 EXPECT_TRUE(session_window != nil); |
| 133 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(sessionWindow.selectedIndex)); | 134 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(session_window.selectedIndex)); |
| 134 EXPECT_EQ(0U, sessionWindow.sessions.count); | 135 EXPECT_EQ(0U, session_window.sessions.count); |
| 135 } | 136 } |
| 136 | 137 |
| 137 TEST_F(SessionServiceTest, LoadCorruptedWindow) { | 138 TEST_F(SessionServiceTest, LoadCorruptedWindow) { |
| 138 SessionWindowIOS* sessionWindow = | 139 SessionWindowIOS* session_window = |
| 139 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("corrupted.plist")); | 140 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("corrupted.plist")); |
| 140 EXPECT_TRUE(sessionWindow == nil); | 141 EXPECT_TRUE(session_window == nil); |
| 142 } |
| 143 |
| 144 // TODO(crbug.com/661633): remove this once M67 has shipped (i.e. once more |
| 145 // than a year has passed since the introduction of the compatibility code). |
| 146 TEST_F(SessionServiceTest, LoadM57Session) { |
| 147 SessionWindowIOS* session_window = |
| 148 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("session_m57.plist")); |
| 149 EXPECT_TRUE(session_window != nil); |
| 150 } |
| 151 |
| 152 // TODO(crbug.com/661633): remove this once M68 has shipped (i.e. once more |
| 153 // than a year has passed since the introduction of the compatibility code). |
| 154 TEST_F(SessionServiceTest, LoadM58Session) { |
| 155 SessionWindowIOS* session_window = |
| 156 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("session_m58.plist")); |
| 157 EXPECT_TRUE(session_window != nil); |
| 141 } | 158 } |
| 142 | 159 |
| 143 } // anonymous namespace | 160 } // anonymous namespace |
| OLD | NEW |