diff options
author | 2018-04-26 11:06:06 -0700 | |
---|---|---|
committer | 2018-04-27 10:44:42 -0700 | |
commit | 953f1093cd037f5a8a5050bc951e2cc197bc71e1 (patch) | |
tree | 02146f705e40d495ac9a1c0409df54ad03a4426d /cmds/bootanimation | |
parent | 71df621252e732a957e5e0357442fe082350b61d (diff) |
Add BootParameters tests.
Bug: 78577334
Test: Builds on master (aosp_x86-userdebug),
unit tests pass on oc-mr1 (on iot target).
Change-Id: I3584a285f1be914cc3e940469d7dea142d4a4231
Diffstat (limited to 'cmds/bootanimation')
-rw-r--r-- | cmds/bootanimation/Android.mk | 2 | ||||
-rw-r--r-- | cmds/bootanimation/iot/Android.mk | 38 | ||||
-rw-r--r-- | cmds/bootanimation/iot/BootParameters.cpp | 8 | ||||
-rw-r--r-- | cmds/bootanimation/iot/BootParameters.h | 3 | ||||
-rw-r--r-- | cmds/bootanimation/iot/BootParameters_test.cpp | 88 |
5 files changed, 136 insertions, 3 deletions
diff --git a/cmds/bootanimation/Android.mk b/cmds/bootanimation/Android.mk index e5d35b3b8a0e..a7349eebea21 100644 --- a/cmds/bootanimation/Android.mk +++ b/cmds/bootanimation/Android.mk @@ -94,3 +94,5 @@ LOCAL_32_BIT_ONLY := true 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 000000000000..8f5cfb371576 --- /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 da6ad0d1f08f..06cdbf8c33bb 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 @@ void BootParameters::loadParameters() { 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 c10bd44bc2ca..50e5d570b628 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 @@ public: // 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 000000000000..431563cdd173 --- /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 |