summaryrefslogtreecommitdiff
path: root/src/tests/elementary/spec/efl_test_format.c
blob: c3fa7d42f52b909d59a05ab05225e5a18d816f6a (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#ifdef _WIN32
# include <evil_private.h> /* setlocale strptime */
#endif

#define EFL_UI_FORMAT_PROTECTED /* To access internal methods */

#include <Efl_Ui.h>
#include "efl_ui_spec_suite.h"
#include "suite_helpers.h"

/* spec-meta-start
      {"test-interface":"Efl.Ui.Format",
       "test-widgets": ["Efl.Ui.Progressbar", "Efl.Ui.Calendar", "Efl.Ui.Tags", "Efl.Ui.Spin", "Efl.Ui.Spin_Button"]}
   spec-meta-end */

static const Efl_Ui_Format_Value values[] = {{15, "fifteen"}, {16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}};

EFL_START_TEST(format_values)
{
  Eina_Strbuf *buf = eina_strbuf_new();
  Eina_Value eina_val;

  efl_ui_format_values_set(widget, EINA_C_ARRAY_ACCESSOR_NEW(values));
  eina_val = eina_value_int_init(17);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "seventeen"); // Check that value works
  eina_val = eina_value_float_init(16.f);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "sixteen"); // Check built-in conversion

  eina_value_flush(&eina_val);
  eina_strbuf_free(buf);
}
EFL_END_TEST

static Eina_Bool
_format_func(void *data, Eina_Strbuf *str, const Eina_Value value)
{
  int i = *(int *)data;
  int v;
  ck_assert_int_eq(i, 1234); // Check that data ptr is passed along correctly
  if (eina_value_type_get(&value) != EINA_VALUE_TYPE_INT) return EINA_FALSE;
  eina_value_get(&value, &v);
  eina_strbuf_append_printf(str, "You said '%d'", v);

  return EINA_TRUE;
}

static void
_format_free_func(void *data)
{
  int i = *(int *)data;
  ck_assert_int_eq(i, 1234); // Check that data ptr is passed along correctly
  *(int *)data = 12345; // Change it to indicate that free func was called
}

EFL_START_TEST(format_func)
{
  int data = 1234;
  Eina_Strbuf *buf = eina_strbuf_new();
  Eina_Value eina_val;

  efl_ui_format_func_set(widget, &data, _format_func, _format_free_func);
  eina_val = eina_value_int_init(15);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "You said '15'"); // Check that format func works
  efl_ui_format_func_set(widget, NULL, NULL, NULL);
  ck_assert_int_eq(data, 12345); // Check that free func is called
  eina_value_flush(&eina_val);

  eina_strbuf_free(buf);
}
EFL_END_TEST

