avs-device-sdk/Settings/test/SettingStringConversionTest...

217 lines
7.1 KiB
C++
Raw Normal View History

Version 1.11 alexa-client-sdk Changes in this update: **Enhancements** * Added support for the new Alexa [DoNotDisturb](https://developer.amazon.com/docs/alexa-voice-service/donotdisturb.html) interface, which enables users to toggle the do not disturb (DND) function on their Alexa built-in products. * The SDK now supports [Opus](https://opus-codec.org/license/) encoding, which is optional. To enable Opus, you must [set the CMake flag to `-DOPUS=ON`](https://github.com/alexa/avs-device-sdk/wiki/Build-Options#Opus-encoding), and include the [libopus library](https://github.com/alexa/avs-device-sdk/wiki/Dependencies#core-dependencies) dependency in your build. * The MediaPlayer reference implementation has been expanded to support the SAMPLE-AES and AES-128 encryption methods for HLS streaming. * AES-128 encryption is dependent on libcrypto, which is part of the required openSSL library, and is enabled by default. * To enable [SAMPLE-AES](https://github.com/alexa/avs-device-sdk/wiki/Dependencies#core-dependencies/Enable-SAMPLE-AES-decryption) encryption, you must set the `-DSAMPLE_AES=ON` in your CMake command, and include the [FFMPEG](https://github.com/alexa/avs-device-sdk/wiki/Dependencies#core-dependencies/Enable-SAMPLE-AES-decryption) library dependency in your build. * A new configuration for [deviceSettings](https://github.com/alexa/avs-device-sdk/blob/v1.11.0/Integration/AlexaClientSDKConfig.json#L59) has been added.This configuration allows you to specify the location of the device settings database. * Added locale support for es-MX. **Bug Fixes** * Fixed an issue where music wouldn't resume playback in the Android app. * Now all equalizer capabilities are fully disabled when equalizer is turned off in configuration file. Previously, devices were unconditionally required to provide support for equalizer in order to run the SDK. * [Issue 1106](https://github.com/alexa/avs-device-sdk/issues/1106) - Fixed an issue in which the `CBLAuthDelegate` wasn't using the correct timeout during request refresh. * [Issue 1128](https://github.com/alexa/avs-device-sdk/issues/1128) - Fixed an issue in which the `AudioPlayer` instance persisted at shutdown, due to a shared dependency with the `ProgressTimer`. * Fixed in issue that occurred when a connection to streaming content was interrupted, the SDK did not attempt to resume the connection, and appeared to assume that the content had been fully downloaded. This triggered the next track to be played, assuming it was a playlist. * [Issue 1040](https://github.com/alexa/avs-device-sdk/issues/1040) - Fixed an issue where alarms would continue to play after user barge-in. **Known Issues** * `PlaylistParser` and `IterativePlaylistParser` generate two HTTP requests (one to fetch the content type, and one to fetch the audio data) for each audio stream played. * Music playback history isn't being displayed in the Alexa app for certain account and device types. * On GCC 8+, issues related to `-Wclass-memaccess` will trigger warnings. However, this won't cause the build to fail, and these warnings can be ignored. * In order to use Bluetooth source and sink PulseAudio, you must manually load and unload PulseAudio modules after the SDK starts. * The `ACL` may encounter issues if audio attachments are received but not consumed. * `SpeechSynthesizerState` currently uses `GAINING_FOCUS` and `LOSING_FOCUS` as a workaround for handling intermediate state. These states may be removed in a future release. * The Alexa app doesn't always indicate when a device is successfully connected via Bluetooth. * Connecting a product to streaming media via Bluetooth will sometimes stop media playback within the source application. Resuming playback through the source application or toggling next/previous will correct playback. * When a source device is streaming silence via Bluetooth, the Alexa app indicates that audio content is streaming. * The Bluetooth agent assumes that the Bluetooth adapter is always connected to a power source. Disconnecting from a power source during operation is not yet supported. * On some products, interrupted Bluetooth playback may not resume if other content is locally streamed. * `make integration` is currently not available for Android. In order to run integration tests on Android, you'll need to manually upload the test binary file along with any input file. At that point, the adb can be used to run the integration tests. * On Raspberry Pi running Android Things with HDMI output audio, beginning of speech is truncated when Alexa responds to user TTS. * When the sample app is restarted and network connection is lost, alerts don't play. * When network connection is lost, lost connection status is not returned via local Text-to Speech (TTS).
2018-12-19 19:13:36 +00:00
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file 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 <memory>
#include <string>
#include <ostream>
#include <istream>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <AVSCommon/Utils/JSON/JSONGenerator.h>
#include <AVSCommon/Utils/JSON/JSONUtils.h>
#include <Settings/SettingStringConversion.h>
namespace alexaClientSDK {
namespace settings {
namespace test {
using namespace avsCommon::utils::json;
/// The key used for converting the hello object.
static const std::string KEY = "key";
/// The value used to initialize the hello object.
static const std::string INIT_VALUE = "value";
/**
* Define an enumeration for testing string conversion.
*/
enum class HelloEnum { HI, THERE };
/**
* Define an operator<< for the test enum @c HelloEnum.
* @param os The output stream.
* @param hello The enumeration to be converted.
* @return The output stream. The failbit is set if the conversion fails.
*/
std::ostream& operator<<(std::ostream& os, const HelloEnum& hello) {
if (HelloEnum::HI == hello) {
os << "HI";
} else if (HelloEnum::THERE == hello) {
os << "THERE";
} else {
os.setstate(std::ios_base::failbit);
}
return os;
}
/**
* Define an operator>> for the test enum @c HelloEnum.
*
* @param is The input stream.
* @param[out] hello The enum that will receive the new value.
* @return The input stream. The failbit is set if the conversion fails.
*/
std::istream& operator>>(std::istream& is, HelloEnum& hello) {
std::string str;
is >> str;
if ("HI" == str) {
hello = HelloEnum::HI;
} else if ("THERE" == str) {
hello = HelloEnum::THERE;
} else {
is.setstate(std::ios_base::failbit);
}
return is;
}
/**
* Dummy class that only has a name key.
*/
class HelloClass {
public:
/**
* The constructor.
*/
HelloClass();
/**
* Compares two instances of HelloClass
*
* @param rhs The right hand size.
* @return @c true if name is the same and @c false otherwise.
*/
bool operator==(const HelloClass& rhs) const;
/// Just a string to check the conversion succeeds.
std::string m_name = INIT_VALUE;
};
HelloClass::HelloClass() : m_name{INIT_VALUE} {
}
bool HelloClass::operator==(const HelloClass& rhs) const {
return m_name == rhs.m_name;
}
/**
* Define an operator<< for the test class @c HelloClass.
*
* @param os The output stream.
* @param hello The object to be converted.
* @return The output stream. The failbit is set if the conversion fails.
*/
std::ostream& operator<<(std::ostream& os, const HelloClass& hello) {
JsonGenerator jsonGenerator;
EXPECT_TRUE(jsonGenerator.addMember(KEY, hello.m_name));
os << jsonGenerator.toString();
return os;
}
/**
* Define an operator>> for the test class @c HelloClass.
*
* @param is The input stream.
* @param[out] hello The object that will receive the new value.
* @return The input stream. The failbit is set if the conversion fails.
*/
std::istream& operator>>(std::istream& is, HelloClass& hello) {
std::string json;
is >> json;
if (!jsonUtils::retrieveValue(json, KEY, &hello.m_name)) {
is.setstate(std::ios_base::failbit);
}
return is;
}
std::pair<bool, std::string> expected(bool result, std::string value) {
return {result, value};
};
/// Test boolean conversions.
TEST(SettingStringConversionTest, testBoolConversion) {
// Valid conversions
EXPECT_EQ(toSettingString<bool>(false), expected(true, "false"));
EXPECT_EQ(toSettingString<bool>(true), expected(true, "true"));
EXPECT_EQ(fromSettingString<bool>("false", true), std::make_pair(true, false));
EXPECT_EQ(fromSettingString<bool>("true", false), std::make_pair(true, true));
// Invalid conversion
EXPECT_EQ(fromSettingString<bool>("not bool", false), std::make_pair(false, false));
};
TEST(SettingStringConversionTest, testIntegralByteSize) {
// Valid conversions
EXPECT_EQ(fromSettingString<int8_t>("10", 100).first, true);
EXPECT_EQ(fromSettingString<int8_t>("10", 100).second, 10);
EXPECT_EQ(fromSettingString<uint8_t>("10", 100).second, 10u);
EXPECT_EQ(toSettingString<int8_t>(10), expected(true, "10"));
EXPECT_EQ(toSettingString<uint8_t>(10), expected(true, "10"));
// Invalid conversion
EXPECT_EQ(fromSettingString<int8_t>("not int", 10).first, false);
EXPECT_EQ(fromSettingString<int8_t>("not int", 10).second, 10);
};
TEST(SettingStringConversionTest, testArithmeticTypes) {
// Valid conversions
EXPECT_EQ(toSettingString<char>('a'), expected(true, "a"));
EXPECT_EQ(toSettingString<int>(10), expected(true, "10"));
EXPECT_EQ(toSettingString<int>(-10), expected(true, "-10"));
EXPECT_EQ(toSettingString<double>(10.2), expected(true, "10.2"));
EXPECT_EQ(toSettingString<double>(1.2e10), expected(true, "1.2e+10"));
EXPECT_EQ(fromSettingString<char>("a", 'b'), std::make_pair(true, 'a'));
EXPECT_EQ(fromSettingString<int>("10", 100), std::make_pair(true, 10));
EXPECT_EQ(fromSettingString<int>("-10", 100), std::make_pair(true, -10));
EXPECT_EQ(fromSettingString<double>("10.2", 2.2), std::make_pair(true, 10.2));
EXPECT_EQ(fromSettingString<double>("1.2e10", 2.2), std::make_pair(true, 1.2e+10));
// Invalid conversion
EXPECT_EQ(fromSettingString<int>("not int", 100), std::make_pair(false, 100));
EXPECT_EQ(fromSettingString<double>("not double", 2.2), std::make_pair(false, 2.2));
}
TEST(SettingStringConversionTest, testFromEnum) {
// Valid conversions
EXPECT_EQ(toSettingString<HelloEnum>(HelloEnum::HI), expected(true, R"("HI")"));
EXPECT_EQ(fromSettingString<HelloEnum>(R"("THERE")", HelloEnum::HI), std::make_pair(true, HelloEnum::THERE));
// Invalid conversion
EXPECT_EQ(fromSettingString<HelloEnum>(R"("BLAH")", HelloEnum::HI), std::make_pair(false, HelloEnum::HI));
EXPECT_EQ(fromSettingString<HelloEnum>("", HelloEnum::HI), std::make_pair(false, HelloEnum::HI));
EXPECT_EQ(fromSettingString<HelloEnum>("-THERE-", HelloEnum::HI), std::make_pair(false, HelloEnum::HI));
}
TEST(SettingStringConversionTest, testFromClass) {
// Valid conversions
HelloClass newValue;
newValue.m_name = "newValue";
EXPECT_EQ(toSettingString<HelloClass>(HelloClass()), expected(true, R"({"key":"value"})"));
EXPECT_EQ(fromSettingString<HelloClass>(R"({"key":"newValue"})", HelloClass()), std::make_pair(true, newValue));
// Invalid conversion
EXPECT_EQ(fromSettingString<HelloClass>("invalid json", HelloClass()), std::make_pair(false, HelloClass()));
}
} // namespace test
} // namespace settings
} // namespace alexaClientSDK