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