EFL_START_TEST(format_string)
{
  Eina_Strbuf *buf = eina_strbuf_new();
  Eina_Value eina_val;
  struct tm t = { 0 };
  const char *old_locale = setlocale(LC_TIME, NULL);

#define CHECK(fmt_str, fmt_type, val, val_type, result) \
  efl_ui_format_string_set(widget, fmt_str, fmt_type); \
  eina_value_setup(&eina_val, val_type); \
  eina_value_set(&eina_val, val); \
  efl_ui_format_formatted_value_get(widget, buf, eina_val); \
  ck_assert_str_eq(eina_strbuf_string_get(buf), result); \
  eina_value_flush(&eina_val)

  CHECK("%d", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234, EINA_VALUE_TYPE_INT, "1234");
  CHECK("%d", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234.f, EINA_VALUE_TYPE_FLOAT, "1234"); // built-in conversion
  CHECK("%d units", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234, EINA_VALUE_TYPE_INT, "1234 units"); // complex format

  CHECK("%.0f", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 12.34f, EINA_VALUE_TYPE_FLOAT, "12");
  CHECK("%.1f", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 12.34f, EINA_VALUE_TYPE_FLOAT, "12.3");
  CHECK("%.2f", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 12.34f, EINA_VALUE_TYPE_FLOAT, "12.34");
  CHECK("%.2f", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234, EINA_VALUE_TYPE_INT, "1234.00"); // built-in conversion
  CHECK("%.0f%%", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 75.f, EINA_VALUE_TYPE_FLOAT, "75%"); // complex format

  CHECK("%s", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, "Hello!", EINA_VALUE_TYPE_STRING, "Hello!");
  CHECK("%s", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234, EINA_VALUE_TYPE_INT, "1234"); // built-in conversion
  CHECK("He said '%s'", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, "Hello!", EINA_VALUE_TYPE_STRING, "He said 'Hello!'"); // complex format

  CHECK("Static string", EFL_UI_FORMAT_STRING_TYPE_SIMPLE, 1234, EINA_VALUE_TYPE_INT, "Static string");

  strptime("2019 7 3 11:49:3", "%Y %m %d %H:%M:%S", &t);
  setlocale(LC_TIME, "C");
  CHECK("%F", EFL_UI_FORMAT_STRING_TYPE_TIME, t, EINA_VALUE_TYPE_TM, "2019-07-03");
  CHECK("%T", EFL_UI_FORMAT_STRING_TYPE_TIME, t, EINA_VALUE_TYPE_TM, "11:49:03");
  CHECK("%A", EFL_UI_FORMAT_STRING_TYPE_TIME, t, EINA_VALUE_TYPE_TM, "Wednesday");
  CHECK("<%B %Y>", EFL_UI_FORMAT_STRING_TYPE_TIME, t, EINA_VALUE_TYPE_TM, "<July 2019>");
  setlocale(LC_TIME, old_locale);

  eina_strbuf_free(buf);
#undef CHECK
}
EFL_END_TEST

static Eina_Bool
_partial_format_func(void *data EINA_UNUSED, Eina_Strbuf *str, const Eina_Value value)
{
  int v;
  if (eina_value_type_get(&value) != EINA_VALUE_TYPE_INT) return EINA_FALSE;
  eina_value_get(&value, &v);
  if (v < 10)
     {
       eina_strbuf_append_printf(str, "You said '%d'", v);
       return EINA_TRUE;
     }
  return EINA_FALSE;
}

EFL_START_TEST(format_mixed)
{
  Eina_Strbuf *buf = eina_strbuf_new();
  Eina_Value eina_val;

  // Now we check combinations of different options
  // Each one should be used in turn when the previous one fails: values, func, string, fallback
  efl_ui_format_values_set(widget, EINA_C_ARRAY_ACCESSOR_NEW(values));
  efl_ui_format_func_set(widget, NULL, _partial_format_func, NULL);
  efl_ui_format_string_set(widget, "%d rabbits", EFL_UI_FORMAT_STRING_TYPE_SIMPLE);

  eina_val = eina_value_int_init(1);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "You said '1'"); // Func
  eina_value_set(&eina_val, 15);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "fifteen"); // Values
  eina_value_set(&eina_val, 25);
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "25 rabbits"); // Values

  EXPECT_ERROR_START;
  // This is an invalid format string (it has two placeholders) which should
  // trigger the fallback mechanism
  efl_ui_format_string_set(widget, "%d %d", EFL_UI_FORMAT_STRING_TYPE_SIMPLE);
  EXPECT_ERROR_END;
  efl_ui_format_formatted_value_get(widget, buf, eina_val);
  ck_assert_str_eq(eina_strbuf_string_get(buf), "25"); // Fallback

  eina_value_flush(&eina_val);
  eina_strbuf_free(buf);
}
EFL_END_TEST

void
efl_ui_format_behavior_test(TCase *tc)
{
   tcase_add_test(tc, format_values);
   tcase_add_test(tc, format_func);
   tcase_add_test(tc, format_string);
   tcase_add_test(tc, format_mixed);
}