Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unitests for alt-ir #7741

Merged
merged 7 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions unit-tests/func/alt-ir/alt-ir-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../../test.h"
#include <concurrency.h>

using namespace rs2;


bool alt_ir_supported_or_message( const rs2::depth_sensor & depth_sens )
{
REQUIRE( depth_sens );

if( depth_sens.supports( RS2_OPTION_ALTERNATE_IR ) )
return true;

std::cout << "FW version " << depth_sens.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION )
<< " doesn't support alt IR option";
return false;
}

void set_alt_ir_if_needed( const rs2::depth_sensor & depth_sens, float val )
{
REQUIRE( depth_sens.supports( RS2_OPTION_ALTERNATE_IR ) );
auto alt_ir = depth_sens.get_option( RS2_OPTION_ALTERNATE_IR );
if( alt_ir != val )
REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, val ) );
}

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 ) );
REQUIRE_NOTHROW( depth_sens.start( [&]( rs2::frame f ) {} ) );

set_alt_ir_if_needed( depth_sens, 1 );

auto calib = dev.as< rs2::device_calibration >();
if( calib )
{
rs2_calibration_status status;
calib.register_calibration_change_callback( [&]( rs2_calibration_status cal_status ) {
status = cal_status;
} );

// Before throwing, AC will notify of a BAD_CONDITIONS status
REQUIRE_THROWS( calib.trigger_device_calibration( RS2_CALIBRATION_MANUAL_DEPTH_TO_RGB ) );

REQUIRE( status == RS2_CALIBRATION_BAD_CONDITIONS );
}

REQUIRE_NOTHROW( depth_sens.stop() );
REQUIRE_NOTHROW( depth_sens.close() );
}

void enable_alt_ir_and_check_that_all_streams_arrived(
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;

auto wait_for_streams = [&]() {
profiles = expected_profiles;
std::unique_lock< std::mutex > lock( m );
REQUIRE( cv.wait_for( lock, std::chrono::seconds( 20 ), [&]() {
maloel marked this conversation as resolved.
Show resolved Hide resolved
return profiles.size() == 0;
} ) );
};

REQUIRE_NOTHROW( depth_sens.start( [&]( rs2::frame f ) {
std::unique_lock< std::mutex > lock( m );
remove_all_streams_arrived( f, profiles );
cv.notify_one();
} ) );

set_alt_ir_if_needed( depth_sens, 1 );
wait_for_streams();

set_alt_ir_if_needed( depth_sens, 0 );
wait_for_streams();

set_alt_ir_if_needed( depth_sens, 1 );
wait_for_streams();

REQUIRE_NOTHROW( depth_sens.stop() );
REQUIRE_NOTHROW( depth_sens.close() );

}
24 changes: 24 additions & 0 deletions unit-tests/func/alt-ir/test-ac-before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../func-common.h"
#include "alt-ir-common.h"

using namespace rs2;

TEST_CASE( "AC fails if AltIR was enabled before stream start", "[l500][live]" )
{
auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, 1 ) );

auto depth = find_default_depth_profile( depth_sens );
auto ir = find_default_ir_profile( depth_sens );

enable_alt_ir_and_check_that_AC_fails( dev, depth_sens, { depth, ir } );
}
}
22 changes: 22 additions & 0 deletions unit-tests/func/alt-ir/test-ac-while.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../func-common.h"
#include "alt-ir-common.h"

using namespace rs2;

TEST_CASE( "AC fails if AltIR was enable after stream start", "[l500][live]" )
{
auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
auto depth = find_default_depth_profile( depth_sens );
auto ir = find_default_ir_profile( depth_sens );

enable_alt_ir_and_check_that_AC_fails( dev, depth_sens, { depth, ir } );
}
}
39 changes: 39 additions & 0 deletions unit-tests/func/alt-ir/test-sanity-while-streaming.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../func-common.h"
#include "alt-ir-common.h"

using namespace rs2;

TEST_CASE( "AltIR", "[l500][live]" )
{

auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
option_range r;
REQUIRE_NOTHROW( r = depth_sens.get_option_range( RS2_OPTION_ALTERNATE_IR ) );

auto depth = find_default_depth_profile( depth_sens );
auto ir = find_default_ir_profile( depth_sens );
auto confidence = find_confidence_corresponding_to_depth( depth_sens, depth );

REQUIRE_NOTHROW( depth_sens.open( { depth, ir, confidence } ) );
REQUIRE_NOTHROW( depth_sens.start( [&]( rs2::frame f ) {} ) );

for( auto i = r.min; i <= r.max; i+=r.step )
{
// We expect that no delay is needed between set and get, as would be the case
// with XU options (during streaming)
CHECK_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, i ) );
CHECK( depth_sens.get_option( RS2_OPTION_ALTERNATE_IR ) == i );
}

