OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <assert.h> | 5 #include <assert.h> |
6 #include <glib.h> | 6 #include <glib.h> |
7 #include <ibus.h> | 7 #include <ibus.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 const gchar kDummySection[] = "aaa/bbb"; | 14 const gchar kDummySection[] = "aaa/bbb"; |
14 const gchar kDummyConfigName[] = "ccc"; | 15 const gchar kDummyConfigName[] = "ccc"; |
15 | 16 |
16 const gboolean kDummyValueBoolean = TRUE; | 17 const gboolean kDummyValueBoolean = TRUE; |
17 const gint kDummyValueInt = 12345; | 18 const gint kDummyValueInt = 12345; |
18 const gdouble kDummyValueDouble = 12345.54321; | 19 const gdouble kDummyValueDouble = 2345.5432; |
19 const gchar kDummyValueString[] = "dummy value"; | 20 const gchar kDummyValueString[] = "dummy value"; |
20 | 21 |
| 22 const size_t kArraySize = 3; |
| 23 const gboolean kDummyValueBooleanArray[kArraySize] = { FALSE, TRUE, FALSE }; |
| 24 const gint kDummyValueIntArray[kArraySize] = { 123, 234, 345 }; |
| 25 const gdouble kDummyValueDoubleArray[kArraySize] = { 111.22, 333.44, 555.66 }; |
| 26 const gchar* kDummyValueStringArray[kArraySize] = { |
| 27 "DUMMY_VALUE 1", "DUMMY_VALUE 2", "DUMMY_VALUE 3", |
| 28 }; |
| 29 |
| 30 // Converts |list_type_string| into its element type (e.g. "int_list" to "int"). |
| 31 std::string GetElementType(const std::string& list_type_string) { |
| 32 const std::string suffix = "_list"; |
| 33 if (list_type_string.length() > suffix.length()) { |
| 34 return list_type_string.substr( |
| 35 0, list_type_string.length() - suffix.length()); |
| 36 } |
| 37 return list_type_string; |
| 38 } |
| 39 |
| 40 // Converts |type_string| into GType. |
| 41 GType GetGValueTypeFromStringOrDie(const std::string& type_string) { |
| 42 if (type_string == "boolean") { |
| 43 return G_TYPE_BOOLEAN; |
| 44 } else if (type_string == "int") { |
| 45 return G_TYPE_INT; |
| 46 } else if (type_string == "double") { |
| 47 return G_TYPE_DOUBLE; |
| 48 } else if (type_string == "string") { |
| 49 return G_TYPE_STRING; |
| 50 } else if (GetElementType(type_string) != type_string) { |
| 51 return G_TYPE_VALUE_ARRAY; |
| 52 } |
| 53 printf("FAIL (unknown type: %s)\n", type_string.c_str()); |
| 54 abort(); |
| 55 } |
| 56 |
21 // Unsets a dummy value from ibus config service. | 57 // Unsets a dummy value from ibus config service. |
22 void UnsetConfigAndPrintResult(IBusConfig* ibus_config) { | 58 void UnsetConfigAndPrintResult(IBusConfig* ibus_config) { |
23 if (ibus_config_unset(ibus_config, kDummySection, kDummyConfigName)) { | 59 if (ibus_config_unset(ibus_config, kDummySection, kDummyConfigName)) { |
24 printf("OK\n"); | 60 printf("OK\n"); |
25 } else { | 61 } else { |
26 printf("FAIL\n"); | 62 printf("FAIL\n"); |
27 } | 63 } |
28 } | 64 } |
29 | 65 |
30 // Sets a dummy value to ibus config service. You can specify a type of the | 66 // Sets a dummy value to ibus config service. You can specify a type of the |
31 // dummy value by |type_string|. "boolean", "int", "double", or "string" are | 67 // dummy value by |type_string|. "boolean", "int", "double", or "string" are |
32 // allowed. | 68 // allowed. |
33 void SetConfigAndPrintResult( | 69 void SetConfigAndPrintResult( |
34 IBusConfig* ibus_config, const std::string& type_string) { | 70 IBusConfig* ibus_config, const std::string& type_string) { |
35 GValue gvalue = {0}; | 71 GValue gvalue = {0}; |
36 | 72 |
37 if (type_string == "boolean") { | 73 const GType gtype = GetGValueTypeFromStringOrDie(type_string); |
38 g_value_init(&gvalue, G_TYPE_BOOLEAN); | 74 g_value_init(&gvalue, gtype); |
| 75 if (gtype == G_TYPE_BOOLEAN) { |
39 g_value_set_boolean(&gvalue, kDummyValueBoolean); | 76 g_value_set_boolean(&gvalue, kDummyValueBoolean); |
40 } else if (type_string == "int") { | 77 } else if (gtype == G_TYPE_INT) { |
41 g_value_init(&gvalue, G_TYPE_INT); | |
42 g_value_set_int(&gvalue, kDummyValueInt); | 78 g_value_set_int(&gvalue, kDummyValueInt); |
43 } else if (type_string == "double") { | 79 } else if (gtype == G_TYPE_DOUBLE) { |
44 g_value_init(&gvalue, G_TYPE_DOUBLE); | |
45 g_value_set_double(&gvalue, kDummyValueDouble); | 80 g_value_set_double(&gvalue, kDummyValueDouble); |
46 } else if (type_string == "string") { | 81 } else if (gtype == G_TYPE_STRING) { |
47 g_value_init(&gvalue, G_TYPE_STRING); | |
48 g_value_set_string(&gvalue, kDummyValueString); | 82 g_value_set_string(&gvalue, kDummyValueString); |
49 } else { | 83 } else if (gtype == G_TYPE_VALUE_ARRAY) { |
50 printf("FAIL (unknown type: %s)\n", type_string.c_str()); | 84 // Process list types. |
51 return; | 85 GValueArray* array = g_value_array_new(kArraySize); |
52 } | 86 |
| 87 const GType element_gtype |
| 88 = GetGValueTypeFromStringOrDie(GetElementType(type_string)); |
| 89 g_assert(element_gtype != G_TYPE_VALUE_ARRAY); |
| 90 |
| 91 for (size_t i = 0; i < kArraySize; ++i) { |
| 92 GValue tmp = {0}; |
| 93 g_value_init(&tmp, element_gtype); |
| 94 if (element_gtype == G_TYPE_BOOLEAN) { |
| 95 g_value_set_boolean(&tmp, kDummyValueBooleanArray[i]); |
| 96 } else if (element_gtype == G_TYPE_INT) { |
| 97 g_value_set_int(&tmp, kDummyValueIntArray[i]); |
| 98 } else if (element_gtype == G_TYPE_DOUBLE) { |
| 99 g_value_set_double(&tmp, kDummyValueDoubleArray[i]); |
| 100 } else if (element_gtype == G_TYPE_STRING) { |
| 101 g_value_set_string(&tmp, kDummyValueStringArray[i]); |
| 102 } |
| 103 g_value_array_append(array, &tmp); |
| 104 } |
| 105 |
| 106 g_value_take_boxed(&gvalue, array); |
| 107 } |
53 | 108 |
54 if (ibus_config_set_value( | 109 if (ibus_config_set_value( |
55 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { | 110 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
56 printf("OK\n"); | 111 printf("OK\n"); |
57 } else { | 112 } else { |
58 printf("FAIL\n"); | 113 printf("FAIL\n"); |
59 } | 114 } |
60 } | 115 } |
61 | 116 |
62 // Gets a dummy value from ibus config service. This function checks if the | 117 // Gets a dummy value from ibus config service. This function checks if the |
63 // dummy value is |type_string| type. | 118 // dummy value is |type_string| type. |
64 void GetConfigAndPrintResult( | 119 void GetConfigAndPrintResult( |
65 IBusConfig* ibus_config, const std::string& type_string) { | 120 IBusConfig* ibus_config, const std::string& type_string) { |
66 GValue gvalue = {0}; | 121 GValue gvalue = {0}; |
67 if (!ibus_config_get_value( | 122 if (!ibus_config_get_value( |
68 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { | 123 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
69 printf("FAIL (not found)\n"); | 124 printf("FAIL (not found)\n"); |
70 return; | 125 return; |
71 } | 126 } |
72 | 127 |
73 if (type_string == "boolean") { | 128 const GType gtype = GetGValueTypeFromStringOrDie(type_string); |
74 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_BOOLEAN) || | 129 if (G_VALUE_TYPE(&gvalue) != gtype) { |
75 (g_value_get_boolean(&gvalue) != kDummyValueBoolean)) { | 130 printf("FAIL (type mismatch)\n"); |
76 printf("FAIL (type/value mismatch)\n"); | 131 return; |
| 132 } |
| 133 |
| 134 if (gtype== G_TYPE_BOOLEAN) { |
| 135 if (g_value_get_boolean(&gvalue) != kDummyValueBoolean) { |
| 136 printf("FAIL (value mismatch)\n"); |
77 return; | 137 return; |
78 } | 138 } |
79 } else if (type_string == "int") { | 139 } else if (gtype == G_TYPE_INT) { |
80 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_INT) || | 140 if (g_value_get_int(&gvalue) != kDummyValueInt) { |
81 (g_value_get_int(&gvalue) != kDummyValueInt)) { | 141 printf("FAIL (value mismatch)\n"); |
82 printf("FAIL (type/value mismatch)\n"); | |
83 return; | 142 return; |
84 } | 143 } |
85 } else if (type_string == "double") { | 144 } else if (gtype == G_TYPE_DOUBLE) { |
86 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_DOUBLE) || | 145 if (g_value_get_double(&gvalue) != kDummyValueDouble) { |
87 // We allow errors for double values. | 146 // Note: ibus-gconf does not pass this test since it converts a double |
88 (g_value_get_double(&gvalue) < kDummyValueDouble - 0.001) || | 147 // value into string to store it on GConf storage. If you want to use |
89 (g_value_get_double(&gvalue) > kDummyValueDouble + 0.001)) { | 148 // desktopui_IBusTest against ibus-gconf, you have to rewrite the |
90 printf("FAIL (type/value mismatch)\n"); | 149 // condition to allow errors. |
| 150 printf("FAIL (value mismatch)\n"); |
91 return; | 151 return; |
92 } | 152 } |
93 } else if (type_string == "string") { | 153 } else if (gtype == G_TYPE_STRING) { |
94 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_STRING) || | 154 if (g_value_get_string(&gvalue) != std::string(kDummyValueString)) { |
95 (g_value_get_string(&gvalue) != std::string(kDummyValueString))) { | 155 printf("FAIL (value mismatch)\n"); |
96 printf("FAIL (type/value mismatch)\n"); | |
97 return; | 156 return; |
98 } | 157 } |
99 } else { | 158 } else if (gtype == G_TYPE_VALUE_ARRAY) { |
100 printf("FAIL (unknown type: %s)\n", type_string.c_str()); | 159 // Process list types |
101 return; | 160 GValueArray* array |
| 161 = reinterpret_cast<GValueArray*>(g_value_get_boxed(&gvalue)); |
| 162 if (!array || (array->n_values != kArraySize)) { |
| 163 printf("FAIL (invalid array)\n"); |
| 164 return; |
| 165 } |
| 166 |
| 167 const GType element_gtype |
| 168 = GetGValueTypeFromStringOrDie(GetElementType(type_string)); |
| 169 g_assert(element_gtype != G_TYPE_VALUE_ARRAY); |
| 170 |
| 171 for (size_t i = 0; i < kArraySize; ++i) { |
| 172 const GValue* element = &(array->values[i]); |
| 173 if (G_VALUE_TYPE(element) != element_gtype) { |
| 174 printf("FAIL (list type mismatch)\n"); |
| 175 return; |
| 176 } |
| 177 bool match = false; |
| 178 if ((element_gtype == G_TYPE_BOOLEAN) && |
| 179 (g_value_get_boolean(element) == kDummyValueBooleanArray[i])) { |
| 180 match = true; |
| 181 } else if ((element_gtype == G_TYPE_INT) && |
| 182 (g_value_get_int(element) == kDummyValueIntArray[i])) { |
| 183 match = true; |
| 184 } else if ((element_gtype == G_TYPE_DOUBLE) && |
| 185 (g_value_get_double(element) == kDummyValueDoubleArray[i])) { |
| 186 // See my comment about ibus-gconf above. |
| 187 match = true; |
| 188 } else if ((element_gtype == G_TYPE_STRING) && |
| 189 (g_value_get_string(element) |
| 190 == std::string(kDummyValueStringArray[i]))) { |
| 191 match = true; |
| 192 } |
| 193 if (!match) { |
| 194 printf("FAIL (value mismatch)\n"); |
| 195 return; |
| 196 } |
| 197 } |
102 } | 198 } |
103 | 199 |
104 printf("OK\n"); | 200 printf("OK\n"); |
105 } | 201 } |
106 | 202 |
107 // Prints the names of the given engines. Takes the ownership of |engines|. | 203 // Prints the names of the given engines. Takes the ownership of |engines|. |
108 void PrintEngineNames(GList* engines) { | 204 void PrintEngineNames(GList* engines) { |
109 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { | 205 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { |
110 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); | 206 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); |
111 assert(engine_desc); | 207 assert(engine_desc); |
112 printf("%s\n", engine_desc->name); | 208 printf("%s\n", engine_desc->name); |
113 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); | 209 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); |
114 } | 210 } |
115 g_list_free(engines); | 211 g_list_free(engines); |
116 } | 212 } |
117 | 213 |
118 void PrintUsage(const char* argv0) { | 214 void PrintUsage(const char* argv0) { |
119 printf("Usage: %s COMMAND\n", argv0); | 215 printf("Usage: %s COMMAND\n", argv0); |
120 printf("check_reachable Check if ibus-daemon is reachable\n"); | 216 printf("check_reachable Check if ibus-daemon is reachable\n"); |
121 printf("list_engines List engine names (all engines)\n"); | 217 printf("list_engines List engine names (all engines)\n"); |
122 printf("list_active_engines List active engine names\n"); | 218 printf("list_active_engines List active engine names\n"); |
123 // TODO(yusukes): Add tests for array of {bool, int, double, string}. | |
124 // TODO(yusukes): Add 2 parameters, config_key and config_value, to | 219 // TODO(yusukes): Add 2 parameters, config_key and config_value, to |
125 // set_config and get_config commands. | 220 // set_config and get_config commands. |
126 printf("set_config (boolean|int|double|string)\n" | 221 printf("set_config (boolean|int|double|string|\n" |
| 222 " boolean_list|int_list|double_list|string_list)\n" |
127 " Set a dummy value to ibus config service\n"); | 223 " Set a dummy value to ibus config service\n"); |
128 printf("get_config (boolean|int|double|string)\n" | 224 printf("get_config (boolean|int|double|string\n" |
| 225 " boolean_list|int_list|double_list|string_list)\n" |
129 " Get a dummy value from ibus config service\n"); | 226 " Get a dummy value from ibus config service\n"); |
130 // TODO(yusukes): Add config_key parameter. | 227 // TODO(yusukes): Add config_key parameter to unset_config. |
131 printf("unset_config Unset a dummy value from ibus config service\n"); | 228 printf("unset_config Unset a dummy value from ibus config service\n"); |
132 } | 229 } |
133 | 230 |
134 } // namespace | 231 } // namespace |
135 | 232 |
136 int main(int argc, char **argv) { | 233 int main(int argc, char **argv) { |
137 if (argc == 1) { | 234 if (argc == 1) { |
138 PrintUsage(argv[0]); | 235 PrintUsage(argv[0]); |
139 return 1; | 236 return 1; |
140 } | 237 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 PrintUsage(argv[0]); | 272 PrintUsage(argv[0]); |
176 return 1; | 273 return 1; |
177 } | 274 } |
178 GetConfigAndPrintResult(ibus_config, argv[2]); | 275 GetConfigAndPrintResult(ibus_config, argv[2]); |
179 } else if (command == "unset_config") { | 276 } else if (command == "unset_config") { |
180 UnsetConfigAndPrintResult(ibus_config); | 277 UnsetConfigAndPrintResult(ibus_config); |
181 } | 278 } |
182 | 279 |
183 return 0; | 280 return 0; |
184 } | 281 } |
OLD | NEW |