| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/public/serializable_user_data_manager.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #import "ios/web/public/test/fakes/test_web_state.h" |
| 9 #import "testing/gtest_mac.h" |
| 10 #include "testing/platform_test.h" |
| 11 |
| 12 namespace { |
| 13 // User Data and Key to use for tests. |
| 14 NSString* const kTestUserData = @"TestUserData"; |
| 15 NSString* const kTestUserDataKey = @"TestUserDataKey"; |
| 16 } // namespace |
| 17 |
| 18 class SerializableUserDataManagerTest : public PlatformTest { |
| 19 protected: |
| 20 // Convenience getter for the user data manager. |
| 21 web::SerializableUserDataManager* manager() { |
| 22 return web::SerializableUserDataManager::FromWebState(&web_state_); |
| 23 } |
| 24 |
| 25 web::TestWebState web_state_; |
| 26 }; |
| 27 |
| 28 // Tests that serializable data can be successfully added and read. |
| 29 TEST_F(SerializableUserDataManagerTest, SetAndReadData) { |
| 30 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); |
| 31 id value = manager()->GetValueForSerializationKey(kTestUserDataKey); |
| 32 EXPECT_NSEQ(value, kTestUserData); |
| 33 } |
| 34 |
| 35 // Tests that SerializableUserData can successfully encode and decode. |
| 36 TEST_F(SerializableUserDataManagerTest, EncodeDecode) { |
| 37 // Create a SerializableUserData instance for the test data. |
| 38 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); |
| 39 std::unique_ptr<web::SerializableUserData> user_data = |
| 40 manager()->CreateSerializableUserData(); |
| 41 |
| 42 // Archive the serializable user data. |
| 43 base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); |
| 44 base::scoped_nsobject<NSKeyedArchiver> archiver( |
| 45 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); |
| 46 user_data->Encode(archiver); |
| 47 [archiver finishEncoding]; |
| 48 |
| 49 // Create a new SerializableUserData by unarchiving. |
| 50 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( |
| 51 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); |
| 52 std::unique_ptr<web::SerializableUserData> decoded_data = |
| 53 web::SerializableUserData::Create(); |
| 54 decoded_data->Decode(unarchiver); |
| 55 |
| 56 // Add the decoded user data to a new WebState and verify its contents. |
| 57 web::TestWebState decoded_web_state; |
| 58 web::SerializableUserDataManager* decoded_manager = |
| 59 web::SerializableUserDataManager::FromWebState(&decoded_web_state); |
| 60 decoded_manager->AddSerializableUserData(decoded_data.get()); |
| 61 id decoded_value = |
| 62 decoded_manager->GetValueForSerializationKey(kTestUserDataKey); |
| 63 EXPECT_NSEQ(decoded_value, kTestUserData); |
| 64 } |
| OLD | NEW |