1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 
7 #include <Eina.h>
8 
9 #include "eina_suite.h"
10 
EFL_START_TEST(eina_test_content_create_destroy)11 EFL_START_TEST(eina_test_content_create_destroy)
12 {
13    const char *text_str = "TestAsDf";
14    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str), "text/plain");
15 
16    ck_assert_str_eq(eina_content_type_get(content), "text/plain");
17    ck_assert_int_eq(eina_content_data_get(content).len, strlen(text_str) + 1);
18    ck_assert_str_eq(eina_content_data_get(content).mem, text_str);
19 
20    eina_content_free(content);
21 }
22 EFL_END_TEST
23 
EFL_START_TEST(eina_test_content_as_file)24 EFL_START_TEST(eina_test_content_as_file)
25 {
26    const char *text_str = "TestAsDf";
27    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str), "text/plain");
28    Eina_File *f;
29    const char *file_path = eina_content_as_file(content);
30 
31    f = eina_file_open(file_path, EINA_FALSE);
32    const char *file_content = eina_file_map_all(f, EINA_FILE_POPULATE);
33    ck_assert_str_eq(file_content, text_str);
34    eina_file_close(f);
35 
36    const char *file_path2 = eina_content_as_file(content);
37    ck_assert_str_eq(file_path, file_path2);
38 
39    eina_content_free(content);
40 }
41 EFL_END_TEST
42 
EFL_START_TEST(eina_test_content_convert_none_existing)43 EFL_START_TEST(eina_test_content_convert_none_existing)
44 {
45    const char *text_str = "TestAsDf";
46    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str), "text/plain");
47 
48    EXPECT_ERROR_START;
49    ck_assert_ptr_eq(eina_content_convert(content, "ThisIsReallyNotHere"), NULL);
50    EXPECT_ERROR_END;
51 }
52 EFL_END_TEST
53 
EFL_START_TEST(eina_test_content_convert_ascii_to_utf8)54 EFL_START_TEST(eina_test_content_convert_ascii_to_utf8)
55 {
56    const char *text_str = "TestAsDf";
57    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str), "text/plain");
58    Eina_Content *c = eina_content_convert(content, "text/plain;charset=utf-8");
59 
60    ck_assert_str_eq(eina_content_type_get(c), "text/plain;charset=utf-8");
61    ck_assert_str_eq(eina_content_data_get(c).mem, text_str);
62 }
63 EFL_END_TEST
64 
EFL_START_TEST(eina_test_content_convert_ascii_to_latin)65 EFL_START_TEST(eina_test_content_convert_ascii_to_latin)
66 {
67    const char *text_str = "TestAsDf";
68    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str), "text/plain");
69    Eina_Content *c = eina_content_convert(content, "text/plain;charset=iso-8859-1");
70 
71    ck_assert_str_eq(eina_content_type_get(c), "text/plain;charset=iso-8859-1");
72    ck_assert_str_eq(eina_content_data_get(c).mem, text_str);
73 }
74 EFL_END_TEST
75 
EFL_START_TEST(eina_test_content_convert_utf8_to_latin)76 EFL_START_TEST(eina_test_content_convert_utf8_to_latin)
77 {
78    //this means AÄÜÖß
79    const char text_str[] = {'A', 0xc3, 0x84, 0xc3, 0x9c, 0xc3, 0x96, 0xc3, 0x9f, 0};
80    const char text_str_latin[] = {'A', 0xC4, 0xDC, 0xD6, 0xDF, 0};
81    Eina_Content *content = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(text_str_latin), "text/plain;charset=iso-8859-1");
82    Eina_Content *c = eina_content_convert(content, "text/plain;charset=utf-8");
83 
84    ck_assert_str_eq(eina_content_type_get(c), "text/plain;charset=utf-8");
85    ck_assert_int_eq(sizeof(text_str), eina_content_data_get(c).len);
86    for (unsigned int i = 0; i < eina_content_data_get(c).len; ++i)
87      {
88         ck_assert_int_eq(text_str[i], ((char*)eina_content_data_get(c).mem)[i]);
89      }
90    ck_assert_str_eq(eina_content_data_get(c).mem, text_str);
91 }
92 EFL_END_TEST
93 
EFL_START_TEST(eina_test_content_possible_converstions)94 EFL_START_TEST(eina_test_content_possible_converstions)
95 {
96    ck_assert_int_eq(eina_content_converter_convert_can("text/plain", "text/plain;charset=utf-8"), 1);
97    ck_assert_int_eq(eina_content_converter_convert_can("text/plain", "ThisDoesNotExist"), 0);
98    ck_assert_int_eq(eina_content_converter_convert_can("ThisDoesNotExist", "text/plain;charset=utf-8"), 0);
99    Eina_Iterator *iterator = eina_content_converter_possible_conversions("text/plain");
100    char *text;
101    int i = 0, j = 0;
102 
103    EINA_ITERATOR_FOREACH(iterator, text)
104      {
105         if (eina_streq(text, "text/plain;charset=utf-8"))
106           i ++;
107         if (eina_streq(text, "text/plain;charset=iso-8859-1"))
108           j ++;
109      }
110    ck_assert_int_eq(i, 1);
111    ck_assert_int_eq(j, 1);
112    eina_iterator_free(iterator);
113 }
114 EFL_END_TEST
115 
116 static Eina_Bool getting_called = EINA_FALSE;
117 
118 static Eina_Content*
_test_cb(Eina_Content * content EINA_UNUSED,const char * type EINA_UNUSED)119 _test_cb(Eina_Content *content EINA_UNUSED, const char *type EINA_UNUSED)
120 {
121    getting_called = EINA_TRUE;
122    return eina_content_new(eina_content_data_get(content), type);
123 }
124 
EFL_START_TEST(eina_test_register_illegal)125 EFL_START_TEST(eina_test_register_illegal)
126 {
127    const char *test_str = "AbCdEfG";
128    eina_content_converter_conversion_register("Test", "Test2", _test_cb);
129    EXPECT_ERROR_START;
130    eina_content_converter_conversion_register("Test", "Test2", _test_cb);
131    EXPECT_ERROR_END;
132 
133    Eina_Content *c = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(test_str), "Test");
134    Eina_Content *c2 = eina_content_convert(c, "Test2");
135    ck_assert_ptr_ne(c2, NULL);
136    ck_assert_int_eq(getting_called, EINA_TRUE);
137 }
138 EFL_END_TEST
139 
EFL_START_TEST(eina_test_content_value)140 EFL_START_TEST(eina_test_content_value)
141 {
142    const char *str_a = "All";
143    const char *str_b = "Out";
144    Eina_Content *a = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_a), "text/plain");
145    Eina_Content *b = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_b), "text/plain");
146    Eina_Content *c = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_a), "text/plain");
147    Eina_Content *d = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_a), "Not_Text");
148    Eina_Value *va = eina_value_content_new(a);
149    Eina_Value *vb = eina_value_content_new(b);
150    Eina_Value *vc = eina_value_content_new(c);
151    Eina_Value *vd = eina_value_content_new(d);
152    Eina_Value *vcopy = eina_value_new(EINA_VALUE_TYPE_CONTENT);
153    Eina_Content *content;
154 
155    ck_assert_int_eq(eina_value_compare(va, vc), 0);
156    ck_assert_int_ne(eina_value_compare(va, vb), 0);
157    ck_assert_int_ne(eina_value_compare(va, vd), 0);
158    ck_assert_int_eq(eina_value_compare(vd, vd), 0);
159 
160    ck_assert_int_eq(eina_value_copy(va, vcopy), 1);
161    ck_assert_int_eq(eina_value_compare(va, vcopy), 0);
162 
163    content = eina_value_to_content(vcopy);
164    Eina_Slice slice = eina_content_data_get(content);
165    ck_assert_int_eq(slice.len, strlen(str_a) + 1);
166    ck_assert_str_eq(slice.mem, str_a);
167 
168    ck_assert_str_eq(eina_content_type_get(content), "text/plain");
169    eina_content_free(content);
170 }
171 EFL_END_TEST
172 
EFL_START_TEST(eina_test_content_value_set)173 EFL_START_TEST(eina_test_content_value_set)
174 {
175    const char *str_a = "All";
176    Eina_Content *a = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_a), "text/plain");
177    Eina_Value *acopy = eina_value_new(EINA_VALUE_TYPE_CONTENT);
178    Eina_Content *content;
179 
180    eina_value_set(acopy, a);
181    content = eina_value_to_content(acopy);
182    Eina_Slice slice = eina_content_data_get(content);
183    ck_assert_int_eq(slice.len, strlen(str_a) + 1);
184    ck_assert_str_eq(slice.mem, str_a);
185 
186    ck_assert_str_eq(eina_content_type_get(content), "text/plain");
187    eina_content_free(content);
188    eina_content_free(a);
189 }
190 EFL_END_TEST
191 
EFL_START_TEST(eina_test_content_value_convertion)192 EFL_START_TEST(eina_test_content_value_convertion)
193 {
194    const char *str_a = "All";
195    const char *str_b = "Out";
196    const char *str_c = "Life";
197    Eina_Content *a = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_a), "text/plain;charset=utf-8");
198    Eina_Content *b = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_b), "text/plain");
199    Eina_Content *c = eina_content_new((Eina_Slice)EINA_SLICE_STR_FULL(str_c), "application/x-elementary-markup");
200    Eina_Value *av = eina_value_content_new(a);
201    Eina_Value *bv = eina_value_content_new(b);
202    Eina_Value *cv = eina_value_content_new(c);
203 
204    ck_assert_str_eq(eina_value_to_string(av), str_a);
205    ck_assert_str_eq(eina_value_to_string(bv), str_b);
206    ck_assert_str_ne(eina_value_to_string(cv), str_c);
207 }
208 EFL_END_TEST
209 
210 void
eina_test_abstract_content(TCase * tc)211 eina_test_abstract_content(TCase *tc)
212 {
213    tcase_add_test(tc, eina_test_content_create_destroy);
214    tcase_add_test(tc, eina_test_content_as_file);
215    tcase_add_test(tc, eina_test_content_convert_none_existing);
216    tcase_add_test(tc, eina_test_content_convert_ascii_to_utf8);
217    tcase_add_test(tc, eina_test_content_convert_ascii_to_latin);
218    tcase_add_test(tc, eina_test_content_convert_utf8_to_latin);
219    tcase_add_test(tc, eina_test_content_possible_converstions);
220    tcase_add_test(tc, eina_test_register_illegal);
221    tcase_add_test(tc, eina_test_content_value);
222    tcase_add_test(tc, eina_test_content_value_set);
223    tcase_add_test(tc, eina_test_content_value_convertion);
224 }
225