Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / common / common_type_converters_unittest.cc
1 // Copyright 2013 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 #include "mojo/common/common_type_converters.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/public/cpp/bindings/allocation_scope.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace mojo {
12 namespace common {
13 namespace test {
14 namespace {
15
16 void ExpectEqualsStringPiece(const std::string& expected,
17                              const base::StringPiece& str) {
18   EXPECT_EQ(expected, str.as_string());
19 }
20
21 void ExpectEqualsMojoString(const std::string& expected,
22                             const String& str) {
23   EXPECT_EQ(expected, str.To<std::string>());
24 }
25
26 void ExpectEqualsString16(const base::string16& expected,
27                           const base::string16& actual) {
28   EXPECT_EQ(expected, actual);
29 }
30
31 void ExpectEqualsMojoString(const base::string16& expected,
32                             const String& str) {
33   EXPECT_EQ(expected, str.To<base::string16>());
34 }
35
36 }  // namespace
37
38 TEST(CommonTypeConvertersTest, StringPiece) {
39   AllocationScope scope;
40
41   std::string kText("hello world");
42
43   base::StringPiece string_piece(kText);
44   String mojo_string(string_piece);
45
46   ExpectEqualsMojoString(kText, mojo_string);
47   ExpectEqualsStringPiece(kText, mojo_string.To<base::StringPiece>());
48
49   // Test implicit construction and conversion:
50   ExpectEqualsMojoString(kText, string_piece);
51   ExpectEqualsStringPiece(kText, mojo_string);
52
53   // Test null String:
54   base::StringPiece empty_string_piece = String();
55   EXPECT_TRUE(empty_string_piece.empty());
56 }
57
58 TEST(CommonTypeConvertersTest, String16) {
59   AllocationScope scope;
60
61   const base::string16 string16(base::ASCIIToUTF16("hello world"));
62   const String mojo_string(string16);
63
64   ExpectEqualsMojoString(string16, mojo_string);
65   EXPECT_EQ(string16, mojo_string.To<base::string16>());
66
67   // Test implicit construction and conversion:
68   ExpectEqualsMojoString(string16, string16);
69   ExpectEqualsString16(string16, mojo_string);
70
71   // Test empty string conversion.
72   ExpectEqualsMojoString(base::string16(), base::string16());
73 }
74
75 }  // namespace test
76 }  // namespace common
77 }  // namespace mojo