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