• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include <map>
6 #include <vector>
7 
8 #include "include/internal/cef_string.h"
9 #include "include/internal/cef_string_list.h"
10 #include "include/internal/cef_string_map.h"
11 #include "include/internal/cef_string_multimap.h"
12 #include "tests/gtest/include/gtest/gtest.h"
13 
14 // Test UTF8 strings.
TEST(StringTest,UTF8)15 TEST(StringTest, UTF8) {
16   CefStringUTF8 str1("Test String");
17   EXPECT_EQ(str1.length(), (size_t)11);
18   EXPECT_FALSE(str1.empty());
19   EXPECT_TRUE(str1.IsOwner());
20 
21   // Test equality.
22   CefStringUTF8 str2("Test String");
23   EXPECT_EQ(str1, str2);
24   EXPECT_LE(str1, str2);
25   EXPECT_GE(str1, str2);
26 
27   str2 = "Test Test";
28   EXPECT_LT(str1, str2);
29   EXPECT_GT(str2, str1);
30 
31   // When strings are the same but of unequal length, the longer string is
32   // greater.
33   str2 = "Test";
34   EXPECT_LT(str2, str1);
35   EXPECT_GT(str1, str2);
36 
37   // Test conversions.
38   str2 = str1.ToString();
39   EXPECT_EQ(str1, str2);
40   str2 = str1.ToWString();
41   EXPECT_EQ(str1, str2);
42 
43   // Test userfree assignment.
44   cef_string_userfree_utf8_t uf = str2.DetachToUserFree();
45   EXPECT_TRUE(uf != nullptr);
46   EXPECT_TRUE(str2.empty());
47   str2.AttachToUserFree(uf);
48   EXPECT_FALSE(str2.empty());
49   EXPECT_EQ(str1, str2);
50 }
51 
52 // Test UTF16 strings.
TEST(StringTest,UTF16)53 TEST(StringTest, UTF16) {
54   CefStringUTF16 str1("Test String");
55   EXPECT_EQ(str1.length(), (size_t)11);
56   EXPECT_FALSE(str1.empty());
57   EXPECT_TRUE(str1.IsOwner());
58 
59   // Test equality.
60   CefStringUTF16 str2("Test String");
61   EXPECT_EQ(str1, str2);
62   EXPECT_LE(str1, str2);
63   EXPECT_GE(str1, str2);
64 
65   str2 = "Test Test";
66   EXPECT_LT(str1, str2);
67   EXPECT_GT(str2, str1);
68 
69   // When strings are the same but of unequal length, the longer string is
70   // greater.
71   str2 = "Test";
72   EXPECT_LT(str2, str1);
73   EXPECT_GT(str1, str2);
74 
75   // Test conversions.
76   str2 = str1.ToString();
77   EXPECT_EQ(str1, str2);
78   str2 = str1.ToWString();
79   EXPECT_EQ(str1, str2);
80 
81   // Test userfree assignment.
82   cef_string_userfree_utf16_t uf = str2.DetachToUserFree();
83   EXPECT_TRUE(uf != nullptr);
84   EXPECT_TRUE(str2.empty());
85   str2.AttachToUserFree(uf);
86   EXPECT_FALSE(str2.empty());
87   EXPECT_EQ(str1, str2);
88 }
89 
90 // Test wide strings.
TEST(StringTest,Wide)91 TEST(StringTest, Wide) {
92   CefStringWide str1("Test String");
93   EXPECT_EQ(str1.length(), (size_t)11);
94   EXPECT_FALSE(str1.empty());
95   EXPECT_TRUE(str1.IsOwner());
96 
97   // Test equality.
98   CefStringWide str2("Test String");
99   EXPECT_EQ(str1, str2);
100   EXPECT_LE(str1, str2);
101   EXPECT_GE(str1, str2);
102 
103   str2 = "Test Test";
104   EXPECT_LT(str1, str2);
105   EXPECT_GT(str2, str1);
106 
107   // When strings are the same but of unequal length, the longer string is
108   // greater.
109   str2 = "Test";
110   EXPECT_LT(str2, str1);
111   EXPECT_GT(str1, str2);
112 
113   // Test conversions.
114   str2 = str1.ToString();
115   EXPECT_EQ(str1, str2);
116   str2 = str1.ToWString();
117   EXPECT_EQ(str1, str2);
118 
119   // Test userfree assignment.
120   cef_string_userfree_wide_t uf = str2.DetachToUserFree();
121   EXPECT_TRUE(uf != nullptr);
122   EXPECT_TRUE(str2.empty());
123   str2.AttachToUserFree(uf);
124   EXPECT_FALSE(str2.empty());
125   EXPECT_EQ(str1, str2);
126 }
127 
128 // Test std::u16string convertion to/from CefString types.
TEST(StringTest,string16)129 TEST(StringTest, string16) {
130   CefStringUTF8 str8("Test String 1"), str8b;
131   CefStringUTF16 str16("Test String 2"), str16b;
132   CefStringWide strwide("Test String 3"), strwideb;
133   std::u16string base_str;
134 
135   base_str = str8;
136   str8b = base_str;
137   EXPECT_EQ(str8, base_str);
138   EXPECT_EQ(str8, str8b);
139 
140   base_str = str16;
141   str16b = base_str;
142   EXPECT_EQ(str16, base_str);
143   EXPECT_EQ(str16, str16b);
144 
145   base_str = strwide;
146   strwideb = base_str;
147   EXPECT_EQ(strwide, base_str);
148   EXPECT_EQ(strwide, strwideb);
149 }
150 
151 // Test string lists.
TEST(StringTest,List)152 TEST(StringTest, List) {
153   typedef std::vector<CefString> ListType;
154   ListType list;
155   list.push_back("String 1");
156   list.push_back("String 2");
157   list.push_back("String 3");
158 
159   EXPECT_EQ(list[0], "String 1");
160   EXPECT_EQ(list[1], "String 2");
161   EXPECT_EQ(list[2], "String 3");
162 
163   cef_string_list_t listPtr = cef_string_list_alloc();
164   EXPECT_TRUE(listPtr != nullptr);
165   ListType::const_iterator it = list.begin();
166   for (; it != list.end(); ++it)
167     cef_string_list_append(listPtr, it->GetStruct());
168 
169   CefString str;
170   int ret;
171 
172   EXPECT_EQ(cef_string_list_size(listPtr), 3U);
173 
174   ret = cef_string_list_value(listPtr, 0U, str.GetWritableStruct());
175   EXPECT_TRUE(ret);
176   EXPECT_EQ(str, "String 1");
177   ret = cef_string_list_value(listPtr, 1U, str.GetWritableStruct());
178   EXPECT_TRUE(ret);
179   EXPECT_EQ(str, "String 2");
180   ret = cef_string_list_value(listPtr, 2U, str.GetWritableStruct());
181   EXPECT_TRUE(ret);
182   EXPECT_EQ(str, "String 3");
183 
184   cef_string_list_t listPtr2 = cef_string_list_copy(listPtr);
185   cef_string_list_clear(listPtr);
186   EXPECT_EQ(cef_string_list_size(listPtr), 0U);
187   cef_string_list_free(listPtr);
188 
189   EXPECT_EQ(cef_string_list_size(listPtr2), 3U);
190 
191   ret = cef_string_list_value(listPtr2, 0U, str.GetWritableStruct());
192   EXPECT_TRUE(ret);
193   EXPECT_EQ(str, "String 1");
194   ret = cef_string_list_value(listPtr2, 1U, str.GetWritableStruct());
195   EXPECT_TRUE(ret);
196   EXPECT_EQ(str, "String 2");
197   ret = cef_string_list_value(listPtr2, 2U, str.GetWritableStruct());
198   EXPECT_TRUE(ret);
199   EXPECT_EQ(str, "String 3");
200 
201   cef_string_list_free(listPtr2);
202 }
203 
204 // Test string maps.
TEST(StringTest,Map)205 TEST(StringTest, Map) {
206   typedef std::map<CefString, CefString> MapType;
207   MapType map;
208   map.insert(std::make_pair("Key 1", "String 1"));
209   map.insert(std::make_pair("Key 2", "String 2"));
210   map.insert(std::make_pair("Key 3", "String 3"));
211 
212   MapType::const_iterator it;
213 
214   it = map.find("Key 2");
215   EXPECT_TRUE(it != map.end());
216   EXPECT_EQ(it->first, "Key 2");
217   EXPECT_EQ(it->second, "String 2");
218 
219   it = map.find(L"Key 2");
220   EXPECT_TRUE(it != map.end());
221   EXPECT_EQ(it->first, L"Key 2");
222   EXPECT_EQ(it->second, L"String 2");
223 
224   EXPECT_EQ(map["Key 1"], "String 1");
225   EXPECT_EQ(map["Key 2"], "String 2");
226   EXPECT_EQ(map["Key 3"], "String 3");
227 
228   cef_string_map_t mapPtr = cef_string_map_alloc();
229 
230   it = map.begin();
231   for (; it != map.end(); ++it) {
232     cef_string_map_append(mapPtr, it->first.GetStruct(),
233                           it->second.GetStruct());
234   }
235 
236   CefString str;
237   int ret;
238 
239   EXPECT_EQ(cef_string_map_size(mapPtr), 3U);
240 
241   ret = cef_string_map_key(mapPtr, 0U, str.GetWritableStruct());
242   EXPECT_TRUE(ret);
243   EXPECT_EQ(str, "Key 1");
244   ret = cef_string_map_value(mapPtr, 0U, str.GetWritableStruct());
245   EXPECT_TRUE(ret);
246   EXPECT_EQ(str, "String 1");
247 
248   ret = cef_string_map_key(mapPtr, 1U, str.GetWritableStruct());
249   EXPECT_TRUE(ret);
250   EXPECT_EQ(str, "Key 2");
251   ret = cef_string_map_value(mapPtr, 1U, str.GetWritableStruct());
252   EXPECT_TRUE(ret);
253   EXPECT_EQ(str, "String 2");
254 
255   ret = cef_string_map_key(mapPtr, 2U, str.GetWritableStruct());
256   EXPECT_TRUE(ret);
257   EXPECT_EQ(str, "Key 3");
258   ret = cef_string_map_value(mapPtr, 2U, str.GetWritableStruct());
259   EXPECT_TRUE(ret);
260   EXPECT_EQ(str, "String 3");
261 
262   CefString key;
263   key.FromASCII("Key 2");
264   ret = cef_string_map_find(mapPtr, key.GetStruct(), str.GetWritableStruct());
265   EXPECT_TRUE(ret);
266   EXPECT_EQ(str, "String 2");
267 
268   cef_string_map_clear(mapPtr);
269   EXPECT_EQ(cef_string_map_size(mapPtr), 0U);
270 
271   cef_string_map_free(mapPtr);
272 }
273 
274 // Test string maps.
TEST(StringTest,Multimap)275 TEST(StringTest, Multimap) {
276   typedef std::multimap<CefString, CefString> MapType;
277   MapType map;
278   map.insert(std::make_pair("Key 1", "String 1"));
279   map.insert(std::make_pair("Key 2", "String 2"));
280   map.insert(std::make_pair("Key 2", "String 2.1"));
281   map.insert(std::make_pair("Key 3", "String 3"));
282 
283   MapType::const_iterator it;
284 
285   it = map.find("Key 2");
286   EXPECT_TRUE(it != map.end());
287   EXPECT_EQ(it->first, "Key 2");
288   EXPECT_EQ(it->second, "String 2");
289 
290   std::pair<MapType::const_iterator, MapType::const_iterator> range_it =
291       map.equal_range("Key 2");
292   EXPECT_TRUE(range_it.first != range_it.second);
293   MapType::const_iterator same_key_it = range_it.first;
294   // Either of "String 2" or "String 2.1" is fine since
295   // std::multimap provides no guarantee wrt the order of
296   // values with the same key.
297   EXPECT_EQ(same_key_it->second.ToString().find("String 2"), 0U);
298   EXPECT_EQ((++same_key_it)->second.ToString().find("String 2"), 0U);
299   EXPECT_EQ(map.count("Key 2"), 2U);
300 
301   EXPECT_EQ(map.find("Key 1")->second, "String 1");
302   EXPECT_EQ(map.find("Key 3")->second, "String 3");
303 
304   cef_string_multimap_t mapPtr = cef_string_multimap_alloc();
305 
306   it = map.begin();
307   for (; it != map.end(); ++it) {
308     cef_string_multimap_append(mapPtr, it->first.GetStruct(),
309                                it->second.GetStruct());
310   }
311 
312   CefString str;
313   int ret;
314 
315   EXPECT_EQ(cef_string_multimap_size(mapPtr), 4U);
316 
317   ret = cef_string_multimap_key(mapPtr, 0U, str.GetWritableStruct());
318   EXPECT_TRUE(ret);
319   EXPECT_EQ(str, "Key 1");
320   ret = cef_string_multimap_value(mapPtr, 0U, str.GetWritableStruct());
321   EXPECT_TRUE(ret);
322   EXPECT_EQ(str, "String 1");
323 
324   ret = cef_string_multimap_key(mapPtr, 1U, str.GetWritableStruct());
325   EXPECT_TRUE(ret);
326   EXPECT_EQ(str, "Key 2");
327   ret = cef_string_multimap_value(mapPtr, 1U, str.GetWritableStruct());
328   EXPECT_TRUE(ret);
329   EXPECT_EQ(str.ToString().find("String 2"), 0U);
330 
331   ret = cef_string_multimap_key(mapPtr, 2U, str.GetWritableStruct());
332   EXPECT_TRUE(ret);
333   EXPECT_EQ(str, "Key 2");
334   ret = cef_string_multimap_value(mapPtr, 2U, str.GetWritableStruct());
335   EXPECT_TRUE(ret);
336   EXPECT_EQ(str.ToString().find("String 2"), 0U);
337 
338   ret = cef_string_multimap_key(mapPtr, 3U, str.GetWritableStruct());
339   EXPECT_TRUE(ret);
340   EXPECT_EQ(str, "Key 3");
341   ret = cef_string_multimap_value(mapPtr, 3U, str.GetWritableStruct());
342   EXPECT_TRUE(ret);
343   EXPECT_EQ(str, "String 3");
344 
345   CefString key;
346   key.FromASCII("Key 2");
347   size_t size = cef_string_multimap_find_count(mapPtr, key.GetStruct());
348   EXPECT_EQ(size, 2U);
349 
350   ret = cef_string_multimap_enumerate(mapPtr, key.GetStruct(), 0U,
351                                       str.GetWritableStruct());
352   EXPECT_TRUE(ret);
353   EXPECT_EQ(str.ToString().find("String 2"), 0U);
354 
355   ret = cef_string_multimap_enumerate(mapPtr, key.GetStruct(), 1U,
356                                       str.GetWritableStruct());
357   EXPECT_TRUE(ret);
358   EXPECT_EQ(str.ToString().find("String 2"), 0U);
359 
360   cef_string_multimap_clear(mapPtr);
361   EXPECT_EQ(cef_string_multimap_size(mapPtr), 0U);
362 
363   cef_string_multimap_free(mapPtr);
364 }
365