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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 12 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 13 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
13 #include "ios/chrome/browser/crash_report/crash_restore_helper.h" | 14 #include "ios/chrome/browser/crash_report/crash_restore_helper.h" |
14 #import "ios/chrome/browser/sessions/session_service_ios.h" | 15 #import "ios/chrome/browser/sessions/session_service_ios.h" |
15 #include "ios/web/public/test/test_web_thread_bundle.h" | 16 #include "ios/web/public/test/test_web_thread_bundle.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
19 #import "third_party/ocmock/OCMock/OCMock.h" | 20 #import "third_party/ocmock/OCMock/OCMock.h" |
20 #include "third_party/ocmock/gtest_support.h" | 21 #include "third_party/ocmock/gtest_support.h" |
21 | 22 |
22 #if !defined(__has_feature) || !__has_feature(objc_arc) | 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
23 #error "This file requires ARC support." | 24 #error "This file requires ARC support." |
24 #endif | 25 #endif |
25 | 26 |
26 using testing::Return; | 27 using testing::Return; |
27 | 28 |
28 @interface CrashRestoreHelper (Test) | 29 @interface CrashRestoreHelper (Test) |
29 - (NSString*)sessionBackupPath; | 30 - (NSString*)sessionBackupPath; |
30 @end | 31 @end |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 class CrashRestoreHelperTest : public PlatformTest { | 35 class CrashRestoreHelperTest : public PlatformTest { |
35 public: | 36 public: |
36 void SetUp() override { | 37 CrashRestoreHelperTest() { |
37 TestChromeBrowserState::Builder test_cbs_builder; | 38 TestChromeBrowserState::Builder test_cbs_builder; |
38 chrome_browser_state_ = test_cbs_builder.Build(); | 39 chrome_browser_state_ = test_cbs_builder.Build(); |
39 otr_chrome_browser_state_ = | 40 off_the_record_chrome_browser_state_ = |
40 chrome_browser_state_->GetOffTheRecordChromeBrowserState(); | 41 chrome_browser_state_->GetOffTheRecordChromeBrowserState(); |
41 helper_ = [[CrashRestoreHelper alloc] | 42 helper_ = [[CrashRestoreHelper alloc] |
42 initWithBrowserState:chrome_browser_state_.get()]; | 43 initWithBrowserState:chrome_browser_state_.get()]; |
43 } | 44 } |
44 | 45 |
45 protected: | 46 protected: |
46 web::TestWebThreadBundle thread_bundle_; | 47 web::TestWebThreadBundle thread_bundle_; |
47 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 48 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
48 ios::ChromeBrowserState* otr_chrome_browser_state_; | 49 ios::ChromeBrowserState* off_the_record_chrome_browser_state_; |
49 CrashRestoreHelper* helper_; | 50 CrashRestoreHelper* helper_; |
50 }; | 51 }; |
51 | 52 |
52 TEST_F(CrashRestoreHelperTest, MoveAsideTest) { | 53 TEST_F(CrashRestoreHelperTest, MoveAsideTest) { |
53 NSString* backupPath = [helper_ sessionBackupPath]; | 54 NSString* backup_path = [helper_ sessionBackupPath]; |
54 NSFileManager* fileManager = [NSFileManager defaultManager]; | 55 NSFileManager* file_manager = [NSFileManager defaultManager]; |
55 [fileManager removeItemAtPath:backupPath error:NULL]; | 56 [file_manager removeItemAtPath:backup_path error:NULL]; |
56 | 57 |
57 NSData* data = [NSData dataWithBytes:"hello" length:5]; | 58 NSData* data = [NSData dataWithBytes:"hello" length:5]; |
58 SessionServiceIOS* sessionService = [SessionServiceIOS sharedService]; | 59 ios::ChromeBrowserState* browser_states[] = { |
59 NSString* profileStashPath = | 60 chrome_browser_state_.get(), off_the_record_chrome_browser_state_, |
60 base::SysUTF8ToNSString(chrome_browser_state_->GetStatePath().value()); | 61 }; |
61 NSString* sessionPath = | 62 |
62 [sessionService sessionFilePathForDirectory:profileStashPath]; | 63 for (size_t index = 0; index < arraysize(browser_states); ++index) { |
63 [fileManager createFileAtPath:sessionPath contents:data attributes:nil]; | 64 NSString* state_path = |
64 NSString* otrProfileStashPath = base::SysUTF8ToNSString( | 65 base::SysUTF8ToNSString(browser_states[index]->GetStatePath().value()); |
65 otr_chrome_browser_state_->GetStatePath().value()); | 66 NSString* session_path = |
66 NSString* otrSessionPath = | 67 [SessionServiceIOS sessionPathForDirectory:state_path]; |
67 [sessionService sessionFilePathForDirectory:otrProfileStashPath]; | 68 [file_manager createFileAtPath:session_path contents:data attributes:nil]; |
68 [fileManager createFileAtPath:otrSessionPath contents:data attributes:nil]; | 69 ASSERT_EQ(YES, [file_manager fileExistsAtPath:session_path]); |
| 70 } |
69 | 71 |
70 [helper_ moveAsideSessionInformation]; | 72 [helper_ moveAsideSessionInformation]; |
71 | 73 |
72 EXPECT_EQ(NO, [fileManager fileExistsAtPath:sessionPath]); | 74 for (size_t index = 0; index < arraysize(browser_states); ++index) { |
73 EXPECT_EQ(NO, [fileManager fileExistsAtPath:otrSessionPath]); | 75 NSString* state_path = |
74 EXPECT_EQ(YES, [fileManager fileExistsAtPath:backupPath]); | 76 base::SysUTF8ToNSString(browser_states[index]->GetStatePath().value()); |
75 [fileManager removeItemAtPath:backupPath error:NULL]; | 77 NSString* session_path = |
| 78 [SessionServiceIOS sessionPathForDirectory:state_path]; |
| 79 EXPECT_EQ(NO, [file_manager fileExistsAtPath:session_path]); |
| 80 } |
| 81 |
| 82 EXPECT_EQ(YES, [file_manager fileExistsAtPath:backup_path]); |
| 83 [file_manager removeItemAtPath:backup_path error:NULL]; |
76 } | 84 } |
77 | 85 |
78 } // namespace | 86 } // namespace |
OLD | NEW |