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 | |
Ben Chan | 1526097 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 5 | #include <base/files/file_util.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 6 | #include <base/json/json_writer.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 10 | #include "buffet/commands/unittest_utils.h" |
| 11 | |
| 12 | using buffet::unittests::CreateDictionaryValue; |
| 13 | |
| 14 | static base::FilePath SaveJsonToTempFile(const base::DictionaryValue& dict) { |
| 15 | std::string json; |
| 16 | base::JSONWriter::Write(&dict, &json); |
| 17 | base::FilePath temp_file; |
| 18 | base::CreateTemporaryFile(&temp_file); |
| 19 | base::WriteFile(temp_file, json.data(), static_cast<int>(json.size())); |
| 20 | return temp_file; |
| 21 | } |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 22 | |
| 23 | TEST(CommandManager, Empty) { |
| 24 | buffet::CommandManager manager; |
| 25 | EXPECT_TRUE(manager.GetCommandDictionary().IsEmpty()); |
| 26 | } |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 27 | |
| 28 | TEST(CommandManager, LoadBaseCommandsJSON) { |
| 29 | buffet::CommandManager manager; |
| 30 | auto json = CreateDictionaryValue(R"({ |
| 31 | 'base': { |
| 32 | 'reboot': { |
| 33 | 'parameters': {'delay': 'integer'} |
| 34 | }, |
| 35 | 'shutdown': { |
| 36 | 'parameters': {} |
| 37 | } |
| 38 | } |
| 39 | })"); |
| 40 | EXPECT_TRUE(manager.LoadBaseCommands(*json, nullptr)); |
| 41 | } |
| 42 | |
| 43 | TEST(CommandManager, LoadBaseCommandsFile) { |
| 44 | buffet::CommandManager manager; |
| 45 | auto json = CreateDictionaryValue(R"({ |
| 46 | 'base': { |
| 47 | 'reboot': { |
| 48 | 'parameters': {'delay': 'integer'} |
| 49 | }, |
| 50 | 'shutdown': { |
| 51 | 'parameters': {} |
| 52 | } |
| 53 | } |
| 54 | })"); |
| 55 | base::FilePath temp_file = SaveJsonToTempFile(*json); |
| 56 | EXPECT_TRUE(manager.LoadBaseCommands(temp_file, nullptr)); |
| 57 | base::DeleteFile(temp_file, false); |
| 58 | } |
| 59 | |
| 60 | TEST(CommandManager, LoadCommandsJSON) { |
| 61 | buffet::CommandManager manager; |
| 62 | auto json = CreateDictionaryValue(R"({ |
| 63 | 'robot': { |
| 64 | '_jump': { |
| 65 | 'parameters': {'height': 'integer'} |
| 66 | }, |
| 67 | '_speak': { |
| 68 | 'parameters': {'phrase': 'string'} |
| 69 | } |
| 70 | } |
| 71 | })"); |
| 72 | EXPECT_TRUE(manager.LoadCommands(*json, "category", nullptr)); |
| 73 | } |
| 74 | |
| 75 | TEST(CommandManager, LoadCommandsFile) { |
| 76 | buffet::CommandManager manager; |
| 77 | // Load some standard command definitions first. |
| 78 | auto json = CreateDictionaryValue(R"({ |
| 79 | 'base': { |
| 80 | 'reboot': { |
| 81 | 'parameters': {'delay': 'integer'} |
| 82 | }, |
| 83 | 'shutdown': { |
| 84 | 'parameters': {} |
| 85 | } |
| 86 | } |
| 87 | })"); |
| 88 | manager.LoadBaseCommands(*json, nullptr); |
| 89 | // Load device-supported commands. |
| 90 | json = CreateDictionaryValue(R"({ |
| 91 | 'base': { |
| 92 | 'reboot': { |
| 93 | 'parameters': {'delay': 'integer'} |
| 94 | } |
| 95 | }, |
| 96 | 'robot': { |
| 97 | '_jump': { |
| 98 | 'parameters': {'height': 'integer'} |
| 99 | } |
| 100 | } |
| 101 | })"); |
| 102 | base::FilePath temp_file = SaveJsonToTempFile(*json); |
| 103 | EXPECT_TRUE(manager.LoadCommands(temp_file, nullptr)); |
| 104 | base::DeleteFile(temp_file, false); |
| 105 | EXPECT_EQ(2, manager.GetCommandDictionary().GetSize()); |
| 106 | EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("base.reboot")); |
| 107 | EXPECT_NE(nullptr, manager.GetCommandDictionary().FindCommand("robot._jump")); |
| 108 | } |