OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "ios/web/public/serializable_user_data_manager.h" | 5 #import "ios/web/public/serializable_user_data_manager.h" |
6 | 6 |
7 #import "base/mac/scoped_nsobject.h" | |
8 #import "ios/web/public/test/fakes/test_web_state.h" | 7 #import "ios/web/public/test/fakes/test_web_state.h" |
9 #import "testing/gtest_mac.h" | 8 #import "testing/gtest_mac.h" |
10 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
11 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
12 namespace { | 15 namespace { |
13 // User Data and Key to use for tests. | 16 // User Data and Key to use for tests. |
14 NSString* const kTestUserData = @"TestUserData"; | 17 NSString* const kTestUserData = @"TestUserData"; |
15 NSString* const kTestUserDataKey = @"TestUserDataKey"; | 18 NSString* const kTestUserDataKey = @"TestUserDataKey"; |
16 } // namespace | 19 } // namespace |
17 | 20 |
18 class SerializableUserDataManagerTest : public PlatformTest { | 21 class SerializableUserDataManagerTest : public PlatformTest { |
19 protected: | 22 protected: |
20 // Convenience getter for the user data manager. | 23 // Convenience getter for the user data manager. |
21 web::SerializableUserDataManager* manager() { | 24 web::SerializableUserDataManager* manager() { |
(...skipping 11 matching lines...) Expand all Loading... |
33 } | 36 } |
34 | 37 |
35 // Tests that SerializableUserData can successfully encode and decode. | 38 // Tests that SerializableUserData can successfully encode and decode. |
36 TEST_F(SerializableUserDataManagerTest, EncodeDecode) { | 39 TEST_F(SerializableUserDataManagerTest, EncodeDecode) { |
37 // Create a SerializableUserData instance for the test data. | 40 // Create a SerializableUserData instance for the test data. |
38 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); | 41 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); |
39 std::unique_ptr<web::SerializableUserData> user_data = | 42 std::unique_ptr<web::SerializableUserData> user_data = |
40 manager()->CreateSerializableUserData(); | 43 manager()->CreateSerializableUserData(); |
41 | 44 |
42 // Archive the serializable user data. | 45 // Archive the serializable user data. |
43 base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); | 46 NSMutableData* data = [[NSMutableData alloc] init]; |
44 base::scoped_nsobject<NSKeyedArchiver> archiver( | 47 NSKeyedArchiver* archiver = |
45 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); | 48 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; |
46 user_data->Encode(archiver); | 49 user_data->Encode(archiver); |
47 [archiver finishEncoding]; | 50 [archiver finishEncoding]; |
48 | 51 |
49 // Create a new SerializableUserData by unarchiving. | 52 // Create a new SerializableUserData by unarchiving. |
50 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( | 53 NSKeyedUnarchiver* unarchiver = |
51 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); | 54 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; |
52 std::unique_ptr<web::SerializableUserData> decoded_data = | 55 std::unique_ptr<web::SerializableUserData> decoded_data = |
53 web::SerializableUserData::Create(); | 56 web::SerializableUserData::Create(); |
54 decoded_data->Decode(unarchiver); | 57 decoded_data->Decode(unarchiver); |
55 | 58 |
56 // Add the decoded user data to a new WebState and verify its contents. | 59 // Add the decoded user data to a new WebState and verify its contents. |
57 web::TestWebState decoded_web_state; | 60 web::TestWebState decoded_web_state; |
58 web::SerializableUserDataManager* decoded_manager = | 61 web::SerializableUserDataManager* decoded_manager = |
59 web::SerializableUserDataManager::FromWebState(&decoded_web_state); | 62 web::SerializableUserDataManager::FromWebState(&decoded_web_state); |
60 decoded_manager->AddSerializableUserData(decoded_data.get()); | 63 decoded_manager->AddSerializableUserData(decoded_data.get()); |
61 id decoded_value = | 64 id decoded_value = |
62 decoded_manager->GetValueForSerializationKey(kTestUserDataKey); | 65 decoded_manager->GetValueForSerializationKey(kTestUserDataKey); |
63 EXPECT_NSEQ(decoded_value, kTestUserData); | 66 EXPECT_NSEQ(decoded_value, kTestUserData); |
64 } | 67 } |
OLD | NEW |