History

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.

Dudemanguy committed 1 year ago

Commit 87359741 parent ef3e6be2 files · +78 −1Browse files

Unified · Split

2 files
  1. test/libmpv_test_track_selection.c +71 −0
  2. test/samples/meson.build +7 −1
test/libmpv_test_track_selection.c+71−0
@@ -16,8 +16,65 @@ · 15 unmodified lines
1616 */
1717
1818 #include <unistd.h>
19+
20+#ifdef __APPLE__
21+#include <CoreFoundation/CoreFoundation.h>
22+#endif
23+
24+#ifdef _WIN32
25+#include <windows.h>
26+#endif
27+
1928 #include "libmpv_common.h"
2029
30+static bool have_english_locale(void)
31+{
32+#ifdef __APPLE__
33+ CFArrayRef arr = CFLocaleCopyPreferredLanguages();
34+ if (!arr)
35+ return false;
36+ CFIndex count = CFArrayGetCount(arr);
37+ if (!count)
38+ return false;
39+
40+ bool ret = false;
41+ CFRange range = CFRangeMake(0, 2);
42+ CFStringRef en = CFStringCreateWithCString(NULL, "en", kCFStringEncodingMacRoman);
43+ for (CFIndex i = 0; i < count; i++) {
44+ CFStringRef cfstr = CFArrayGetValueAtIndex(arr, i);
45+ if (CFStringCompareWithOptions(cfstr, en, range, 0) == 0) {
46+ ret = true;
47+ break;
48+ }
49+ }
50+ CFRelease(en);
51+ return ret;
52+#endif
53+
54+#ifdef _WIN32
55+ ULONG count = 0;
56+ ULONG size = 0;
57+ wchar_t buf[1024];
58+
59+ if (GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &count, buf, &size)) {
60+ for (ULONG pos = 0; buf[pos]; pos += wcslen(buf + pos) + 1) {
61+ if (wcsncmp(buf + pos, L"en", 2) == 0)
62+ return true;
63+ }
64+ }
65+
66+ if (GetSystemPreferredUILanguages(MUI_LANGUAGE_NAME, &count, buf, &size)) {
67+ for (ULONG pos = 0; buf[pos]; pos += wcslen(buf + pos) + 1) {
68+ if (wcsncmp(buf + pos, L"en", 2) == 0)
69+ return true;
70+ }
71+ }
72+
73+ return false;
74+#endif
75+ return true;
76+}
77+
2178 static void check_string(const char *property, const char *expect)
2279 {
2380 char *result_string;
@@ -84,6 +141,15 @@ static void test_track_selection(char *file, char *path) · 60 unmodified lines
84141 check_api_error(mpv_set_property_string(ctx, "subs-fallback", "yes"));
85142 reload_file(path);
86143 check_string("current-tracks/sub/selected", "yes");
144+ } else if (strcmp(file, "locale.mkv") == 0) {
145+ // default english subs
146+ reload_file(path);
147+ check_string("current-tracks/sub/lang", "eng");
148+
149+ // default german subs
150+ check_api_error(mpv_set_property_string(ctx, "subs-match-os-language", "no"));
151+ reload_file(path);
152+ check_string("current-tracks/sub/lang", "ger");
87153 } else if (strcmp(file, "multilang.mkv") == 0) {
88154 // --alang=jpn should select forced jpn subs
89155 check_api_error(mpv_set_property_string(ctx, "alang", "jpn"));
@@ -186,6 +252,11 @@ int main(int argc, char *argv[]) · 96 unmodified lines
186252 if (argc < 3)
187253 return 1;
188254
255+ if (!strcmp(argv[1], "locale.mkv") && !have_english_locale()) {
256+ printf("Non English language detected. Skipping locale test.\n");
257+ return 77;
258+ }
259+
189260 ctx = mpv_create();
190261 if (!ctx)
191262 return 1;
test/samples/meson.build+7−1
@@ -32,6 +32,11 @@ samples = { · 31 unmodified lines
3232 '-disposition:s:0', 'forced', '@OUTPUT@'],
3333 'eng_no_default.mkv':
3434 [ffmpeg, common_args, '@OUTPUT@'],
35+ 'locale.mkv':
36+ [ffmpeg, '-v', 'error', '-y', '-i', video, '-i', audio, '-f', 'srt', '-i', sub, '-i', sub,
37+ '-c', 'copy', '-map', '0:0', '-map', '1:0', '-map', '2:0', '-map', '3:0', '-metadata:s:s:0',
38+ 'language=ger', '-metadata:s:s:1', 'language=eng', '-disposition:s:0', 'default',
39+ '-disposition:s:1', 'default', '@OUTPUT@'],
3540 'multilang.mkv':
3641 [ffmpeg, '-v', 'error', '-y', '-i', video, '-i', audio, '-i', audio, '-i', audio, '-i', audio,
3742 '-f', 'srt', '-i', sub, '-i', sub, '-i', sub, '-i', sub, '-c', 'copy', '-map', '0:0',
@@ -57,9 +62,10 @@ foreach name, cmd: samples · 19 unmodified lines
5762 depends: [video, audio],
5863 command: cmd,
5964 )
65+ env = name == 'locale.mkv' ? 'LANG=C' : []
6066 testname = 'libmpv-test-' + name
6167 exe = executable(testname, '../libmpv_test_track_selection.c',
6268 include_directories: incdir, link_with: libmpv)
6369 test(testname, exe, args: [name, target.full_path()],
64− depends: target, suite: 'libmpv')
70+ depends: target, env: env, suite: 'libmpv')
6571 endforeach

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t