blob: 2d4fed8b21ceadab68544145a72ad286e2ebdbb1 [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_manager.h"
6
Ben Chan15260972014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Christopher Wileybb5b8482015-02-12 13:42:16 -08008#include <base/files/scoped_temp_dir.h>
Alex Vakulenkofd448692014-07-22 07:46:53 -07009#include <base/json/json_writer.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070010#include <gtest/gtest.h>
11
Alex Vakulenkofd448692014-07-22 07:46:53 -070012#include "buffet/commands/unittest_utils.h"
13
14using buffet::unittests::CreateDictionaryValue;
15
Christopher Wileybb5b8482015-02-12 13:42:16 -080016namespace {
17
18const char kTestBaseCommands[] = R"({
19 'base': {
20 'reboot': {
21 'parameters': {'delay': 'integer'},
22 'results': {}
23 },
24 'shutdown': {
25 'parameters': {},
26 'results': {}
27 }
28 }
29})";
30
31const char kTestVendorCommands[] = R"({
32 'robot': {
33 '_jump': {
34 'parameters': {'height': 'integer'},
35 'results': {}
36 },
37 '_speak': {
38 'parameters': {'phrase': 'string'},
39 'results': {}
40 }
41 }
42})";
43
44const char kTestTestCommands[] = R"({
45 'test': {
46 '_yo': {
47 'parameters': {'name': 'string'},
48 'results': {}
49 }
50 }
51})";
52
53static void SaveJsonToFile(const base::DictionaryValue& dict,
54 const base::FilePath& file_path) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070055 std::string json;
56 base::JSONWriter::Write(&dict, &json);
Christopher Wileybb5b8482015-02-12 13:42:16 -080057 const int bytes_to_write = static_cast<int>(json.size());
58 CHECK_EQ(bytes_to_write, WriteFile(file_path, json.data(), bytes_to_write));
59}
60
61static base::FilePath SaveJsonToTempFile(const base::DictionaryValue& dict) {
Alex Vakulenkofd448692014-07-22 07:46:53 -070062 base::FilePath temp_file;
63 base::CreateTemporaryFile(&temp_file);
Christopher Wileybb5b8482015-02-12 13:42:16 -080064 SaveJsonToFile(dict, temp_file);
Alex Vakulenkofd448692014-07-22 07:46:53 -070065 return temp_file;
66}
Alex Vakulenko7c36b672014-07-16 14:50:58 -070067
Christopher Wileybb5b8482015-02-12 13:42:16 -080068} // namespace
69
Alex Vakulenko7c36b672014-07-16 14:50:58 -070070TEST(CommandManager, Empty) {
71 buffet::CommandManager manager;
72 EXPECT_TRUE(manager.GetCommandDictionary().IsEmpty());
73}
Alex Vakulenkofd448692014-07-22 07:46:53 -070074
75TEST(CommandManager, LoadBaseCommandsJSON) {
76 buffet::CommandManager manager;
Christopher Wileybb5b8482015-02-12 13:42:16 -080077 auto json = CreateDictionaryValue(kTestBaseCommands);
Alex Vakulenkofd448692014-07-22 07:46:53 -070078 EXPECT_TRUE(manager.LoadBaseCommands(*json, nullptr));
79}
80
81TEST(CommandManager, LoadBaseCommandsFile) {
82 buffet::CommandManager manager;
Christopher Wileybb5b8482015-02-12 13:42:16 -080083 auto json = CreateDictionaryValue(kTestBaseCommands);
Alex Vakulenkofd448692014-07-22 07:46:53 -070084 base::FilePath temp_file = SaveJsonToTempFile(*json);
85 EXPECT_TRUE(manager.LoadBaseCommands(temp_file, nullptr));
86 base::DeleteFile(temp_file, false);
87}
88
89TEST(CommandManager, LoadCommandsJSON) {
90 buffet::CommandManager manager;
Christopher Wileybb5b8482015-02-12 13:42:16 -080091 auto json = CreateDictionaryValue(kTestVendorCommands);
Alex Vakulenkofd448692014-07-22 07:46:53 -070092 EXPECT_TRUE(manager.LoadCommands(*json, "category", nullptr));
93}
94
95TEST(CommandManager, LoadCommandsFile) {
96 buffet::CommandManager manager;
97 // Load some standard command definitions first.
98 auto json = CreateDictionaryValue(R"({
99 'base': {
100 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400101 'parameters': {'delay': 'integer'},
102 'results': {}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700103 },
104 'shutdown': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400105 'parameters': {},
106 'results': {}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700107 }
108 }
109 })");
110 manager.LoadBaseCommands(*json, nullptr);
111 // Load device-supported commands.
112 json = CreateDictionaryValue(R"({
113 'base': {
114 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400115 'parameters': {'delay': 'integer'},
116 'results': {}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700117 }
118 },
119 'robot': {
120 '_jump': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400121 'parameters': {'height': 'integer'},
122 'results': {}
Alex Vakulenkofd448692014-07-22 07:46:53 -0700123 }
124 }
125 })");
126 base::FilePath temp_file = SaveJsonToTempFile(*json);
127 EXPECT_TRUE(manager.LoadCommands(temp_file, nullptr));
128 base::DeleteFile(temp_file, false);
129 EXPECT_EQ(2, manager.GetCommandDictionary().GetSize());
130 EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("base.reboot"));
131 EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("robot._jump"));
132}
Christopher Wileybb5b8482015-02-12 13:42:16 -0800133
134TEST(CommandManager, ShouldLoadStandardAndTestDefinitions) {
135 buffet::CommandManager manager;
136 base::ScopedTempDir temp;
137 CHECK(temp.CreateUniqueTempDir());
138 base::FilePath base_path{temp.path().Append("base_defs")};
139 base::FilePath test_path{temp.path().Append("test_defs")};
140 base::FilePath base_commands_path{base_path.Append("commands")};
141 base::FilePath test_commands_path{test_path.Append("commands")};
142 CHECK(CreateDirectory(base_commands_path));
143 CHECK(CreateDirectory(test_commands_path));
144 SaveJsonToFile(*CreateDictionaryValue(kTestBaseCommands),
145 base_path.Append("gcd.json"));
146 SaveJsonToFile(*CreateDictionaryValue(kTestVendorCommands),
147 base_commands_path.Append("category.json"));
148 SaveJsonToFile(*CreateDictionaryValue(kTestTestCommands),
149 test_commands_path.Append("test.json"));
150 manager.Startup(base_path, test_path);
151 EXPECT_EQ(3, manager.GetCommandDictionary().GetSize());
152 EXPECT_NE(nullptr,
153 manager.GetCommandDictionary().FindCommand("robot._jump"));
154 EXPECT_NE(nullptr,
155 manager.GetCommandDictionary().FindCommand("robot._speak"));
156 EXPECT_NE(nullptr,
157 manager.GetCommandDictionary().FindCommand("test._yo"));
158}