Add BootParameters tests.

Bug: 78577334
Test: Builds on master (aosp_x86-userdebug),
unit tests pass on oc-mr1 (on iot target).

Change-Id: I3584a285f1be914cc3e940469d7dea142d4a4231
diff --git a/cmds/bootanimation/Android.mk b/cmds/bootanimation/Android.mk
index e5d35b3..a7349ee 100644
--- a/cmds/bootanimation/Android.mk
+++ b/cmds/bootanimation/Android.mk
@@ -94,3 +94,5 @@
 endif
 
 include ${BUILD_SHARED_LIBRARY}
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/cmds/bootanimation/iot/Android.mk b/cmds/bootanimation/iot/Android.mk
new file mode 100644
index 0000000..8f5cfb37
--- /dev/null
+++ b/cmds/bootanimation/iot/Android.mk
@@ -0,0 +1,38 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH:= $(call my-dir)
+
+ifeq ($(PRODUCT_IOT),true)
+
+# libbootanimation_iot_test
+# ===========================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbootanimation_iot_test
+LOCAL_CFLAGS := -Wall -Werror -Wunused -Wunreachable-code
+
+LOCAL_SHARED_LIBRARIES := \
+    libandroidthings \
+    libbase \
+    libchrome \
+    liblog \
+
+LOCAL_SRC_FILES := \
+    BootParameters.cpp \
+    BootParameters_test.cpp \
+
+include $(BUILD_NATIVE_TEST)
+
+endif # PRODUCT_IOT
diff --git a/cmds/bootanimation/iot/BootParameters.cpp b/cmds/bootanimation/iot/BootParameters.cpp
index da6ad0d..06cdbf8 100644
--- a/cmds/bootanimation/iot/BootParameters.cpp
+++ b/cmds/bootanimation/iot/BootParameters.cpp
@@ -20,8 +20,6 @@
 
 #include <fcntl.h>
 
-#include <string>
-
 #include <android-base/file.h>
 #include <base/json/json_parser.h>
 #include <base/json/json_reader.h>
@@ -98,7 +96,11 @@
         return;
     }
 
-    std::unique_ptr<Value> json = JSONReader::Read(contents);
+    loadParameters(contents);
+}
+
+void BootParameters::loadParameters(const std::string& raw_json) {
+    std::unique_ptr<Value> json = JSONReader::Read(raw_json);
     if (json.get() == nullptr) {
         return;
     }
diff --git a/cmds/bootanimation/iot/BootParameters.h b/cmds/bootanimation/iot/BootParameters.h
index c10bd44..50e5d57 100644
--- a/cmds/bootanimation/iot/BootParameters.h
+++ b/cmds/bootanimation/iot/BootParameters.h
@@ -18,6 +18,7 @@
 #define _BOOTANIMATION_BOOT_PARAMETERS_H_
 
 #include <list>
+#include <string>
 #include <vector>
 
 #include <base/json/json_value_converter.h>
@@ -43,6 +44,8 @@
     // Returns the additional boot parameters that were set on reboot.
     const std::vector<ABootActionParameter>& getParameters() const { return mParameters; }
 
+    // Exposed for testing. Updates the parameters with new JSON values.
+    void loadParameters(const std::string& raw_json);
 private:
     // Raw boot saved_parameters loaded from .json.
     struct SavedBootParameters {
diff --git a/cmds/bootanimation/iot/BootParameters_test.cpp b/cmds/bootanimation/iot/BootParameters_test.cpp
new file mode 100644
index 0000000..431563c
--- /dev/null
+++ b/cmds/bootanimation/iot/BootParameters_test.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BootParameters.h"
+
+#include <gtest/gtest.h>
+
+namespace android {
+
+namespace {
+
+TEST(BootParametersTest, TestParseValidParameters) {
+  BootParameters boot_parameters = BootParameters();
+  boot_parameters.loadParameters(R"(
+    {
+      "brightness":200,
+      "volume":100,
+      "param_names":["key1","key2"],
+      "param_values":["value1","value2"]
+    }
+  )");
+
+  EXPECT_TRUE(boot_parameters.hasBrightness());
+  EXPECT_TRUE(boot_parameters.hasVolume());
+  EXPECT_FLOAT_EQ(0.2f, boot_parameters.getBrightness());
+  EXPECT_FLOAT_EQ(0.1f, boot_parameters.getVolume());
+
+  auto parameters = boot_parameters.getParameters();
+  ASSERT_EQ(2u, parameters.size());
+  ASSERT_STREQ(parameters[0].key, "key1");
+  ASSERT_STREQ(parameters[0].value, "value1");
+  ASSERT_STREQ(parameters[1].key, "key2");
+  ASSERT_STREQ(parameters[1].value, "value2");
+}
+
+TEST(BootParametersTest, TestMismatchedParameters) {
+  BootParameters boot_parameters = BootParameters();
+  boot_parameters.loadParameters(R"(
+    {
+      "brightness":500,
+      "volume":500,
+      "param_names":["key1","key2"],
+      "param_values":["value1"]
+    }
+  )");
+
+  EXPECT_TRUE(boot_parameters.hasBrightness());
+  EXPECT_TRUE(boot_parameters.hasVolume());
+  EXPECT_FLOAT_EQ(0.5f, boot_parameters.getBrightness());
+  EXPECT_FLOAT_EQ(0.5f, boot_parameters.getVolume());
+
+  auto parameters = boot_parameters.getParameters();
+  ASSERT_EQ(0u, parameters.size());
+}
+
+TEST(BootParametersTest, TestMissingParameters) {
+  BootParameters boot_parameters = BootParameters();
+  boot_parameters.loadParameters(R"(
+    {
+      "brightness":500
+    }
+  )");
+
+  EXPECT_TRUE(boot_parameters.hasBrightness());
+  EXPECT_FALSE(boot_parameters.hasVolume());
+  EXPECT_FLOAT_EQ(0.5f, boot_parameters.getBrightness());
+  EXPECT_FLOAT_EQ(-1.0f, boot_parameters.getVolume());
+
+  auto parameters = boot_parameters.getParameters();
+  ASSERT_EQ(0u, parameters.size());
+}
+
+}  // namespace
+
+}  // namespace android