From 723dfb908d40f53bcb017850a70cfd9131c66a11 Mon Sep 17 00:00:00 2001 From: aangerma Date: Wed, 11 Nov 2020 17:32:31 +0200 Subject: [PATCH] Added unit tests for alt-ir --- .../test-enable-before-stream-start.cpp | 33 ++++++ .../alt-IR/test-enable-while-streaming.cpp | 32 ++++++ unit-tests/functional/alt-IR/test-sanity.cpp | 38 +++++++ unit-tests/functional/utilities.h | 97 ++++++++++++++++ unit-tests/tests.h | 104 ++++++++++++++++++ 5 files changed, 304 insertions(+) create mode 100644 unit-tests/functional/alt-IR/test-enable-before-stream-start.cpp create mode 100644 unit-tests/functional/alt-IR/test-enable-while-streaming.cpp create mode 100644 unit-tests/functional/alt-IR/test-sanity.cpp create mode 100644 unit-tests/functional/utilities.h create mode 100644 unit-tests/tests.h diff --git a/unit-tests/functional/alt-IR/test-enable-before-stream-start.cpp b/unit-tests/functional/alt-IR/test-enable-before-stream-start.cpp new file mode 100644 index 00000000000..ac39d408bf2 --- /dev/null +++ b/unit-tests/functional/alt-IR/test-enable-before-stream-start.cpp @@ -0,0 +1,33 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + + +#include "../../tests.h" +#include "../utilities.h" + +using namespace rs2; + +TEST_CASE( "AC fails if AltIR was enable before stream start", "[l500][live]" ) +{ + + auto devices = find_device_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 ); + auto dev = devices[0]; + std::string serial; + REQUIRE_NOTHROW( serial = dev.get_info( RS2_CAMERA_INFO_SERIAL_NUMBER ) ); + + auto depth_sens = get_depth_sensor( dev ); + + const uint32_t MIN_FW_VER[4] = { 1, 5, 2, 0 }; + if( is_fw_version_newer( depth_sens, MIN_FW_VER ) ) + { + REQUIRE( depth_sens.supports( RS2_OPTION_ALTERNATE_IR ) ); + REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, 1 ) ); + + auto profiles = find_profiles_to_stream( depth_sens ); + enable_alt_ir_and_check_that_AC_fails( dev, get_depth_sensor( dev ), + profiles ); + } + else + std::cout << depth_sens.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION ) + << " doesn't support alt IR option"; +} \ No newline at end of file diff --git a/unit-tests/functional/alt-IR/test-enable-while-streaming.cpp b/unit-tests/functional/alt-IR/test-enable-while-streaming.cpp new file mode 100644 index 00000000000..37232100878 --- /dev/null +++ b/unit-tests/functional/alt-IR/test-enable-while-streaming.cpp @@ -0,0 +1,32 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + + +#include "../../tests.h" +#include "../utilities.h" + +using namespace rs2; + +TEST_CASE( "AC fails if AltIR was enable after stream start", "[l500][live]" ) +{ + auto devices = find_device_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 ); + auto dev = devices[0]; + + std::string serial; + REQUIRE_NOTHROW( serial = dev.get_info( RS2_CAMERA_INFO_SERIAL_NUMBER ) ); + + + auto depth_sens = get_depth_sensor( dev ); + + const uint32_t MIN_FW_VER[4] = { 1, 5, 2, 0 }; + if( is_fw_version_newer( depth_sens, MIN_FW_VER ) ) + { + auto profiles = find_profiles_to_stream( depth_sens ); + enable_alt_ir_and_check_that_AC_fails( dev, get_depth_sensor( dev ), + profiles ); + } + else + std::cout << depth_sens.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION ) + << " doesn't support alt IR option"; + +} \ No newline at end of file diff --git a/unit-tests/functional/alt-IR/test-sanity.cpp b/unit-tests/functional/alt-IR/test-sanity.cpp new file mode 100644 index 00000000000..36acd906efa --- /dev/null +++ b/unit-tests/functional/alt-IR/test-sanity.cpp @@ -0,0 +1,38 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#include "../../tests.h" + +using namespace rs2; + +TEST_CASE( "AltIR", "[l500][live]" ) +{ + + auto devices = find_device_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 ); + auto dev = devices[0]; + + std::string serial; + REQUIRE_NOTHROW( serial = dev.get_info( RS2_CAMERA_INFO_SERIAL_NUMBER ) ); + + auto depth_sens = get_depth_sensor( dev ); + + const uint32_t MIN_FW_VER[4] = { 1, 5, 2, 0 }; + if( is_fw_version_newer( depth_sens, MIN_FW_VER ) ) + { + REQUIRE( depth_sens.supports( RS2_OPTION_ALTERNATE_IR ) ); + option_range r; + REQUIRE_NOTHROW( r = depth_sens.get_option_range( RS2_OPTION_ALTERNATE_IR ) ); + REQUIRE( depth_sens.get_option( RS2_OPTION_ALTERNATE_IR ) == r.def ); + + for( auto i = r.min; i <= r.max; i++ ) + { + REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, i ) ); + REQUIRE( depth_sens.get_option( RS2_OPTION_ALTERNATE_IR ) == i ); + } + + REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, r.def ) ); + } + else + std::cout << depth_sens.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION ) + << " doesn't support alt IR option"; +} diff --git a/unit-tests/functional/utilities.h b/unit-tests/functional/utilities.h new file mode 100644 index 00000000000..f5b4f0bb9c9 --- /dev/null +++ b/unit-tests/functional/utilities.h @@ -0,0 +1,97 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +#include "../../tests.h" +#include "../include/librealsense2/rs.hpp" +#include "../include/librealsense2/hpp/rs_context.hpp" +#include "../include/librealsense2/hpp/rs_internal.hpp" +#include "concurrency.h" + +using namespace rs2; + +std::vector< stream_profile > find_profiles_to_stream( rs2::depth_sensor depth_sens ) +{ + std::vector< stream_profile > stream_profiles; + REQUIRE_NOTHROW( stream_profiles = depth_sens.get_stream_profiles() ); + + auto depth_profile + = std::find_if( stream_profiles.begin(), stream_profiles.end(), []( stream_profile sp ) { + return sp.is_default() && sp.stream_type() == RS2_STREAM_DEPTH; + } ); + + auto ir_profile + = std::find_if( stream_profiles.begin(), stream_profiles.end(), []( stream_profile sp ) { + return sp.is_default() && sp.stream_type() == RS2_STREAM_INFRARED; + } ); + + auto confidence_profile + = std::find_if( stream_profiles.begin(), stream_profiles.end(), [&]( stream_profile sp ) { + return sp.stream_type() == RS2_STREAM_CONFIDENCE + && sp.as< rs2::video_stream_profile >().width() + == ir_profile->as< rs2::video_stream_profile >().width() + && sp.as< rs2::video_stream_profile >().height() + == ir_profile->as< rs2::video_stream_profile >().height(); + } ); + + return { *depth_profile, *ir_profile, *confidence_profile }; +} + +void enable_alt_ir_and_check_that_AC_fails( const rs2::device& dev, + const rs2::depth_sensor& depth_sens, + const std::vector< stream_profile > & expected_profiles ) +{ + std::vector< stream_profile > profiles = expected_profiles; + REQUIRE_NOTHROW( depth_sens.open( profiles ) ); + + std::condition_variable cv; + std::mutex m; + + // wait for all streams + // scope for unique_lock + { + REQUIRE_NOTHROW( depth_sens.start( [&]( rs2::frame f ) { + std::unique_lock< std::mutex > lock( m ); + remove_all_streams_arrived( f, profiles ); + cv.notify_one(); + } ) ); + + std::unique_lock< std::mutex > lock( m ); + REQUIRE( cv.wait_for( lock, std::chrono::seconds( 20 ), [&]() { + return profiles.size() == 0; + } ) ); + } + + // set alt_ir if it was disabled + REQUIRE( depth_sens.supports( RS2_OPTION_ALTERNATE_IR ) ); + auto alt_ir = depth_sens.get_option( RS2_OPTION_ALTERNATE_IR ); + if( alt_ir == 0) + REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, 1 ) ); + + // check that AC throws exception + if( dev.is< rs2::device_calibration >() ) + { + auto calib = dev.as< rs2::device_calibration >(); + + rs2_calibration_status status; + calib.register_calibration_change_callback( [&]( rs2_calibration_status cal_status ) { + std::unique_lock< std::mutex > lock( m ); + status = cal_status; + cv.notify_one(); + } ); + + REQUIRE_THROWS( calib.trigger_device_calibration( RS2_CALIBRATION_MANUAL_DEPTH_TO_RGB ) ); + + std::unique_lock< std::mutex > lock( m ); + REQUIRE( cv.wait_for( lock, std::chrono::seconds( 20 ), [&]() { + return status == RS2_CALIBRATION_BAD_CONDITIONS; + } ) ); + } + + depth_sens.stop(); + depth_sens.close(); + + // return alt IR to default + option_range r; + REQUIRE_NOTHROW( r = depth_sens.get_option_range( RS2_OPTION_ALTERNATE_IR ) ); + REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, r.def ) ); +} diff --git a/unit-tests/tests.h b/unit-tests/tests.h new file mode 100644 index 00000000000..d374ad4717a --- /dev/null +++ b/unit-tests/tests.h @@ -0,0 +1,104 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2015 Intel Corporation. All Rights Reserved. + +#pragma once +#ifndef TESTS_H +#define TESTS_H + +#include "../include/librealsense2/rs.hpp" + +#define CATCH_CONFIG_MAIN + +#include "catch.h" + +#include +#ifdef BUILD_SHARED_LIBS +// With static linkage, ELPP is initialized by librealsense, so doing it here will +// create errors. When we're using the shared .so/.dll, the two are separate and we have +// to initialize ours if we want to use the APIs! +INITIALIZE_EASYLOGGINGPP +#endif + +inline rs2::device_list find_device_by_product_line_or_exit( int product ) +{ + rs2::context ctx; + rs2::device_list devices_list = ctx.query_devices( product ); + size_t device_count = devices_list.size(); + if( device_count == 0 ) + { + std::cout << "No L500 device connected "; + exit( 0 ); + } + + return devices_list; +} + +inline rs2::depth_sensor get_depth_sensor( rs2::device dev ) +{ + auto sensors = dev.query_sensors(); + + auto it = std::find_if( sensors.begin(), sensors.end(), []( rs2::sensor s ) { + return s.is< rs2::depth_sensor >(); + } ); + + return *it; +} + +inline void remove_all_streams_arrived( rs2::frame f, + std::vector< rs2::stream_profile > & stream_to_arrive ) +{ + auto remove_stream = [&]() { + auto it = std::remove_if( stream_to_arrive.begin(), + stream_to_arrive.end(), + [&]( rs2::stream_profile s ) { + return s.stream_type() == f.get_profile().stream_type(); + } ); + + + if( it != stream_to_arrive.end() ) + stream_to_arrive.erase( it ); + }; + + if( f.is< rs2::frameset >() ) + { + auto set = f.as< rs2::frameset >(); + set.foreach_rs( [&]( rs2::frame fr ) { remove_stream(); } ); + } + else + { + remove_stream(); + } +} + +inline std::vector< uint32_t > split( const std::string & s, char delim ) +{ + std::stringstream ss( s ); + std::string item; + std::vector< uint32_t > tokens; + while( std::getline( ss, item, delim ) ) + { + tokens.push_back( std::stoi( item, nullptr ) ); + } + return tokens; +} + +inline bool is_fw_version_newer( rs2::sensor & subdevice, const uint32_t other_fw[4] ) +{ + std::string fw_version_str; + REQUIRE_NOTHROW( fw_version_str = subdevice.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION ) ); + auto fw = split( fw_version_str, '.' ); + if( fw[0] > other_fw[0] ) + return true; + if( fw[0] == other_fw[0] && fw[1] > other_fw[1] ) + return true; + if( fw[0] == other_fw[0] && fw[1] == other_fw[1] && fw[2] > other_fw[2] ) + return true; + if( fw[0] == other_fw[0] && fw[1] == other_fw[1] && fw[2] == other_fw[2] + && fw[3] > other_fw[3] ) + return true; + if( fw[0] == other_fw[0] && fw[1] == other_fw[1] && fw[2] == other_fw[2] + && fw[3] == other_fw[3] ) + return true; + return false; +} +#endif