Chromium Code Reviews| 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 protected: | |
|
Eugene But (OOO till 7-30)
2017/02/11 02:46:48
nit: you don't need this
kkhorimoto
2017/02/11 03:19:49
Done.
| |
| 26 web::TestWebState web_state_; | |
| 27 }; | |
| 28 | |
| 29 // Tests that serializable data can be successfully added and read. | |
| 30 TEST_F(SerializableUserDataManagerTest, SetAndReadData) { | |
| 31 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); | |
| 32 id value = manager()->GetValueForSerializationKey(kTestUserDataKey); | |
| 33 EXPECT_NSEQ(value, kTestUserData); | |
| 34 } | |
| 35 | |
| 36 // Tests that SerializableUserData can successfully encode and decode. | |
| 37 TEST_F(SerializableUserDataManagerTest, EncodeDecode) { | |
| 38 // Create a SerializableUserData instance for the test data. | |
| 39 manager()->AddSerializableData(kTestUserData, kTestUserDataKey); | |
| 40 std::unique_ptr<web::SerializableUserData> user_data = | |
| 41 manager()->CreateSerializableUserData(); | |
| 42 | |
| 43 // Archive the serializable user data. | |
| 44 base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]); | |
| 45 base::scoped_nsobject<NSKeyedArchiver> archiver( | |
| 46 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); | |
| 47 user_data->Encode(archiver); | |
| 48 [archiver finishEncoding]; | |
| 49 | |
| 50 // Create a new SerializableUserData by unarchiving. | |
| 51 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( | |
| 52 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); | |
| 53 std::unique_ptr<web::SerializableUserData> decoded_data = | |
| 54 web::SerializableUserData::Create(); | |
| 55 decoded_data->Decode(unarchiver); | |
| 56 | |
| 57 // Add the decoded user data to a new WebState and verify its contents. | |
| 58 web::TestWebState decoded_web_state; | |
| 59 web::SerializableUserDataManager* decoded_manager = | |
| 60 web::SerializableUserDataManager::FromWebState(&decoded_web_state); | |
| 61 decoded_manager->AddSerializableUserData(decoded_data.get()); | |
| 62 id decoded_value = | |
| 63 decoded_manager->GetValueForSerializationKey(kTestUserDataKey); | |
| 64 EXPECT_NSEQ(decoded_value, kTestUserData); | |
| 65 } | |
| OLD | NEW |