-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplatform_windows_mode_test.go
More file actions
244 lines (222 loc) · 7.68 KB
/
platform_windows_mode_test.go
File metadata and controls
244 lines (222 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//go:build windows
package main
import "testing"
func TestShouldOpenIMEForSourceID(t *testing.T) {
tests := []struct {
name string
sourceID string
wantOpen bool
}{
{name: "english mode", sourceID: "en-US", wantOpen: false},
{name: "chinese mode", sourceID: "zh-CN", wantOpen: true},
{name: "japanese prefix", sourceID: "ja", wantOpen: true},
{name: "korean mode", sourceID: "ko-KR", wantOpen: true},
{name: "trim whitespace", sourceID: " zh-CN ", wantOpen: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldOpenIMEForSourceID(tt.sourceID)
if got != tt.wantOpen {
t.Fatalf("shouldOpenIMEForSourceID(%q) = %v, want %v", tt.sourceID, got, tt.wantOpen)
}
})
}
}
func TestIsRequestSupportedForLayout(t *testing.T) {
tests := []struct {
name string
currentLayout string
requestedSource string
want bool
}{
{name: "korean layout allows ko-KR", currentLayout: "ko-KR", requestedSource: "ko-KR", want: true},
{name: "korean layout allows en-US", currentLayout: "ko-KR", requestedSource: "en-US", want: true},
{name: "korean layout rejects ja-JP", currentLayout: "ko-KR", requestedSource: "ja-JP", want: false},
{name: "korean layout rejects random id", currentLayout: "ko-KR", requestedSource: "abc", want: false},
{name: "korean layout allows official klid code", currentLayout: "ko-KR", requestedSource: "00000804", want: true},
{name: "japanese layout allows official 0x code", currentLayout: "ja-JP", requestedSource: "0x412", want: true},
{name: "japanese layout rejects korean request", currentLayout: "ja-JP", requestedSource: "ko-KR", want: false},
{name: "chinese layout rejects japanese request", currentLayout: "zh-CN", requestedSource: "ja-JP", want: false},
{name: "chinese layout allows chinese request", currentLayout: "zh-CN", requestedSource: "zh-CN", want: true},
{name: "english layout allows english variant", currentLayout: "en-US", requestedSource: "en-GB", want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isRequestSupportedForLayout(tt.currentLayout, tt.requestedSource)
if got != tt.want {
t.Fatalf("isRequestSupportedForLayout(%q, %q) = %v, want %v", tt.currentLayout, tt.requestedSource, got, tt.want)
}
})
}
}
func TestIsOfficialLayoutCode(t *testing.T) {
tests := []struct {
name string
sourceID string
want bool
}{
{name: "8 digit klid", sourceID: "00000804", want: true},
{name: "0x klid", sourceID: "0x411", want: true},
{name: "locale tag", sourceID: "ja-JP", want: false},
{name: "random", sourceID: "abc", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isOfficialLayoutCode(tt.sourceID)
if got != tt.want {
t.Fatalf("isOfficialLayoutCode(%q) = %v, want %v", tt.sourceID, got, tt.want)
}
})
}
}
func TestShouldSwitchLayoutForSourceID(t *testing.T) {
tests := []struct {
name string
sourceID string
want bool
}{
{name: "klid should switch layout", sourceID: "00000804", want: true},
{name: "0x code should switch layout", sourceID: "0x411", want: true},
{name: "language tag should not switch layout", sourceID: "en-US", want: false},
{name: "cjk language tag should not switch layout", sourceID: "zh-CN", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldSwitchLayoutForSourceID(tt.sourceID)
if got != tt.want {
t.Fatalf("shouldSwitchLayoutForSourceID(%q) = %v, want %v", tt.sourceID, got, tt.want)
}
})
}
}
func TestPrimaryLanguage(t *testing.T) {
tests := []struct {
name string
sourceID string
want string
}{
{name: "region language tag", sourceID: "ko-KR", want: "ko"},
{name: "simple language", sourceID: "ja", want: "ja"},
{name: "trim and lowercase", sourceID: " ZH-cn ", want: "zh"},
{name: "empty string", sourceID: "", want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := primaryLanguage(tt.sourceID)
if got != tt.want {
t.Fatalf("primaryLanguage(%q) = %q, want %q", tt.sourceID, got, tt.want)
}
})
}
}
func TestParseRequestedLangID(t *testing.T) {
tests := []struct {
name string
sourceID string
want uint32
ok bool
}{
{name: "locale tag", sourceID: "ja-JP", want: 0x0411, ok: true},
{name: "klid hex", sourceID: "00000412", want: 0x0412, ok: true},
{name: "0x value", sourceID: "0x409", want: 0x0409, ok: true},
{name: "invalid", sourceID: "not-a-layout", want: 0, ok: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseRequestedLangID(tt.sourceID)
if ok != tt.ok {
t.Fatalf("parseRequestedLangID(%q) ok = %v, want %v", tt.sourceID, ok, tt.ok)
}
if got != tt.want {
t.Fatalf("parseRequestedLangID(%q) = %#x, want %#x", tt.sourceID, got, tt.want)
}
})
}
}
func TestNormalizeKLID(t *testing.T) {
tests := []struct {
name string
sourceID string
want string
ok bool
}{
{name: "existing klid", sourceID: "00000411", want: "00000411", ok: true},
{name: "0x format", sourceID: "0x412", want: "00000412", ok: true},
{name: "locale tag", sourceID: "ko-KR", want: "00000412", ok: true},
{name: "invalid", sourceID: "xx", want: "", ok: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := normalizeKLID(tt.sourceID)
if ok != tt.ok {
t.Fatalf("normalizeKLID(%q) ok = %v, want %v", tt.sourceID, ok, tt.ok)
}
if got != tt.want {
t.Fatalf("normalizeKLID(%q) = %q, want %q", tt.sourceID, got, tt.want)
}
})
}
}
func TestIsSourceMatchLayout(t *testing.T) {
tests := []struct {
name string
sourceID string
layout string
want bool
}{
{name: "exact match", sourceID: "ko-KR", layout: "ko-KR", want: true},
{name: "short language match", sourceID: "ko", layout: "ko-KR", want: true},
{name: "case-insensitive match", sourceID: "EN-us", layout: "en-US", want: true},
{name: "different language", sourceID: "ja-JP", layout: "ko-KR", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isSourceMatchLayout(tt.sourceID, tt.layout)
if got != tt.want {
t.Fatalf("isSourceMatchLayout(%q, %q) = %v, want %v", tt.sourceID, tt.layout, got, tt.want)
}
})
}
}
func TestShouldForceNativeMode(t *testing.T) {
tests := []struct {
name string
sourceID string
want bool
}{
{name: "korean uses native mode", sourceID: "ko-KR", want: true},
{name: "korean case-insensitive", sourceID: "KO-kr", want: true},
{name: "chinese simplified uses native mode", sourceID: "zh-CN", want: true},
{name: "chinese traditional uses native mode", sourceID: "zh-TW", want: true},
{name: "english no native mode", sourceID: "en-US", want: false},
{name: "japanese no forced native mode", sourceID: "ja-JP", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldForceNativeMode(tt.sourceID)
if got != tt.want {
t.Fatalf("shouldForceNativeMode(%q) = %v, want %v", tt.sourceID, got, tt.want)
}
})
}
}
func TestShouldDisableNativeMode(t *testing.T) {
tests := []struct {
name string
sourceID string
want bool
}{
{name: "english disables native mode", sourceID: "en-US", want: true},
{name: "english case-insensitive", sourceID: "EN-us", want: true},
{name: "korean does not disable native mode", sourceID: "ko-KR", want: false},
{name: "chinese does not disable native mode", sourceID: "zh-CN", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldDisableNativeMode(tt.sourceID)
if got != tt.want {
t.Fatalf("shouldDisableNativeMode(%q) = %v, want %v", tt.sourceID, got, tt.want)
}
})
}
}