avs-device-sdk/CapabilityAgents/DoNotDisturb/test/DoNotDisturbCapabilityAgent...

280 lines
10 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <AVSCommon/SDKInterfaces/MockDirectiveSequencer.h>
#include <AVSCommon/SDKInterfaces/MockExceptionEncounteredSender.h>
#include <AVSCommon/SDKInterfaces/MockMessageSender.h>
#include <RegistrationManager/CustomerDataManager.h>
#include <Settings/MockDeviceSettingStorage.h>
#include "DoNotDisturbCA/DoNotDisturbCapabilityAgent.h"
namespace alexaClientSDK {
namespace capabilityAgents {
namespace doNotDisturb {
namespace test {
using namespace ::testing;
using namespace avsCommon::avs;
using namespace avsCommon::sdkInterfaces;
using namespace avsCommon::sdkInterfaces::test;
using namespace avsCommon::utils;
using namespace registrationManager;
using namespace settings::storage;
using namespace settings::storage::test;
using namespace settings;
/// Amount of time for the test to wait for event to be sent.
static const std::chrono::seconds WAIT_TIMEOUT(2);
/// A sample Directive JSON string for the purposes of creating an AVSDirective object.
static const std::string SETDNDMODE_DIRECTIVE_VALID_JSON_STRING = R"delim(
{
"directive": {
"header": {
"namespace": "DoNotDisturb",
"name": "SetDoNotDisturb",
"messageId": "12345"
},
"payload": {
"enabled": true
}
}
})delim";
/// "Report" event for DoNotDisturb API
static const std::string DND_REPORT_EVENT = "ReportDoNotDisturb";
/// "Changed" event for DoNotDisturb API
static const std::string DND_CHANGE_EVENT = "DoNotDisturbChanged";
/// Test harness for @c DoNotDisturbCapabilityAgent class.
class DoNotDisturbCapabilityAgentTest : public ::testing::Test {
public:
/// Set up the test harness for running a test.
void SetUp() override;
protected:
void TearDown() override;
public:
/// Helper method to set up mocks for an upcoming event.
bool expectEventSend(
const std::string& eventName,
MessageRequestObserverInterface::Status statusReported,
std::function<void()> triggerOperation);
protected:
/// The InteractionModelCapabilityAgent instance to be tested
std::shared_ptr<DoNotDisturbCapabilityAgent> m_dndCA;
/// Message sender mock to track messages being sent.
std::shared_ptr<MockMessageSender> m_messageSender;
/// The mock @c ExceptionEncounteredSenderInterface.
std::shared_ptr<avsCommon::sdkInterfaces::test::MockExceptionEncounteredSender> m_mockExceptionEncounteredSender;
/// Customer data manager to handle the factory reset logic.
std::shared_ptr<CustomerDataManager> m_customerDataManager;
/// An instace of @c DeviceSettingsManager,
std::shared_ptr<DeviceSettingsManager> m_settingsManager;
/// Storage for the settings.
std::shared_ptr<MockDeviceSettingStorage> m_settingsStorage;
};
void DoNotDisturbCapabilityAgentTest::SetUp() {
m_customerDataManager = std::make_shared<CustomerDataManager>();
m_messageSender = std::make_shared<MockMessageSender>();
m_mockExceptionEncounteredSender =
std::make_shared<avsCommon::sdkInterfaces::test::MockExceptionEncounteredSender>();
m_settingsManager = std::make_shared<DeviceSettingsManager>();
m_settingsStorage = std::make_shared<MockDeviceSettingStorage>();
EXPECT_CALL(*m_settingsStorage, storeSetting(_, _, _)).WillRepeatedly(Return(true));
EXPECT_CALL(*m_settingsStorage, updateSettingStatus(_, _)).WillRepeatedly(Return(true));
m_dndCA = DoNotDisturbCapabilityAgent::create(
m_customerDataManager, m_mockExceptionEncounteredSender, m_messageSender, m_settingsManager, m_settingsStorage);
ASSERT_NE(m_dndCA, nullptr);
}
bool DoNotDisturbCapabilityAgentTest::expectEventSend(
const std::string& eventName,
MessageRequestObserverInterface::Status statusReported,
std::function<void()> triggerOperation) {
std::promise<bool> eventPromise;
EXPECT_CALL(*m_messageSender, sendMessage(_))
.Times(1)
.WillOnce(Invoke([statusReported, &eventName, &eventPromise](std::shared_ptr<MessageRequest> request) {
if (request->getJsonContent().find(eventName) != std::string::npos) {
request->sendCompleted(statusReported);
eventPromise.set_value(true);
return;
}
// Unexpected event
eventPromise.set_value(false);
}));
triggerOperation();
auto future = eventPromise.get_future();
bool isFutureReady = future.wait_for(WAIT_TIMEOUT) == std::future_status::ready;
EXPECT_TRUE(isFutureReady);
if (!isFutureReady) {
return false;
}
return future.get();
}
void DoNotDisturbCapabilityAgentTest::TearDown() {
if (m_dndCA) {
m_dndCA->shutdown();
}
}
TEST_F(DoNotDisturbCapabilityAgentTest, givenInvalidParameters_create_shouldFail) {
auto dndCA = DoNotDisturbCapabilityAgent::create(
nullptr, m_mockExceptionEncounteredSender, m_messageSender, m_settingsManager, m_settingsStorage);
EXPECT_THAT(dndCA, IsNull());
dndCA = DoNotDisturbCapabilityAgent::create(
m_customerDataManager, nullptr, m_messageSender, m_settingsManager, m_settingsStorage);
EXPECT_THAT(dndCA, IsNull());
dndCA = DoNotDisturbCapabilityAgent::create(
m_customerDataManager, m_mockExceptionEncounteredSender, nullptr, m_settingsManager, m_settingsStorage);
EXPECT_THAT(dndCA, IsNull());
dndCA = DoNotDisturbCapabilityAgent::create(
m_customerDataManager, m_mockExceptionEncounteredSender, m_messageSender, nullptr, m_settingsStorage);
EXPECT_THAT(dndCA, IsNull());
dndCA = DoNotDisturbCapabilityAgent::create(
m_customerDataManager, m_mockExceptionEncounteredSender, m_messageSender, m_settingsManager, nullptr);
EXPECT_THAT(dndCA, IsNull());
}
TEST_F(DoNotDisturbCapabilityAgentTest, givenValidSetDNDDirective_handleDirective_shouldSucceed) {
// Become online
bool initialReportSent =
expectEventSend(DND_REPORT_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this]() {
m_dndCA->onConnectionStatusChanged(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::SUCCESS);
});
ASSERT_TRUE(initialReportSent);
auto directivePair = AVSDirective::create(SETDNDMODE_DIRECTIVE_VALID_JSON_STRING, nullptr, "");
std::shared_ptr<AVSDirective> directive = std::move(directivePair.first);
bool directiveResponseEventSent =
expectEventSend(DND_REPORT_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this, &directive]() {
m_dndCA->handleDirectiveImmediately(directive);
});
ASSERT_TRUE(directiveResponseEventSent);
}
TEST_F(DoNotDisturbCapabilityAgentTest, beingOnline_applyLocalChange_shouldSendReport) {
bool initialReportSent =
expectEventSend(DND_REPORT_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this]() {
m_dndCA->onConnectionStatusChanged(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::SUCCESS);
});
ASSERT_TRUE(initialReportSent);
bool changeEventSent =
expectEventSend(DND_CHANGE_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this]() {
m_dndCA->sendChangedEvent("true");
});
ASSERT_TRUE(changeEventSent);
}
TEST_F(DoNotDisturbCapabilityAgentTest, beingOffline_applyLocalChangeAndBecomeOnline_shouldSendChanged) {
// Apply change while offline
m_dndCA->sendChangedEvent("true");
bool changeEventSent =
expectEventSend(DND_CHANGE_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this]() {
m_dndCA->onConnectionStatusChanged(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::SUCCESS);
});
ASSERT_TRUE(changeEventSent);
}
TEST_F(DoNotDisturbCapabilityAgentTest, whileSendingChangedEvent_sendChangedFail_shouldSendReport) {
// Become online and ignore the first "report" event.
bool initialReportSent =
expectEventSend(DND_REPORT_EVENT, MessageRequestObserverInterface::Status::SUCCESS, [this]() {
m_dndCA->onConnectionStatusChanged(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::SUCCESS);
});
ASSERT_TRUE(initialReportSent);
int eventMask = 0;
std::promise<bool> eventPromise;
EXPECT_CALL(*m_messageSender, sendMessage(_))
.Times(2)
.WillRepeatedly(Invoke([&eventMask, &eventPromise](std::shared_ptr<MessageRequest> request) {
if (request->getJsonContent().find(DND_CHANGE_EVENT) != std::string::npos) {
eventMask <<= 1;
eventMask |= 1;
request->sendCompleted(MessageRequestObserverInterface::Status::INTERNAL_ERROR);
return;
} else if (request->getJsonContent().find(DND_REPORT_EVENT) != std::string::npos) {
eventMask <<= 1;
eventPromise.set_value(true);
return;
}
// Unexpected event
eventPromise.set_value(false);
}));
m_dndCA->sendChangedEvent("true");
auto future = eventPromise.get_future();
bool isFutureReady = future.wait_for(WAIT_TIMEOUT) == std::future_status::ready;
EXPECT_TRUE(isFutureReady);
// Check the order and existence of the events: Changed first, Report second, both happened.
EXPECT_EQ(eventMask, 2);
}
} // namespace test
} // namespace doNotDisturb
} // namespace capabilityAgents
} // namespace alexaClientSDK