Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 1 | // 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 Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "buffet/commands/command_manager.h" |
| 6 | |
Ben Chan | 1526097 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 8 | #include <base/files/scoped_temp_dir.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 9 | #include <base/json/json_writer.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 12 | #include "buffet/commands/unittest_utils.h" |
| 13 | |
| 14 | using buffet::unittests::CreateDictionaryValue; |
| 15 | |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const char kTestBaseCommands[] = R"({ |
| 19 | 'base': { |
| 20 | 'reboot': { |
| 21 | 'parameters': {'delay': 'integer'}, |
| 22 | 'results': {} |
| 23 | }, |
| 24 | 'shutdown': { |
| 25 | 'parameters': {}, |
| 26 | 'results': {} |
| 27 | } |
| 28 | } |
| 29 | })"; |
| 30 | |
| 31 | const 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 | |
| 44 | const char kTestTestCommands[] = R"({ |
| 45 | 'test': { |
| 46 | '_yo': { |
| 47 | 'parameters': {'name': 'string'}, |
| 48 | 'results': {} |
| 49 | } |
| 50 | } |
| 51 | })"; |
| 52 | |
| 53 | static void SaveJsonToFile(const base::DictionaryValue& dict, |
| 54 | const base::FilePath& file_path) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 55 | std::string json; |
| 56 | base::JSONWriter::Write(&dict, &json); |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 57 | 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 | |
| 61 | static base::FilePath SaveJsonToTempFile(const base::DictionaryValue& dict) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 62 | base::FilePath temp_file; |
| 63 | base::CreateTemporaryFile(&temp_file); |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 64 | SaveJsonToFile(dict, temp_file); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 65 | return temp_file; |
| 66 | } |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 67 | |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 70 | TEST(CommandManager, Empty) { |
| 71 | buffet::CommandManager manager; |
| 72 | EXPECT_TRUE(manager.GetCommandDictionary().IsEmpty()); |
| 73 | } |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 74 | |
| 75 | TEST(CommandManager, LoadBaseCommandsJSON) { |
| 76 | buffet::CommandManager manager; |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 77 | auto json = CreateDictionaryValue(kTestBaseCommands); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(manager.LoadBaseCommands(*json, nullptr)); |
| 79 | } |
| 80 | |
| 81 | TEST(CommandManager, LoadBaseCommandsFile) { |
| 82 | buffet::CommandManager manager; |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 83 | auto json = CreateDictionaryValue(kTestBaseCommands); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 84 | base::FilePath temp_file = SaveJsonToTempFile(*json); |
| 85 | EXPECT_TRUE(manager.LoadBaseCommands(temp_file, nullptr)); |
| 86 | base::DeleteFile(temp_file, false); |
| 87 | } |
| 88 | |
| 89 | TEST(CommandManager, LoadCommandsJSON) { |
| 90 | buffet::CommandManager manager; |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 91 | auto json = CreateDictionaryValue(kTestVendorCommands); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(manager.LoadCommands(*json, "category", nullptr)); |
| 93 | } |
| 94 | |
| 95 | TEST(CommandManager, LoadCommandsFile) { |
| 96 | buffet::CommandManager manager; |
| 97 | // Load some standard command definitions first. |
| 98 | auto json = CreateDictionaryValue(R"({ |
| 99 | 'base': { |
| 100 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 101 | 'parameters': {'delay': 'integer'}, |
| 102 | 'results': {} |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 103 | }, |
| 104 | 'shutdown': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 105 | 'parameters': {}, |
| 106 | 'results': {} |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | })"); |
| 110 | manager.LoadBaseCommands(*json, nullptr); |
| 111 | // Load device-supported commands. |
| 112 | json = CreateDictionaryValue(R"({ |
| 113 | 'base': { |
| 114 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 115 | 'parameters': {'delay': 'integer'}, |
| 116 | 'results': {} |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 117 | } |
| 118 | }, |
| 119 | 'robot': { |
| 120 | '_jump': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 121 | 'parameters': {'height': 'integer'}, |
| 122 | 'results': {} |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 123 | } |
| 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 Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 133 | |
| 134 | TEST(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 | } |