test/libmpv_test_track_selection: add a test for os languages

This test requires that the OS language settings be English so the
--subs-match-os-language option is actually testable. In Linux, this can
be done by just setting LANG and it always will work. For macOS and
Windows, we use the OS APIs to get the languages and let the test
proceed if we find English. Gracefully fail and skip otherwise.
This commit is contained in:
Dudemanguy committed 2025-02-16 00:47:13 +00:00
1 parent ef3e6bebe4
commit 8735974d12
2 files changed
+78 -1

No files matched your search

+71
View File
@@ -16,8 +16,65 @@
*/
#include <unistd.h>
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include "libmpv_common.h"
static bool have_english_locale(void)
{
#ifdef __APPLE__
CFArrayRef arr = CFLocaleCopyPreferredLanguages();
if (!arr)
return false;
CFIndex count = CFArrayGetCount(arr);
if (!count)
return false;
bool ret = false;
CFRange range = CFRangeMake(0, 2);
CFStringRef en = CFStringCreateWithCString(NULL, "en", kCFStringEncodingMacRoman);
for (CFIndex i = 0; i < count; i++) {
CFStringRef cfstr = CFArrayGetValueAtIndex(arr, i);
if (CFStringCompareWithOptions(cfstr, en, range, 0) == 0) {
ret = true;
break;
}
}
CFRelease(en);
return ret;
#endif
#ifdef _WIN32
ULONG count = 0;
ULONG size = 0;
wchar_t buf[1024];
if (GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &count, buf, &size)) {
for (ULONG pos = 0; buf[pos]; pos += wcslen(buf + pos) + 1) {
if (wcsncmp(buf + pos, L"en", 2) == 0)
return true;
}
}
if (GetSystemPreferredUILanguages(MUI_LANGUAGE_NAME, &count, buf, &size)) {
for (ULONG pos = 0; buf[pos]; pos += wcslen(buf + pos) + 1) {
if (wcsncmp(buf + pos, L"en", 2) == 0)
return true;
}
}
return false;
#endif
return true;
}
static void check_string(const char *property, const char *expect)
{
char *result_string;
@@ -84,6 +141,15 @@ static void test_track_selection(char *file, char *path)
check_api_error(mpv_set_property_string(ctx, "subs-fallback", "yes"));
reload_file(path);
check_string("current-tracks/sub/selected", "yes");
} else if (strcmp(file, "locale.mkv") == 0) {
// default english subs
reload_file(path);
check_string("current-tracks/sub/lang", "eng");
// default german subs
check_api_error(mpv_set_property_string(ctx, "subs-match-os-language", "no"));
reload_file(path);
check_string("current-tracks/sub/lang", "ger");
} else if (strcmp(file, "multilang.mkv") == 0) {
// --alang=jpn should select forced jpn subs
check_api_error(mpv_set_property_string(ctx, "alang", "jpn"));
@@ -186,6 +252,11 @@ int main(int argc, char *argv[])
if (argc < 3)
return 1;
if (!strcmp(argv[1], "locale.mkv") && !have_english_locale()) {
printf("Non English language detected. Skipping locale test.\n");
return 77;
}
ctx = mpv_create();
if (!ctx)
return 1;
+7 -1
View File
@@ -32,6 +32,11 @@ samples = {
'-disposition:s:0', 'forced', '@OUTPUT@'],
'eng_no_default.mkv':
[ffmpeg, common_args, '@OUTPUT@'],
'locale.mkv':
[ffmpeg, '-v', 'error', '-y', '-i', video, '-i', audio, '-f', 'srt', '-i', sub, '-i', sub,
'-c', 'copy', '-map', '0:0', '-map', '1:0', '-map', '2:0', '-map', '3:0', '-metadata:s:s:0',
'language=ger', '-metadata:s:s:1', 'language=eng', '-disposition:s:0', 'default',
'-disposition:s:1', 'default', '@OUTPUT@'],
'multilang.mkv':
[ffmpeg, '-v', 'error', '-y', '-i', video, '-i', audio, '-i', audio, '-i', audio, '-i', audio,
'-f', 'srt', '-i', sub, '-i', sub, '-i', sub, '-i', sub, '-c', 'copy', '-map', '0:0',
@@ -57,9 +62,10 @@ foreach name, cmd: samples
depends: [video, audio],
command: cmd,
)
env = name == 'locale.mkv' ? 'LANG=C' : []
testname = 'libmpv-test-' + name
exe = executable(testname, '../libmpv_test_track_selection.c',
include_directories: incdir, link_with: libmpv)
test(testname, exe, args: [name, target.full_path()],
depends: target, suite: 'libmpv')
depends: target, env: env, suite: 'libmpv')
endforeach