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

Side by Side Diff: ios/chrome/browser/sessions/session_service_ios_unittest.mm

Issue 2810743002: [ios] Refactor SessionServiceIOS to remove dependency on BrowserState. (Closed)
Patch Set: 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
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 <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/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
14 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" 14 #include "base/test/scoped_task_environment.h"
15 #include "base/threading/thread_task_runner_handle.h"
15 #include "ios/chrome/browser/chrome_paths.h" 16 #include "ios/chrome/browser/chrome_paths.h"
16 #import "ios/chrome/browser/sessions/session_service_ios.h" 17 #import "ios/chrome/browser/sessions/session_service_ios.h"
17 #import "ios/chrome/browser/sessions/session_window_ios.h" 18 #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" 19 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/gtest_mac.h" 20 #include "testing/gtest_mac.h"
23 #include "testing/platform_test.h" 21 #include "testing/platform_test.h"
24 #import "third_party/ocmock/OCMock/OCMock.h"
25 22
26 #if !defined(__has_feature) || !__has_feature(objc_arc) 23 #if !defined(__has_feature) || !__has_feature(objc_arc)
27 #error "This file requires ARC support." 24 #error "This file requires ARC support."
28 #endif 25 #endif
29 26
30 namespace { 27 namespace {
31 28
32 // 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.
33 class SessionServiceTest : public PlatformTest { 30 class SessionServiceTest : public PlatformTest {
34 public: 31 public:
35 SessionServiceTest() = default; 32 SessionServiceTest() = default;
36 ~SessionServiceTest() override = default; 33 ~SessionServiceTest() override = default;
37 34
38 protected: 35 protected:
39 void SetUp() override { 36 void SetUp() override {
40 PlatformTest::SetUp(); 37 PlatformTest::SetUp();
41 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 38 ASSERT_TRUE(scoped_temp_directory_.CreateUniqueTempDir());
42 TestChromeBrowserState::Builder test_cbs_builder; 39 base::FilePath directory_name = scoped_temp_directory_.GetPath();
43 test_cbs_builder.SetPath(test_dir_.GetPath()); 40 directory_name = directory_name.Append(FILE_PATH_LITERAL("sessions"));
44 chrome_browser_state_ = test_cbs_builder.Build(); 41 directory_name_ = base::SysUTF8ToNSString(directory_name.AsUTF8Unsafe());
45 directory_name_ = [base::SysUTF8ToNSString( 42
46 chrome_browser_state_->GetStatePath().value()) copy]; 43 scoped_refptr<base::SequencedTaskRunner> task_runner =
44 base::ThreadTaskRunnerHandle::Get();
45 session_service_ =
46 [[SessionServiceIOS alloc] initWithTaskRunner:task_runner];
47 } 47 }
48 48
49 // Helper function to load a SessionWindowIOS from a given testdata 49 void TearDown() override { session_service_ = nil; }
50 // |filename|. Returns nil if there was an error loading the session. 50
51 SessionWindowIOS* LoadSessionFromTestDataFile( 51 // Returns the path to serialized SessionWindowIOS from a testdata file named
52 const base::FilePath::StringType& filename) { 52 // |filename| or nil if the file cannot be found.
53 SessionServiceIOS* service = [SessionServiceIOS sharedService]; 53 NSString* SessionPathForTestData(const base::FilePath::CharType* filename) {
54 base::FilePath plist_path; 54 base::FilePath session_path;
55 bool success = PathService::Get(ios::DIR_TEST_DATA, &plist_path); 55 if (!PathService::Get(ios::DIR_TEST_DATA, &session_path))
56 EXPECT_TRUE(success);
57 if (!success) {
58 return nil; 56 return nil;
59 }
60 57
61 plist_path = plist_path.AppendASCII("sessions"); 58 session_path = session_path.Append(FILE_PATH_LITERAL("sessions"));
62 plist_path = plist_path.Append(filename); 59 session_path = session_path.Append(filename);
63 EXPECT_TRUE(base::PathExists(plist_path)); 60 if (!base::PathExists(session_path))
61 return nil;
64 62
65 NSString* path = base::SysUTF8ToNSString(plist_path.value()); 63 return base::SysUTF8ToNSString(session_path.AsUTF8Unsafe());
66 return [service loadWindowFromPath:path];
67 } 64 }
68 65
69 ios::ChromeBrowserState* chrome_browser_state() { 66 NSData* CreateSerializedSessionWindow() {
70 return chrome_browser_state_.get(); 67 SessionWindowIOS* session_window =
68 [[SessionWindowIOS alloc] initWithSessions:@[]
69 selectedIndex:NSNotFound];
70 return [NSKeyedArchiver archivedDataWithRootObject:session_window];
71 } 71 }
72 72
73 SessionServiceIOS* session_service() { return session_service_; }
74
73 NSString* directory_name() { return directory_name_; } 75 NSString* directory_name() { return directory_name_; }
74 76
77 NSString* session_path() {
78 return [session_service() sessionPathForDirectory:directory_name()];
79 }
80
75 private: 81 private:
76 base::ScopedTempDir test_dir_; 82 base::ScopedTempDir scoped_temp_directory_;
77 web::TestWebThreadBundle thread_bundle_; 83 base::test::ScopedTaskEnvironment scoped_task_environment_;
78 std::unique_ptr<ios::ChromeBrowserState> chrome_browser_state_; 84 SessionServiceIOS* session_service_;
79 NSString* directory_name_; 85 NSString* directory_name_;
80 86
81 DISALLOW_COPY_AND_ASSIGN(SessionServiceTest); 87 DISALLOW_COPY_AND_ASSIGN(SessionServiceTest);
82 }; 88 };
83 89
84 TEST_F(SessionServiceTest, Singleton) { 90 TEST_F(SessionServiceTest, SaveWindowToDirectory) {
sdefresne 2017/04/10 14:37:18 I removed this test intentionally as the singleton
85 SessionServiceIOS* service = [SessionServiceIOS sharedService]; 91 NSData* session_data = CreateSerializedSessionWindow();
86 EXPECT_TRUE(service != nil); 92 ASSERT_NSNE(nil, session_data);
87 93
88 SessionServiceIOS* another_service = [SessionServiceIOS sharedService]; 94 [session_service() performSaveSessionData:session_data
89 EXPECT_TRUE(another_service != nil); 95 sessionPath:session_path()];
90
91 EXPECT_TRUE(service == another_service);
92 }
93
94 TEST_F(SessionServiceTest, SaveWindowToDirectory) {
95 id session_window_mock =
96 [OCMockObject niceMockForClass:[SessionWindowIOS class]];
97 SessionServiceIOS* service = [SessionServiceIOS sharedService];
98 [service performSaveWindow:session_window_mock toDirectory:directory_name()];
99 96
100 NSFileManager* file_manager = [NSFileManager defaultManager]; 97 NSFileManager* file_manager = [NSFileManager defaultManager];
101 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]); 98 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]);
102 } 99 }
103 100
104 TEST_F(SessionServiceTest, SaveWindowToDirectoryAlreadyExistent) { 101 TEST_F(SessionServiceTest, SaveWindowToDirectoryAlreadyExistent) {
105 id session_window_mock = 102 ASSERT_TRUE([[NSFileManager defaultManager]
106 [OCMockObject niceMockForClass:[SessionWindowIOS class]];
107 EXPECT_TRUE([[NSFileManager defaultManager]
108 createDirectoryAtPath:directory_name() 103 createDirectoryAtPath:directory_name()
109 withIntermediateDirectories:YES 104 withIntermediateDirectories:YES
110 attributes:nil 105 attributes:nil
111 error:nullptr]); 106 error:nullptr]);
112 107
113 SessionServiceIOS* service = [SessionServiceIOS sharedService]; 108 NSData* session_data = CreateSerializedSessionWindow();
114 [service performSaveWindow:session_window_mock toDirectory:directory_name()]; 109 ASSERT_NSNE(nil, session_data);
110
111 [session_service() performSaveSessionData:session_data
112 sessionPath:session_path()];
115 113
116 NSFileManager* file_manager = [NSFileManager defaultManager]; 114 NSFileManager* file_manager = [NSFileManager defaultManager];
117 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]); 115 EXPECT_TRUE([file_manager removeItemAtPath:directory_name() error:nullptr]);
118 } 116 }
119 117
120 TEST_F(SessionServiceTest, LoadEmptyWindowFromDirectory) { 118 TEST_F(SessionServiceTest, LoadEmptyWindowFromDirectory) {
121 SessionServiceIOS* service = [SessionServiceIOS sharedService]; 119 SessionServiceIOS* service = session_service();
122 SessionWindowIOS* session_window = 120 SessionWindowIOS* session_window =
123 [service loadWindowForBrowserState:chrome_browser_state()]; 121 [service loadSessionWindowFromPath:session_path()];
124 EXPECT_TRUE(session_window == nil); 122 EXPECT_TRUE(session_window == nil);
125 } 123 }
126 124
127 TEST_F(SessionServiceTest, LoadWindowFromDirectory) { 125 TEST_F(SessionServiceTest, LoadWindowFromDirectory) {
128 SessionServiceIOS* service = [SessionServiceIOS sharedService]; 126 NSData* session_data = CreateSerializedSessionWindow();
129 SessionWindowIOS* origSessionWindow = [[SessionWindowIOS alloc] init]; 127 ASSERT_NSNE(nil, session_data);
130 [service performSaveWindow:origSessionWindow toDirectory:directory_name()]; 128
129 [session_service() performSaveSessionData:session_data
130 sessionPath:session_path()];
131 131
132 SessionWindowIOS* session_window = 132 SessionWindowIOS* session_window =
133 [service loadWindowForBrowserState:chrome_browser_state()]; 133 [session_service() loadSessionWindowFromPath:session_path()];
134 EXPECT_TRUE(session_window != nil); 134 EXPECT_TRUE(session_window != nil);
135 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(session_window.selectedIndex)); 135 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(session_window.selectedIndex));
136 EXPECT_EQ(0U, session_window.sessions.count); 136 EXPECT_EQ(0U, session_window.sessions.count);
137 } 137 }
138 138
139 TEST_F(SessionServiceTest, LoadCorruptedWindow) { 139 TEST_F(SessionServiceTest, LoadCorruptedWindow) {
140 NSString* session_path =
141 SessionPathForTestData(FILE_PATH_LITERAL("corrupted.plist"));
142 ASSERT_NSNE(nil, session_path);
140 SessionWindowIOS* session_window = 143 SessionWindowIOS* session_window =
141 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("corrupted.plist")); 144 [session_service() loadSessionWindowFromPath:session_path];
142 EXPECT_TRUE(session_window == nil); 145 EXPECT_TRUE(session_window == nil);
143 } 146 }
144 147
145 // TODO(crbug.com/661633): remove this once M67 has shipped (i.e. once more 148 // TODO(crbug.com/661633): remove this once M67 has shipped (i.e. once more
146 // than a year has passed since the introduction of the compatibility code). 149 // than a year has passed since the introduction of the compatibility code).
147 TEST_F(SessionServiceTest, LoadM57Session) { 150 TEST_F(SessionServiceTest, LoadM57Session) {
151 NSString* session_path =
152 SessionPathForTestData(FILE_PATH_LITERAL("session_m57.plist"));
153 ASSERT_NSNE(nil, session_path);
148 SessionWindowIOS* session_window = 154 SessionWindowIOS* session_window =
149 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("session_m57.plist")); 155 [session_service() loadSessionWindowFromPath:session_path];
150 EXPECT_TRUE(session_window != nil); 156 EXPECT_TRUE(session_window != nil);
151 } 157 }
152 158
153 // TODO(crbug.com/661633): remove this once M68 has shipped (i.e. once more 159 // TODO(crbug.com/661633): remove this once M68 has shipped (i.e. once more
154 // than a year has passed since the introduction of the compatibility code). 160 // than a year has passed since the introduction of the compatibility code).
155 TEST_F(SessionServiceTest, LoadM58Session) { 161 TEST_F(SessionServiceTest, LoadM58Session) {
162 NSString* session_path =
163 SessionPathForTestData(FILE_PATH_LITERAL("session_m58.plist"));
164 ASSERT_NSNE(nil, session_path);
156 SessionWindowIOS* session_window = 165 SessionWindowIOS* session_window =
157 LoadSessionFromTestDataFile(FILE_PATH_LITERAL("session_m58.plist")); 166 [session_service() loadSessionWindowFromPath:session_path];
158 EXPECT_TRUE(session_window != nil); 167 EXPECT_TRUE(session_window != nil);
159 } 168 }
160 169
161 } // anonymous namespace 170 } // anonymous namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/sessions/session_service_ios.mm ('k') | ios/chrome/browser/sessions/session_util.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698