1 // Copyright (c) 2012 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 <Foundation/Foundation.h> 6 7 #include "base/basictypes.h" 8 #include "base/mac/scoped_nsobject.h" 9 #include "chrome/common/mac/nscoder_util.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/platform_test.h" 12 13 namespace { 14 15 typedef PlatformTest NSCoderStdStringTest; 16 17 const char* testStrings[] = { 18 "Arf", 19 "", 20 "This is working", 21 "\n", 22 " ", 23 "Bang!\t\n" 24 }; 25 26 TEST_F(NSCoderStdStringTest, encodeDecode) { 27 for (size_t i = 0; i < arraysize(testStrings); ++i) { 28 NSMutableData *data = [NSMutableData data]; 29 30 base::scoped_nsobject<NSKeyedArchiver> archiver( 31 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); 32 nscoder_util::EncodeString(archiver, @"test", testStrings[i]); 33 [archiver finishEncoding]; 34 35 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( 36 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); 37 const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test"); 38 39 EXPECT_EQ(decoded, testStrings[i]); 40 } 41 } 42 43 TEST_F(NSCoderStdStringTest, decodeEmpty) { 44 NSMutableData *data = [NSMutableData data]; 45 46 base::scoped_nsobject<NSKeyedArchiver> archiver( 47 [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]); 48 [archiver finishEncoding]; 49 50 base::scoped_nsobject<NSKeyedUnarchiver> unarchiver( 51 [[NSKeyedUnarchiver alloc] initForReadingWithData:data]); 52 const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test"); 53 54 EXPECT_EQ(decoded, ""); 55 } 56 57 } // namespace 58