avs-device-sdk/AVSCommon/AVS/test/Attachment/AttachmentWriterTest.cpp

191 lines
6.2 KiB
C++
Raw Normal View History

/*
* Copyright 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 <gtest/gtest.h>
#include <gmock/gmock.h>
#include "AVSCommon/AVS/Attachment/InProcessAttachment.h"
#include "Common/Common.h"
namespace alexaClientSDK {
namespace avsCommon {
namespace avs {
namespace test {
using namespace ::testing;
using namespace alexaClientSDK::avsCommon::avs::attachment;
using namespace alexaClientSDK::avsCommon::utils::sds;
/**
* A class which helps drive this unit test suite.
*/
class AttachmentWriterTest : public ::testing::Test {
public:
/**
* Constructor.
*/
AttachmentWriterTest() = default;
/**
* A function to nitilialize class data structures as needed.
*/
void init();
/**
* A utility function to wrap up an important test for reading test data in multiple passes, and testing
* various points of progress.
*
* @param closeWriterBeforeReading Indicating if the test should close the writer before performing reads.
*/
void testMultipleReads(bool closeWriterBeforeReading);
/// The commonly used SDS in these tests.
std::shared_ptr<InProcessSDS> m_sds;
/// The commonly used reader in these tests.
std::unique_ptr<InProcessAttachmentReader> m_reader;
/// The commonly used writer in these tests.
std::unique_ptr<InProcessAttachmentWriter> m_writer;
/// The commonly used test pattern in these tests.
std::vector<uint8_t> m_testPattern;
};
void AttachmentWriterTest::init() {
m_sds = createSDS(TEST_SDS_BUFFER_SIZE_IN_BYTES);
ASSERT_NE(m_sds, nullptr);
m_writer = InProcessAttachmentWriter::create(m_sds);
ASSERT_NE(m_writer, nullptr);
Version 1.5.0 alexa-client-sdk Changes in this update: **Enhancements** * Added the `ExternalMediaPlayer` Capability Agent. This allows playback from music providers that control their own playback queue. Example: Spotify. * Added support for AU and NZ to the `SampleApp`. * Firmware version can now be sent to Alexa via the `SoftwareInfo` event. The firmware version is specified in the config file under the `sampleApp` object as an integer value named [`firmwareVersion`](https://github.com/alexa/avs-device-sdk/blob/master/Integration/AlexaClientSDKConfig.json#L52). * The new `f` command was added to the `SampleApp` which allows the firmware version to be updated at run-time. * Optional configuration changes have been introduced. Now a [default log level](https://github.com/alexa/avs-device-sdk/blob/master/Integration/AlexaClientSDKConfig.json#L93) can be set for `ACSDK_LOG_MODULE` components, globally or individually. This value is specified under a new root level configuration object called `logger`, and the value itself is named `logLevel`. This allows you to limit the degree of logging to that default value, such as `ERROR`or `INFO`. **Bug Fixes** * Fixed bug where `AudioPlayer` progress reports were not being sent, or were being sent incorrectly. * [Issue 408](https://github.com/alexa/avs-device-sdk/issues/408) - Irrelevant code related to `UrlSource` was removed from the `GStreamer-based MediaPlayer` implementation. * The `TZ` variable no longer needs to be set to `UTC` when building the `SampleApp`. * Fixed a bug where `CurlEasyHandleWrapper` logged unwanted data on failure conditions. * Fixed a bug to improve `SIGPIPE` handling. * Fixed a bug where the filename and classname were mismatched. Changed `UrlToAttachmentConverter.h` to `UrlContentToAttachmentConverter.h`,and `UrlToAttachmentConverter.cpp` to `UrlContentToAttachmentConverter.cpp` **Known Issues** * The `ACL` may encounter issues if audio attachments are received but not consumed. * Display Cards for Kindle don't render. * If using the GStreamer-based `MediaPlayer` implementation, after muting and un-muting an audio item, the next item in the queue will begin playing rather than continuing playback of the originally muted audio item. * `SpeechSynthesizerState` currently uses `GAINING_FOCUS` and `LOSING_FOCUS` as a workaround for handling intermediate state. These states may be removed in a future release. * Music playback doesn't immediately stop when a user barges-in on iHeartRadio.
2018-02-12 23:31:53 +00:00
m_reader = InProcessAttachmentReader::create(ReaderPolicy::NONBLOCKING, m_sds);
ASSERT_NE(m_reader, nullptr);
m_testPattern = createTestPattern(TEST_SDS_BUFFER_SIZE_IN_BYTES);
}
void AttachmentWriterTest::testMultipleReads(bool closeWriterBeforeReading) {
init();
auto writeStatus = InProcessAttachmentWriter::WriteStatus::OK;
auto numWritten = m_writer->write(m_testPattern.data(), m_testPattern.size(), &writeStatus);
ASSERT_EQ(numWritten, m_testPattern.size());
ASSERT_EQ(writeStatus, InProcessAttachmentWriter::WriteStatus::OK);
AttachmentReader::ReadStatus terminalStatus = AttachmentReader::ReadStatus::OK_WOULDBLOCK;
if (closeWriterBeforeReading) {
m_writer->close();
terminalStatus = AttachmentReader::ReadStatus::CLOSED;
}
std::vector<uint8_t> result(TEST_SDS_PARTIAL_READ_AMOUNT_IN_BYTES);
auto readStatus = InProcessAttachmentReader::ReadStatus::OK;
int totalBytesRead = 0;
bool done = false;
int iterations = 0;
int iterationsMax = 10;
while (!done && iterations < iterationsMax) {
auto bytesRead = m_reader->read(result.data(), result.size(), &readStatus);
if (terminalStatus == readStatus) {
done = true;
}
for (size_t i = 0; i < bytesRead; ++i) {
EXPECT_EQ(result[i], m_testPattern[i + totalBytesRead]);
}
totalBytesRead += bytesRead;
iterations++;
}
ASSERT_NE(iterations, iterationsMax);
ASSERT_EQ(readStatus, terminalStatus);
ASSERT_EQ(totalBytesRead, static_cast<ssize_t>(m_testPattern.size()));
}
/**
* Test writing to an invalid SDS.
*/
TEST_F(AttachmentWriterTest, test_attachmentWriterWithInvalidSDS) {
auto writer = InProcessAttachmentWriter::create(nullptr);
ASSERT_EQ(writer, nullptr);
}
/**
* Test writing to a closed writer.
*/
TEST_F(AttachmentWriterTest, test_attachmentWriterOnClosedWriter) {
init();
m_writer->close();
AttachmentWriter::WriteStatus writeStatus = AttachmentWriter::WriteStatus::OK;
auto numWritten = m_writer->write(m_testPattern.data(), TEST_SDS_PARTIAL_WRITE_AMOUNT_IN_BYTES, &writeStatus);
ASSERT_EQ(numWritten, 0U);
ASSERT_EQ(writeStatus, InProcessAttachmentWriter::WriteStatus::CLOSED);
}
/**
* Test writing a single pass of data.
*/
TEST_F(AttachmentWriterTest, test_attachmentWriterWriteSinglePass) {
init();
AttachmentWriter::WriteStatus writeStatus = AttachmentWriter::WriteStatus::OK;
auto numWritten = m_writer->write(m_testPattern.data(), TEST_SDS_PARTIAL_WRITE_AMOUNT_IN_BYTES, &writeStatus);
ASSERT_EQ(static_cast<ssize_t>(numWritten), TEST_SDS_PARTIAL_WRITE_AMOUNT_IN_BYTES);
ASSERT_EQ(writeStatus, InProcessAttachmentWriter::WriteStatus::OK);
}
/**
* Test a one-pass write and read with both wrapper classes.
*/
TEST_F(AttachmentWriterTest, test_attachmentWriterAndReadInOnePass) {
init();
auto writeStatus = InProcessAttachmentWriter::WriteStatus::OK;
auto numWritten = m_writer->write(m_testPattern.data(), m_testPattern.size(), &writeStatus);
ASSERT_EQ(numWritten, m_testPattern.size());
ASSERT_EQ(writeStatus, InProcessAttachmentWriter::WriteStatus::OK);
std::vector<uint8_t> result(m_testPattern.size());
auto readStatus = InProcessAttachmentReader::ReadStatus::OK;
auto numRead = m_reader->read(result.data(), result.size(), &readStatus);
ASSERT_EQ(numRead, m_testPattern.size());
ASSERT_EQ(readStatus, InProcessAttachmentReader::ReadStatus::OK);
for (size_t i = 0; i < m_testPattern.size(); ++i) {
EXPECT_EQ(result[i], m_testPattern[i]);
}
}
/**
* Test multiple partial reads of complete data, where the writer is closed.
*/
TEST_F(AttachmentWriterTest, test_attachmentReaderAndWriterMultipleReads) {
testMultipleReads(true);
}
/**
* Test multiple partial reads of complete data, where the writer remains open.
*/
TEST_F(AttachmentWriterTest, test_attachmentWriterAndReaderMultipleReadsOfUnfinishedData) {
testMultipleReads(false);
}
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
} // namespace test
} // namespace avs
} // namespace avsCommon
} // namespace alexaClientSDK