blob: e31f8cc150cbda8c08bf9d6cd693af61ac110716 [file] [log] [blame]
Alex Vakulenko7c36b672014-07-16 14:50:58 -07001// Copyright 2014 The Chromium OS 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
Alex Deymof6cbe322014-11-10 19:55:35 -08005#include "buffet/commands/command_dictionary.h"
6
Alex Vakulenko7c36b672014-07-16 14:50:58 -07007#include <gtest/gtest.h>
8
Alex Vakulenko7c36b672014-07-16 14:50:58 -07009#include "buffet/commands/unittest_utils.h"
10
Vitaly Buka32005de2015-05-01 12:33:31 -070011namespace buffet {
12
13using unittests::CreateDictionaryValue;
Vitaly Buka7c82d292015-05-03 18:08:12 -070014using unittests::IsEqualValue;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070015
16TEST(CommandDictionary, Empty) {
Vitaly Buka32005de2015-05-01 12:33:31 -070017 CommandDictionary dict;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070018 EXPECT_TRUE(dict.IsEmpty());
19 EXPECT_EQ(nullptr, dict.FindCommand("robot.jump"));
20 EXPECT_TRUE(dict.GetCommandNamesByCategory("robotd").empty());
21}
22
23TEST(CommandDictionary, LoadCommands) {
24 auto json = CreateDictionaryValue(R"({
25 'robot': {
26 'jump': {
27 'parameters': {
28 'height': 'integer',
29 '_jumpType': ['_withAirFlip', '_withSpin', '_withKick']
Anton Muhin71fb9d52014-11-21 22:22:39 +040030 },
Vitaly Buka4129dfa2015-04-29 12:16:58 -070031 'progress': {
32 'progress': 'integer'
33 },
Anton Muhin71fb9d52014-11-21 22:22:39 +040034 'results': {}
Alex Vakulenko7c36b672014-07-16 14:50:58 -070035 }
36 }
37 })");
Vitaly Buka32005de2015-05-01 12:33:31 -070038 CommandDictionary dict;
Alex Vakulenkofd448692014-07-22 07:46:53 -070039 EXPECT_TRUE(dict.LoadCommands(*json, "robotd", nullptr, nullptr));
Alex Vakulenko7c36b672014-07-16 14:50:58 -070040 EXPECT_EQ(1, dict.GetSize());
41 EXPECT_NE(nullptr, dict.FindCommand("robot.jump"));
42 json = CreateDictionaryValue(R"({
43 'base': {
44 'reboot': {
Vitaly Buka10cf5c72015-04-29 12:05:10 -070045 'parameters': {'delay': 'integer'}
Alex Vakulenko7c36b672014-07-16 14:50:58 -070046 },
47 'shutdown': {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070048 }
49 }
50 })");
Alex Vakulenkofd448692014-07-22 07:46:53 -070051 EXPECT_TRUE(dict.LoadCommands(*json, "powerd", nullptr, nullptr));
Alex Vakulenko7c36b672014-07-16 14:50:58 -070052 EXPECT_EQ(3, dict.GetSize());
53 EXPECT_NE(nullptr, dict.FindCommand("robot.jump"));
54 EXPECT_NE(nullptr, dict.FindCommand("base.reboot"));
55 EXPECT_NE(nullptr, dict.FindCommand("base.shutdown"));
56 EXPECT_EQ(nullptr, dict.FindCommand("foo.bar"));
57 std::vector<std::string> expected_commands{"base.reboot", "base.shutdown"};
58 EXPECT_EQ(expected_commands, dict.GetCommandNamesByCategory("powerd"));
59}
60
61TEST(CommandDictionary, LoadCommands_Failures) {
Vitaly Buka32005de2015-05-01 12:33:31 -070062 CommandDictionary dict;
Alex Vakulenko5f472062014-08-14 17:54:04 -070063 chromeos::ErrorPtr error;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070064
Alex Vakulenko7c36b672014-07-16 14:50:58 -070065 // Command definition is not an object.
Vitaly Buka10cf5c72015-04-29 12:05:10 -070066 auto json = CreateDictionaryValue("{'robot':{'jump':0}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -070067 EXPECT_FALSE(dict.LoadCommands(*json, "robotd", nullptr, &error));
Alex Vakulenko7c36b672014-07-16 14:50:58 -070068 EXPECT_EQ("type_mismatch", error->GetCode());
69 EXPECT_EQ("Expecting an object for command 'jump'", error->GetMessage());
70 error.reset();
71
72 // Package definition is not an object.
73 json = CreateDictionaryValue("{'robot':'blah'}");
Alex Vakulenkofd448692014-07-22 07:46:53 -070074 EXPECT_FALSE(dict.LoadCommands(*json, "robotd", nullptr, &error));
Alex Vakulenko7c36b672014-07-16 14:50:58 -070075 EXPECT_EQ("type_mismatch", error->GetCode());
76 EXPECT_EQ("Expecting an object for package 'robot'", error->GetMessage());
77 error.reset();
78
79 // Invalid command definition is not an object.
Anton Muhin71fb9d52014-11-21 22:22:39 +040080 json = CreateDictionaryValue(
81 "{'robot':{'jump':{'parameters':{'flip':0},'results':{}}}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -070082 EXPECT_FALSE(dict.LoadCommands(*json, "robotd", nullptr, &error));
Alex Vakulenko7c36b672014-07-16 14:50:58 -070083 EXPECT_EQ("invalid_object_schema", error->GetCode());
84 EXPECT_EQ("Invalid definition for command 'robot.jump'", error->GetMessage());
85 EXPECT_NE(nullptr, error->GetInnerError()); // Must have additional info.
86 error.reset();
87
Alex Vakulenkofd448692014-07-22 07:46:53 -070088 // Empty command name.
Anton Muhin71fb9d52014-11-21 22:22:39 +040089 json = CreateDictionaryValue("{'robot':{'':{'parameters':{},'results':{}}}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -070090 EXPECT_FALSE(dict.LoadCommands(*json, "robotd", nullptr, &error));
91 EXPECT_EQ("invalid_command_name", error->GetCode());
92 EXPECT_EQ("Unnamed command encountered in package 'robot'",
93 error->GetMessage());
94 error.reset();
95}
96
Christopher Wiley13fca9d2015-01-14 09:56:34 -080097TEST(CommandDictionaryDeathTest, LoadCommands_RedefineInDifferentCategory) {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070098 // Redefine commands in different category.
Vitaly Buka32005de2015-05-01 12:33:31 -070099 CommandDictionary dict;
Alex Vakulenko5f472062014-08-14 17:54:04 -0700100 chromeos::ErrorPtr error;
Vitaly Buka10cf5c72015-04-29 12:05:10 -0700101 auto json = CreateDictionaryValue("{'robot':{'jump':{}}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -0700102 dict.LoadCommands(*json, "category1", nullptr, &error);
Christopher Wiley13fca9d2015-01-14 09:56:34 -0800103 ASSERT_DEATH(dict.LoadCommands(*json, "category2", nullptr, &error),
104 ".*Definition for command 'robot.jump' overrides an "
105 "earlier definition in category 'category1'");
Alex Vakulenko7c36b672014-07-16 14:50:58 -0700106}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700107
108TEST(CommandDictionary, LoadCommands_CustomCommandNaming) {
109 // Custom command must start with '_'.
Vitaly Buka32005de2015-05-01 12:33:31 -0700110 CommandDictionary base_dict;
111 CommandDictionary dict;
Alex Vakulenko5f472062014-08-14 17:54:04 -0700112 chromeos::ErrorPtr error;
Alex Vakulenkofd448692014-07-22 07:46:53 -0700113 auto json = CreateDictionaryValue(R"({
114 'base': {
115 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400116 'parameters': {'delay': 'integer'},
117 'results': {}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700118 }
119 }
120 })");
121 base_dict.LoadCommands(*json, "", nullptr, &error);
122 EXPECT_TRUE(dict.LoadCommands(*json, "robotd", &base_dict, &error));
Anton Muhin71fb9d52014-11-21 22:22:39 +0400123 auto json2 = CreateDictionaryValue(
124 "{'base':{'jump':{'parameters':{},'results':{}}}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -0700125 EXPECT_FALSE(dict.LoadCommands(*json2, "robotd", &base_dict, &error));
126 EXPECT_EQ("invalid_command_name", error->GetCode());
127 EXPECT_EQ("The name of custom command 'jump' in package 'base' must start "
128 "with '_'", error->GetMessage());
129 error.reset();
130
131 // If the command starts with "_", then it's Ok.
Anton Muhin71fb9d52014-11-21 22:22:39 +0400132 json2 = CreateDictionaryValue(
133 "{'base':{'_jump':{'parameters':{},'results':{}}}}");
Alex Vakulenkofd448692014-07-22 07:46:53 -0700134 EXPECT_TRUE(dict.LoadCommands(*json2, "robotd", &base_dict, nullptr));
135}
136
137TEST(CommandDictionary, LoadCommands_RedefineStdCommand) {
138 // Redefine commands parameter type.
Vitaly Buka32005de2015-05-01 12:33:31 -0700139 CommandDictionary base_dict;
140 CommandDictionary dict;
Alex Vakulenko5f472062014-08-14 17:54:04 -0700141 chromeos::ErrorPtr error;
Alex Vakulenkofd448692014-07-22 07:46:53 -0700142 auto json = CreateDictionaryValue(R"({
143 'base': {
144 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400145 'parameters': {'delay': 'integer'},
146 'results': {'version': 'integer'}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700147 }
148 }
149 })");
150 base_dict.LoadCommands(*json, "", nullptr, &error);
Anton Muhin71fb9d52014-11-21 22:22:39 +0400151
Alex Vakulenkofd448692014-07-22 07:46:53 -0700152 auto json2 = CreateDictionaryValue(R"({
153 'base': {
154 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400155 'parameters': {'delay': 'string'},
156 'results': {'version': 'integer'}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700157 }
158 }
159 })");
160 EXPECT_FALSE(dict.LoadCommands(*json2, "robotd", &base_dict, &error));
161 EXPECT_EQ("invalid_object_schema", error->GetCode());
162 EXPECT_EQ("Invalid definition for command 'base.reboot'",
163 error->GetMessage());
164 EXPECT_EQ("invalid_parameter_definition", error->GetInnerError()->GetCode());
165 EXPECT_EQ("Error in definition of property 'delay'",
166 error->GetInnerError()->GetMessage());
167 EXPECT_EQ("param_type_changed", error->GetFirstError()->GetCode());
168 EXPECT_EQ("Redefining a property of type integer as string",
169 error->GetFirstError()->GetMessage());
170 error.reset();
Anton Muhin71fb9d52014-11-21 22:22:39 +0400171
172 auto json3 = CreateDictionaryValue(R"({
173 'base': {
174 'reboot': {
175 'parameters': {'delay': 'integer'},
176 'results': {'version': 'string'}
177 }
178 }
179 })");
180 EXPECT_FALSE(dict.LoadCommands(*json3, "robotd", &base_dict, &error));
181 EXPECT_EQ("invalid_object_schema", error->GetCode());
182 EXPECT_EQ("Invalid definition for command 'base.reboot'",
183 error->GetMessage());
184 // TODO(antonm): remove parameter from error below and use some generic.
185 EXPECT_EQ("invalid_parameter_definition", error->GetInnerError()->GetCode());
186 EXPECT_EQ("Error in definition of property 'version'",
187 error->GetInnerError()->GetMessage());
188 EXPECT_EQ("param_type_changed", error->GetFirstError()->GetCode());
189 EXPECT_EQ("Redefining a property of type integer as string",
190 error->GetFirstError()->GetMessage());
191 error.reset();
Alex Vakulenkofd448692014-07-22 07:46:53 -0700192}
Alex Vakulenko45109442014-07-29 11:07:10 -0700193
194TEST(CommandDictionary, GetCommandsAsJson) {
195 auto json_base = CreateDictionaryValue(R"({
196 'base': {
197 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400198 'parameters': {'delay': {'maximum': 100}},
199 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700200 },
201 'shutdown': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400202 'parameters': {},
203 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700204 }
205 }
206 })");
Vitaly Buka32005de2015-05-01 12:33:31 -0700207 CommandDictionary base_dict;
Alex Vakulenko45109442014-07-29 11:07:10 -0700208 base_dict.LoadCommands(*json_base, "base", nullptr, nullptr);
209
Vitaly Buka7c82d292015-05-03 18:08:12 -0700210 auto json = CreateDictionaryValue(R"({
Alex Vakulenko45109442014-07-29 11:07:10 -0700211 'base': {
212 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400213 'parameters': {'delay': {'minimum': 10}},
214 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700215 }
216 },
217 'robot': {
218 '_jump': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400219 'parameters': {'_height': 'integer'},
220 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700221 }
222 }
223 })");
Vitaly Buka32005de2015-05-01 12:33:31 -0700224 CommandDictionary dict;
Alex Vakulenko45109442014-07-29 11:07:10 -0700225 dict.LoadCommands(*json, "device", &base_dict, nullptr);
226
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700227 json = dict.GetCommandsAsJson(
228 [](const CommandDefinition* def) { return true; }, false, nullptr);
Vitaly Buka7c82d292015-05-03 18:08:12 -0700229 ASSERT_NE(nullptr, json.get());
230 auto expected = R"({
231 'base': {
232 'reboot': {
233 'parameters': {'delay': {'minimum': 10}}
234 }
235 },
236 'robot': {
237 '_jump': {
238 'parameters': {'_height': 'integer'}
239 }
240 }
241 })";
242 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko45109442014-07-29 11:07:10 -0700243
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700244 json = dict.GetCommandsAsJson(
245 [](const CommandDefinition* def) { return true; }, true, nullptr);
Vitaly Buka7c82d292015-05-03 18:08:12 -0700246 ASSERT_NE(nullptr, json.get());
247 expected = R"({
248 'base': {
249 'reboot': {
250 'parameters': {
251 'delay': {
252 'maximum': 100,
253 'minimum': 10,
254 'type': 'integer'
255 }
256 }
257 }
258 },
259 'robot': {
260 '_jump': {
261 'parameters': {
262 '_height': {
263 'type': 'integer'
264 }
265 }
266 }
267 }
268 })";
269 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko45109442014-07-29 11:07:10 -0700270}
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700271
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700272TEST(CommandDictionary, GetCommandsAsJsonWithVisibility) {
Vitaly Buka7c82d292015-05-03 18:08:12 -0700273 auto json = CreateDictionaryValue(R"({
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700274 'test': {
275 'command1': {
276 'parameters': {},
277 'results': {},
278 'visibility': 'none'
279 },
280 'command2': {
281 'parameters': {},
282 'results': {},
283 'visibility': 'local'
284 },
285 'command3': {
286 'parameters': {},
287 'results': {},
288 'visibility': 'cloud'
289 },
290 'command4': {
291 'parameters': {},
292 'results': {},
293 'visibility': 'all'
294 },
295 'command5': {
296 'parameters': {},
297 'results': {},
298 'visibility': 'none'
299 },
300 'command6': {
301 'parameters': {},
302 'results': {},
303 'visibility': 'local'
304 },
305 'command7': {
306 'parameters': {},
307 'results': {},
308 'visibility': 'cloud'
309 },
310 'command8': {
311 'parameters': {},
312 'results': {},
313 'visibility': 'all'
314 }
315 }
316 })");
Vitaly Buka32005de2015-05-01 12:33:31 -0700317 CommandDictionary dict;
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700318 ASSERT_TRUE(dict.LoadCommands(*json, "test", nullptr, nullptr));
319
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700320 json = dict.GetCommandsAsJson(
321 [](const CommandDefinition* def) { return true; }, false, nullptr);
322 ASSERT_NE(nullptr, json.get());
Vitaly Buka7c82d292015-05-03 18:08:12 -0700323 auto expected = R"({
324 'test': {
325 'command1': {'parameters': {}},
326 'command2': {'parameters': {}},
327 'command3': {'parameters': {}},
328 'command4': {'parameters': {}},
329 'command5': {'parameters': {}},
330 'command6': {'parameters': {}},
331 'command7': {'parameters': {}},
332 'command8': {'parameters': {}}
333 }
334 })";
335 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700336
337 json = dict.GetCommandsAsJson(
338 [](const CommandDefinition* def) { return def->GetVisibility().local; },
339 false, nullptr);
340 ASSERT_NE(nullptr, json.get());
Vitaly Buka7c82d292015-05-03 18:08:12 -0700341 expected = R"({
342 'test': {
343 'command2': {'parameters': {}},
344 'command4': {'parameters': {}},
345 'command6': {'parameters': {}},
346 'command8': {'parameters': {}}
347 }
348 })";
349 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700350
351 json = dict.GetCommandsAsJson(
352 [](const CommandDefinition* def) { return def->GetVisibility().cloud; },
353 false, nullptr);
354 ASSERT_NE(nullptr, json.get());
Vitaly Buka7c82d292015-05-03 18:08:12 -0700355 expected = R"({
356 'test': {
357 'command3': {'parameters': {}},
358 'command4': {'parameters': {}},
359 'command7': {'parameters': {}},
360 'command8': {'parameters': {}}
361 }
362 })";
363 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700364
365 json = dict.GetCommandsAsJson(
366 [](const CommandDefinition* def) {
367 return def->GetVisibility().local && def->GetVisibility().cloud;
368 }, false, nullptr);
369 ASSERT_NE(nullptr, json.get());
Vitaly Buka7c82d292015-05-03 18:08:12 -0700370 expected = R"({
371 'test': {
372 'command4': {'parameters': {}},
373 'command8': {'parameters': {}}
374 }
375 })";
376 EXPECT_JSON_EQ(expected, *json);
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700377}
378
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700379TEST(CommandDictionary, LoadCommandsWithVisibility) {
Vitaly Buka32005de2015-05-01 12:33:31 -0700380 CommandDictionary dict;
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700381 auto json = CreateDictionaryValue(R"({
382 'base': {
383 'command1': {
384 'parameters': {},
385 'results': {},
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700386 'visibility':'none'
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700387 },
388 'command2': {
389 'parameters': {},
390 'results': {},
391 'visibility':'local'
392 },
393 'command3': {
394 'parameters': {},
395 'results': {},
396 'visibility':'cloud'
397 },
398 'command4': {
399 'parameters': {},
400 'results': {},
401 'visibility':'all'
402 },
403 'command5': {
404 'parameters': {},
405 'results': {},
406 'visibility':'cloud,local'
407 }
408 }
409 })");
410 EXPECT_TRUE(dict.LoadCommands(*json, "testd", nullptr, nullptr));
411 auto cmd = dict.FindCommand("base.command1");
412 ASSERT_NE(nullptr, cmd);
413 EXPECT_EQ("none", cmd->GetVisibility().ToString());
414
415 cmd = dict.FindCommand("base.command2");
416 ASSERT_NE(nullptr, cmd);
417 EXPECT_EQ("local", cmd->GetVisibility().ToString());
418
419 cmd = dict.FindCommand("base.command3");
420 ASSERT_NE(nullptr, cmd);
421 EXPECT_EQ("cloud", cmd->GetVisibility().ToString());
422
423 cmd = dict.FindCommand("base.command4");
424 ASSERT_NE(nullptr, cmd);
425 EXPECT_EQ("all", cmd->GetVisibility().ToString());
426
427 cmd = dict.FindCommand("base.command5");
428 ASSERT_NE(nullptr, cmd);
429 EXPECT_EQ("all", cmd->GetVisibility().ToString());
430}
431
432TEST(CommandDictionary, LoadCommandsWithVisibility_Inheritance) {
Vitaly Buka32005de2015-05-01 12:33:31 -0700433 CommandDictionary base_dict;
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700434 auto json = CreateDictionaryValue(R"({
435 'base': {
436 'command1': {
437 'parameters': {},
438 'results': {},
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700439 'visibility':'none'
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700440 },
441 'command2': {
442 'parameters': {},
443 'results': {},
444 'visibility':'local'
445 },
446 'command3': {
447 'parameters': {},
448 'results': {},
449 'visibility':'cloud'
450 },
451 'command4': {
452 'parameters': {},
453 'results': {},
454 'visibility':'all'
455 },
456 'command5': {
457 'parameters': {},
458 'results': {},
459 'visibility':'local,cloud'
460 }
461 }
462 })");
463 EXPECT_TRUE(base_dict.LoadCommands(*json, "testd", nullptr, nullptr));
464
Vitaly Buka32005de2015-05-01 12:33:31 -0700465 CommandDictionary dict;
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700466 json = CreateDictionaryValue(R"({
467 'base': {
468 'command1': {
469 'parameters': {},
470 'results': {}
471 },
472 'command2': {
473 'parameters': {},
474 'results': {}
475 },
476 'command3': {
477 'parameters': {},
478 'results': {}
479 },
480 'command4': {
481 'parameters': {},
482 'results': {}
483 },
484 'command5': {
485 'parameters': {},
486 'results': {}
487 },
488 '_command6': {
489 'parameters': {},
490 'results': {}
491 }
492 }
493 })");
494 EXPECT_TRUE(dict.LoadCommands(*json, "testd", &base_dict, nullptr));
495
496 auto cmd = dict.FindCommand("base.command1");
497 ASSERT_NE(nullptr, cmd);
498 EXPECT_EQ("none", cmd->GetVisibility().ToString());
499
500 cmd = dict.FindCommand("base.command2");
501 ASSERT_NE(nullptr, cmd);
502 EXPECT_EQ("local", cmd->GetVisibility().ToString());
503
504 cmd = dict.FindCommand("base.command3");
505 ASSERT_NE(nullptr, cmd);
506 EXPECT_EQ("cloud", cmd->GetVisibility().ToString());
507
508 cmd = dict.FindCommand("base.command4");
509 ASSERT_NE(nullptr, cmd);
510 EXPECT_EQ("all", cmd->GetVisibility().ToString());
511
512 cmd = dict.FindCommand("base.command5");
513 ASSERT_NE(nullptr, cmd);
514 EXPECT_EQ("all", cmd->GetVisibility().ToString());
515
516 cmd = dict.FindCommand("base._command6");
517 ASSERT_NE(nullptr, cmd);
518 EXPECT_EQ("all", cmd->GetVisibility().ToString());
519}
520
521TEST(CommandDictionary, LoadCommandsWithVisibility_Failures) {
Vitaly Buka32005de2015-05-01 12:33:31 -0700522 CommandDictionary dict;
Alex Vakulenko5e86fee2015-04-17 08:47:45 -0700523 chromeos::ErrorPtr error;
524
525 auto json = CreateDictionaryValue(R"({
526 'base': {
527 'jump': {
528 'parameters': {},
529 'results': {},
530 'visibility':'foo'
531 }
532 }
533 })");
534 EXPECT_FALSE(dict.LoadCommands(*json, "testd", nullptr, &error));
535 EXPECT_EQ("invalid_command_visibility", error->GetCode());
536 EXPECT_EQ("Error parsing command 'base.jump'", error->GetMessage());
537 EXPECT_EQ("invalid_parameter_value", error->GetInnerError()->GetCode());
538 EXPECT_EQ("Invalid command visibility value 'foo'",
539 error->GetInnerError()->GetMessage());
540 error.reset();
541}
Vitaly Buka32005de2015-05-01 12:33:31 -0700542
543} // namespace buffet