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

#include "liblinphone_tester_windows.h"

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

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

static OutputTraceListener^ sTraceListener;

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

static void nativeOutputTraceHandler(int lev, const char *fmt, va_list args)
{
	if (sTraceListener) {
22
		wchar_t wstr[MAX_TRACE_SIZE] = { 0 };
23 24 25
		std::string str;
		str.resize(MAX_TRACE_SIZE);
		vsnprintf((char *)str.c_str(), MAX_TRACE_SIZE, fmt, args);
26
		mbstowcs(wstr, str.c_str(), MAX_TRACE_SIZE - 1);
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
		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);
	}
}

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


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

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

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

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

	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);
91 92 93
	}
}

94
bool NativeTester::run(Platform::String^ suiteName, Platform::String^ caseName, Platform::Boolean verbose)
95 96 97 98 99 100 101 102 103
{
	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));

104 105 106 107 108 109
	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);
	}
110
	linphone_core_set_log_handler(libLinphoneNativeOutputTraceHandler);
111
	return bc_tester_run_tests(wssuitename == all ? 0 : csuitename, wscasename == all ? 0 : ccasename, NULL) != 0;
112 113
}

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

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

128
unsigned int NativeTester::nbTests(Platform::String^ suiteName)
129 130 131 132 133 134 135
{
	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);
}

136
Platform::String^ NativeTester::testSuiteName(int index)
137 138 139 140 141 142 143
{
	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);
}

144
Platform::String^ NativeTester::testName(Platform::String^ suiteName, int testIndex)
145 146 147 148 149 150 151 152 153
{
	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);
	wchar_t wcname[MAX_SUITE_NAME_SIZE];
	mbstowcs(wcname, cname, sizeof(wcname));
	return ref new String(wcname);
}