summaryrefslogtreecommitdiffstats
path: root/ios/web/navigation/nscoder_util_unittest.mm
blob: 8d3b667e618759ec36e7f9f92191ade94a7cf7d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Foundation/Foundation.h>

#include "base/basictypes.h"
#include "base/mac/scoped_nsobject.h"
#include "ios/web/navigation/nscoder_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

namespace web {
namespace {

typedef PlatformTest NSCoderStdStringTest;

const char* testStrings[] = {
    "Arf",
    "",
    "This is working™",
    "古池や蛙飛込む水の音\nふるいけやかわずとびこむみずのおと",
    "ἀγεωμέτρητος μηδεὶς εἰσίτω",
    "Bang!\t\n"
};

TEST_F(NSCoderStdStringTest, encodeDecode) {
  for (size_t i = 0; i < arraysize(testStrings); ++i) {
    NSMutableData* data = [NSMutableData data];

    base::scoped_nsobject<NSKeyedArchiver> archiver(
        [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]);
    nscoder_util::EncodeString(archiver, @"test", testStrings[i]);
    [archiver finishEncoding];

    base::scoped_nsobject<NSKeyedUnarchiver> unarchiver(
        [[NSKeyedUnarchiver alloc] initForReadingWithData:data]);
    const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test");

    EXPECT_EQ(decoded, testStrings[i]);
  }
}

TEST_F(NSCoderStdStringTest, decodeEmpty) {
  NSMutableData* data = [NSMutableData data];

  base::scoped_nsobject<NSKeyedArchiver> archiver(
      [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]);
  [archiver finishEncoding];

  base::scoped_nsobject<NSKeyedUnarchiver> unarchiver(
      [[NSKeyedUnarchiver alloc] initForReadingWithData:data]);
  const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test");

  EXPECT_EQ(decoded, "");
}

}  // namespace
}  // namespace web