liblinphone_tester_windows.cpp 4.37 KB
Newer Older
1 2 3
#include <string>

#include "liblinphone_tester_windows.h"
Benjamin REIS's avatar
Benjamin REIS committed
4
#include "tester_utils.h"
5

6
using namespace BelledonneCommunications::Linphone::Tester;
7 8 9 10 11
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::System::Threading;

12
#define MAX_TRACE_SIZE		2048
13 14 15 16 17
#define MAX_SUITE_NAME_SIZE	128
#define MAX_WRITABLE_DIR_SIZE 1024

static OutputTraceListener^ sTraceListener;

18
NativeTester^ NativeTester::_instance = ref new NativeTester();
19 20 21 22

static void nativeOutputTraceHandler(int lev, const char *fmt, va_list args)
{
	if (sTraceListener) {
23
		wchar_t wstr[MAX_TRACE_SIZE] = { 0 };
24 25 26
		std::string str;
		str.resize(MAX_TRACE_SIZE);
		vsnprintf((char *)str.c_str(), MAX_TRACE_SIZE, fmt, args);
27
		mbstowcs(wstr, str.c_str(), MAX_TRACE_SIZE - 1);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		String^ msg = ref new String(wstr);
		String^ l;
		switch (lev) {
		case ORTP_FATAL:
		case ORTP_ERROR:
			l = ref new String(L"Error");
			break;
		case ORTP_WARNING:
			l = ref new String(L"Warning");
			break;
		case ORTP_MESSAGE:
			l = ref new String(L"Message");
			break;
		default:
			l = ref new String(L"Debug");
			break;
		}
		sTraceListener->outputTrace(l, msg);
	}
}

49
static void libLinphoneNativeOutputTraceHandler(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args)
50 51 52 53 54
{
	nativeOutputTraceHandler((int)lev, fmt, args);
}


55
NativeTester::NativeTester()
56 57 58
{
}

59
NativeTester::~NativeTester()
60 61 62 63
{
	liblinphone_tester_uninit();
}

64
void NativeTester::setOutputTraceListener(OutputTraceListener^ traceListener)
65 66 67 68
{
	sTraceListener = traceListener;
}

69
void NativeTester::initialize(StorageFolder^ writableDirectory, Platform::Boolean ui)
70
{
71 72 73 74
	if (ui) {
		liblinphone_tester_init(nativeOutputTraceHandler);
	} else {
		liblinphone_tester_init(NULL);
75 76
		linphone_core_set_log_level_mask((OrtpLogLevel)(ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL));
	}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	char writable_dir[MAX_WRITABLE_DIR_SIZE] = { 0 };
	const wchar_t *wwritable_dir = writableDirectory->Path->Data();
	wcstombs(writable_dir, wwritable_dir, sizeof(writable_dir));
	bc_tester_set_writable_dir_prefix(writable_dir);
	bc_tester_set_resource_dir_prefix("Assets");

	if (!ui) {
		char *xmlFile = bc_tester_file("LibLinphoneWindows10.xml");
		char *args[] = { "--xml-file", xmlFile };
		bc_tester_parse_args(2, args, 0);

		char *logFile = bc_tester_file("LibLinphoneWindows10.log");
		liblinphone_tester_set_log_file(logFile);
		free(logFile);
92 93 94
	}
}

95
bool NativeTester::run(Platform::String^ suiteName, Platform::String^ caseName, Platform::Boolean verbose)
96 97 98 99 100 101 102 103 104
{
	std::wstring all(L"ALL");
	std::wstring wssuitename = suiteName->Data();
	std::wstring wscasename = caseName->Data();
	char csuitename[MAX_SUITE_NAME_SIZE] = { 0 };
	char ccasename[MAX_SUITE_NAME_SIZE] = { 0 };
	wcstombs(csuitename, wssuitename.c_str(), sizeof(csuitename));
	wcstombs(ccasename, wscasename.c_str(), sizeof(ccasename));

105 106 107 108 109 110
	if (verbose) {
		linphone_core_set_log_level_mask((OrtpLogLevel)(ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL));
	}
	else {
		linphone_core_set_log_level_mask(ORTP_FATAL);
	}
111
	linphone_core_set_log_handler(libLinphoneNativeOutputTraceHandler);
112
	return bc_tester_run_tests(wssuitename == all ? 0 : csuitename, wscasename == all ? 0 : ccasename, NULL) != 0;
113 114
}

115
void NativeTester::runAllToXml()
116 117
{
	auto workItem = ref new WorkItemHandler([this](IAsyncAction ^workItem) {
118
		bc_tester_start(NULL);
119 120 121 122 123
		bc_tester_uninit();
	});
	_asyncAction = ThreadPool::RunAsync(workItem);
}

124
unsigned int NativeTester::nbTestSuites()
125 126 127 128
{
	return bc_tester_nb_suites();
}

129
unsigned int NativeTester::nbTests(Platform::String^ suiteName)
130 131 132 133 134 135 136
{
	std::wstring suitename = suiteName->Data();
	char cname[MAX_SUITE_NAME_SIZE] = { 0 };
	wcstombs(cname, suitename.c_str(), sizeof(cname));
	return bc_tester_nb_tests(cname);
}

137
Platform::String^ NativeTester::testSuiteName(int index)
138 139 140 141 142 143 144
{
	const char *cname = bc_tester_suite_name(index);
	wchar_t wcname[MAX_SUITE_NAME_SIZE];
	mbstowcs(wcname, cname, sizeof(wcname));
	return ref new String(wcname);
}

145
Platform::String^ NativeTester::testName(Platform::String^ suiteName, int testIndex)
146 147 148 149 150
{
	std::wstring suitename = suiteName->Data();
	char csuitename[MAX_SUITE_NAME_SIZE] = { 0 };
	wcstombs(csuitename, suitename.c_str(), sizeof(csuitename));
	const char *cname = bc_tester_test_name(csuitename, testIndex);
151
	if (cname == NULL) return ref new String();
152 153 154 155
	wchar_t wcname[MAX_SUITE_NAME_SIZE];
	mbstowcs(wcname, cname, sizeof(wcname));
	return ref new String(wcname);
}