depth_sens.stop();
depth_sens.close();
}
}
27 changes: 27 additions & 0 deletions unit-tests/func/alt-ir/test-sanity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../func-common.h"
#include "alt-ir-common.h"

using namespace rs2;

TEST_CASE( "AltIR", "[l500][live]" )
{
auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
option_range r;
REQUIRE_NOTHROW( r = depth_sens.get_option_range( RS2_OPTION_ALTERNATE_IR ) );

for( auto i = r.min; i <= r.max; i += r.step )
{
// Outside of streaming, no delay is expected between set and get
CHECK_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, i ) );
CHECK( depth_sens.get_option( RS2_OPTION_ALTERNATE_IR ) == i );
}
}
}
28 changes: 28 additions & 0 deletions unit-tests/func/alt-ir/test-streamimg-before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../func-common.h" //todo functional-common.h
#include "alt-ir-common.h" //todo alt-ir-common.h


using namespace rs2;

TEST_CASE( "Enable AltIR before stream start and check that all streams arrived", "[l500][live]" )
{
auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
REQUIRE_NOTHROW( depth_sens.set_option( RS2_OPTION_ALTERNATE_IR, 1 ) );

auto depth = find_default_depth_profile( depth_sens );
auto ir = find_default_ir_profile( depth_sens );
auto confidence = find_confidence_corresponding_to_depth( depth_sens, depth );

enable_alt_ir_and_check_that_all_streams_arrived( dev,
depth_sens,
{ depth, ir, confidence } );
}
}
26 changes: 26 additions & 0 deletions unit-tests/func/alt-ir/test-streaming-while.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.


#include "../func-common.h"
#include "alt-ir-common.h"

using namespace rs2;

TEST_CASE( "Enable AltIR while streaming and check that all streams arrived", "[l500][live]" )
{
auto devices = find_devices_by_product_line_or_exit( RS2_PRODUCT_LINE_L500 );
auto dev = devices[0];

auto depth_sens = dev.first< rs2::depth_sensor >();
if( alt_ir_supported_or_message( depth_sens ) )
{
auto depth = find_default_depth_profile( depth_sens );
auto ir = find_default_ir_profile( depth_sens );
auto confidence = find_confidence_corresponding_to_depth( depth_sens, depth );

enable_alt_ir_and_check_that_all_streams_arrived( dev,
depth_sens,
{ depth, ir, confidence } );
}
}
96 changes: 96 additions & 0 deletions unit-tests/func/func-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

#include "../test.h"
#include "librealsense2/rs.hpp"

using namespace rs2;

inline rs2::device_list find_devices_by_product_line_or_exit( int product )
maloel marked this conversation as resolved.
Show resolved Hide resolved
{
rs2::context ctx;
rs2::device_list devices_list = ctx.query_devices( product );
if( devices_list.size() == 0 )
{
std::cout << "No device of the " << product << " product line was found; skipping test"
<< std::endl;
exit( 0 );
}

return devices_list;
}

// Remove the frame's stream (or streams if a frameset) from the list of streams we expect to arrive
// If any stream is unexpected, it is ignored
inline void remove_all_streams_arrived( rs2::frame f,
maloel marked this conversation as resolved.
Show resolved Hide resolved
std::vector< rs2::stream_profile > & expected_streams )
{
auto remove_stream = [&]() {
auto it = std::remove_if( expected_streams.begin(),
expected_streams.end(),
[&]( rs2::stream_profile s ) {
return s.stream_type() == f.get_profile().stream_type();
} );


if( it != expected_streams.end() )
expected_streams.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 stream_profile find_default_depth_profile( 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;
} );

REQUIRE( depth_profile != stream_profiles.end() );
return *depth_profile;
}

inline stream_profile find_default_ir_profile( rs2::depth_sensor depth_sens )
{
std::vector< stream_profile > stream_profiles;
REQUIRE_NOTHROW( stream_profiles = depth_sens.get_stream_profiles() );

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;
} );

REQUIRE( ir_profile != stream_profiles.end() );
return *ir_profile;
maloel marked this conversation as resolved.
Show resolved Hide resolved
}

inline stream_profile find_confidence_corresponding_to_depth( rs2::depth_sensor depth_sens,
stream_profile depth_profile )
{
std::vector< stream_profile > stream_profiles;
REQUIRE_NOTHROW( stream_profiles = depth_sens.get_stream_profiles() );

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()
== depth_profile.as< rs2::video_stream_profile >().width()
&& sp.as< rs2::video_stream_profile >().height()
== depth_profile.as< rs2::video_stream_profile >().height();
} );

REQUIRE( confidence_profile != stream_profiles.end() );
return *confidence_profile;
}
21 changes: 21 additions & 0 deletions unit-tests/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.

#pragma once

#include "librealsense2/rs.hpp"

#if ! defined( NO_CATCH_CONFIG_MAIN )
#define CATCH_CONFIG_MAIN
#endif

#include "catch.h"

#include <easylogging++.h>
#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