avs-device-sdk/AVSCommon/Utils/test/StopwatchTest.cpp

196 lines
5.4 KiB
C++
Raw Normal View History

Version 1.10 alexa-client-sdk Changes in this update: **Enhancements** * New optional configuration for [EqualizerController](https://github.com/alexa/avs-device-sdk/blob/v1.10.0/Integration/AlexaClientSDKConfig.json#L154). The EqualizerController interface allows you to adjust equalizer settings on your product, such as decibel (dB) levels and modes. * Added reference implementation of the EqualizerController for GStreamer-based (MacOS, Linux, and Raspberry Pi) and OpenSL ES-based (Android) MediaPlayers. Note: In order to use with Android, it must support OpenSL ES. * New optional configuration for the [TemplateRuntime display card value](https://github.com/alexa/avs-device-sdk/blob/v1.10.0/Integration/AlexaClientSDKConfig.json#L144). * A configuration file generator script, `genConfig.sh` is now included with the SDK in the **tools/Install** directory. `genConfig.sh` and it's associated arguments populate `AlexaClientSDKConfig.json` with the data required to authorize with LWA. * Added Bluetooth A2DP source and AVRCP target support for Linux. * Added Amazon for Business (A4B) support, which includes support for handling the new [RevokeAuthorization](https://developer.amazon.com/docs/alexa-voice-service/system.html#revokeauth) directive in the Settings interface. A new CMake option has been added to enable A4B within the SDK, `-DA4B`. * Added locale support for IT and ES. * The Alexa Communication Library (ACL), `CBLAUthDelegate`, and sample app have been enhanced to detect de-authorization using the new `z` command. * Added `ExternalMediaPlayerObserver`, which receives notification of player state, track, and username changes. * `HTTP2ConnectionInterface` was factored out of `HTTP2Transport` to enable unit testing of `HTTP2Transport` and re-use of `HTTP2Connection` logic. **Bug Fixes** * Fixed a bug in which `ExternalMediaPlayer` adapter playback wasn't being recognized by AVS. * [Issue 973](https://github.com/alexa/avs-device-sdk/issues/973) - Fixed issues related to `AudioPlayer` where progress reports were being sent out of order or with incorrect offsets. * An `EXPECTING`, state has been added to `DialogUXState` in order to handle `EXPECT_SPEECH` state for hold-to-talk devices. * [Issue 948](https://github.com/alexa/avs-device-sdk/issues/948) - Fixed a bug in which the sample app was stuck in a listening state. * Fixed a bug where there was a delay between receiving a `DeleteAlert` directive, and deleting the alert. * [Issue 839](https://github.com/alexa/avs-device-sdk/issues/839) - Fixed an issue where speech was being truncated due to the `DialogUXStateAggregator` transitioning between a `THINKING` and `IDLE` state. * Fixed a bug in which the `AudioPlayer` attempted to play when it wasn't in the `FOREGROUND` focus. * `CapabilitiesDelegateTest` now works on Android. * [Issue 950](https://github.com/alexa/avs-device-sdk/issues/950) - Improved Android Media Player audio quality. * [Issue 908](https://github.com/alexa/avs-device-sdk/issues/908) - Fixed compile error on g++ 7.x in which includes were missing.
2018-10-24 17:01:29 +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.
*/
/// @file StopwatchTest.cpp
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
#include "AVSCommon/Utils/Timing/Stopwatch.h"
namespace alexaClientSDK {
namespace avsCommon {
namespace utils {
namespace timing {
namespace test {
static const std::chrono::milliseconds TESTABLE_TIME_INCREMENT{100};
/// Test harness for Stopwatch class.
class StopwatchTest : public ::testing::Test {
public:
bool checkElapsed(int expectedIncrement);
Stopwatch m_stopwatch;
};
bool StopwatchTest::checkElapsed(int expectedIncrement) {
auto elapsed = m_stopwatch.getElapsed();
if (elapsed < TESTABLE_TIME_INCREMENT * (expectedIncrement - 1)) {
return false;
}
if (elapsed > TESTABLE_TIME_INCREMENT * (expectedIncrement + 1)) {
return false;
}
return true;
}
/**
* Test good sequencing of method calls.
*/
TEST_F(StopwatchTest, goodSequencing) {
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
m_stopwatch.stop();
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
m_stopwatch.stop();
}
/**
* Test bad sequencing of method calls.
*/
TEST_F(StopwatchTest, badSequencing) {
// Must be reset to start().
ASSERT_TRUE(m_stopwatch.start());
ASSERT_FALSE(m_stopwatch.start());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_FALSE(m_stopwatch.start());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_FALSE(m_stopwatch.start());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
m_stopwatch.stop();
ASSERT_FALSE(m_stopwatch.start());
// Must be started to pause().
m_stopwatch.reset();
ASSERT_FALSE(m_stopwatch.pause());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_FALSE(m_stopwatch.pause());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_TRUE(m_stopwatch.pause());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
m_stopwatch.stop();
ASSERT_FALSE(m_stopwatch.pause());
// Must be paused to resume().
m_stopwatch.reset();
ASSERT_FALSE(m_stopwatch.resume());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_FALSE(m_stopwatch.resume());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_FALSE(m_stopwatch.resume());
m_stopwatch.reset();
ASSERT_TRUE(m_stopwatch.start());
m_stopwatch.stop();
ASSERT_FALSE(m_stopwatch.resume());
}
/**
* Test report of elapsed time. This test is timing sensitive.
*/
TEST_F(StopwatchTest, testElapsed) {
// Expect progression after start().
ASSERT_TRUE(m_stopwatch.start());
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(2));
// Expect NO progression during pause().
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(checkElapsed(2));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(2));
// Expect progression after resume().
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_TRUE(checkElapsed(2));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(4));
// Expect NO progression during pause().
ASSERT_TRUE(m_stopwatch.pause());
ASSERT_TRUE(checkElapsed(4));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(4));
// Expect progression after resume().
ASSERT_TRUE(m_stopwatch.resume());
ASSERT_TRUE(checkElapsed(4));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(6));
// Expect NO progression after stop().
m_stopwatch.stop();
ASSERT_TRUE(checkElapsed(6));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(6));
// Expect NO progression after reset().
m_stopwatch.reset();
ASSERT_TRUE(checkElapsed(0));
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(0));
// Expect start() works after reset()..
ASSERT_TRUE(m_stopwatch.start());
std::this_thread::sleep_for(TESTABLE_TIME_INCREMENT * 2);
ASSERT_TRUE(checkElapsed(2));
}
} // namespace test
} // namespace timing
} // namespace utils
} // namespace avsCommon
} // namespace alexaClientSDK