3
3
using System . Globalization ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
+ using System . Threading ;
6
7
using Mono . Unix . Native ;
7
8
using NUnit . Framework ;
8
9
using NUnit . Framework . Interfaces ;
@@ -16,8 +17,8 @@ namespace Xamarin.Android.Build.Tests
16
17
[ NonParallelizable ]
17
18
public class LocalizationTests : DeviceTest
18
19
{
19
- static ProjectBuilder builder ;
20
- static XamarinAndroidApplicationProject proj ;
20
+ ProjectBuilder builder ;
21
+ XamarinAndroidApplicationProject proj ;
21
22
string localeFileSuffix ;
22
23
23
24
[ OneTimeSetUp ]
@@ -60,19 +61,6 @@ public override void SetupTest ()
60
61
if ( ! string . IsNullOrEmpty ( localeParam ) ) {
61
62
localeFileSuffix = localeParam . Replace ( "/" , "-" ) ;
62
63
}
63
-
64
- if ( ! IsDeviceAttached ( refreshCachedValue : true ) ) {
65
- RestartDevice ( ) ;
66
- AssertHasDevices ( ) ;
67
- }
68
-
69
- // Attempt to reinstall the app that was installed during fixture setup if it is missing
70
- var packageOutput = RunAdbCommand ( $ "shell pm list packages { proj . PackageName } ") . Trim ( ) ;
71
- var expectedPackageOutput = $ "package:{ proj . PackageName } ";
72
- if ( packageOutput != expectedPackageOutput ) {
73
- builder . BuildLogFile = $ "setup-install-{ localeFileSuffix } .log";
74
- Assert . IsTrue ( builder . Install ( proj ) , "Install should have succeeded." ) ;
75
- }
76
64
}
77
65
78
66
/// <summary>
@@ -93,27 +81,22 @@ protected override void CleanupTest ()
93
81
}
94
82
}
95
83
84
+ protected override void DeviceTearDown ( )
85
+ {
86
+ }
87
+
96
88
[ OneTimeTearDown ]
97
- public void AfterAllTests ( )
89
+ protected override void AfterAllTests ( )
98
90
{
99
- string output = Path . Combine ( Root , builder ? . ProjectDirectory ) ;
100
- if ( TestContext . CurrentContext . Result . FailCount == 0 && Directory . Exists ( output ) ) {
101
- try {
102
- Directory . Delete ( output , recursive : true ) ;
103
- } catch ( IOException ex ) {
104
- // This happens on CI occasionally, let's not fail the test
105
- TestContext . Out . WriteLine ( $ "Failed to delete '{ output } ': { ex } ") ;
106
- }
107
- }
108
91
}
109
92
110
93
111
94
const int LOCALIZATION_NODE_COUNT = 15 ;
112
95
const int LOCALIZATION_RETRY_COUNT = 3 ;
113
96
114
- static object [ ] GetLocalizationTestCases ( int node )
97
+ static List < string > GetLocalizationTestInfo ( )
115
98
{
116
- List < object > tests = new List < object > ( ) ;
99
+ var tests = new List < string > ( ) ;
117
100
var ignore = new string [ ] {
118
101
"he-IL" , // maps to wi-IL on Android.
119
102
"id-ID" , // maps to in-ID on Android
@@ -127,11 +110,14 @@ static object [] GetLocalizationTestCases (int node)
127
110
TestContext . WriteLine ( $ "Ignoring { ci . Name } Localization Test") ;
128
111
continue ;
129
112
}
130
- tests . Add ( new object [ ] {
131
- ci . Name ,
132
- } ) ;
113
+ tests . Add ( ci . Name ) ;
133
114
}
115
+ return tests ;
116
+ }
134
117
118
+ static object [ ] GetLocalizationTestCases ( int node )
119
+ {
120
+ var tests = GetLocalizationTestInfo ( ) ;
135
121
return tests . Where ( p => tests . IndexOf ( p ) % LOCALIZATION_NODE_COUNT == node ) . ToArray ( ) ;
136
122
}
137
123
@@ -210,41 +196,56 @@ static object [] GetLocalizationTestCases (int node)
210
196
[ TestCaseSource ( nameof ( GetLocalizationTestCases ) , new object [ ] { 14 } ) ]
211
197
public void CheckLocalizationIsCorrectNode15 ( string locale ) => CheckLocalizationIsCorrect ( locale ) ;
212
198
199
+ [ Test ]
200
+ [ Retry ( LOCALIZATION_RETRY_COUNT ) ]
201
+ [ TestCaseSource ( nameof ( GetLocalizationTestInfo ) ) ]
202
+ public void CheckLocalizationIsCorrectWithSlicer ( string locale ) => CheckLocalizationIsCorrect ( locale ) ;
203
+
213
204
214
205
public void CheckLocalizationIsCorrect ( string locale )
215
206
{
207
+ AssertHasDevices ( ) ;
208
+
209
+ // Attempt to reinstall the app that was installed during fixture setup if it is missing
210
+ var packageOutput = RunAdbCommand ( $ "shell pm list packages { proj . PackageName } ") . Trim ( ) ;
211
+ var expectedPackageOutput = $ "package:{ proj . PackageName } ";
212
+ if ( packageOutput != expectedPackageOutput ) {
213
+ builder . BuildLogFile = $ "setup-install-{ localeFileSuffix } .log";
214
+ Assert . IsTrue ( builder . Install ( proj ) , "Install should have succeeded." ) ;
215
+ }
216
+
217
+ RunAdbCommand ( $ "shell am force-stop --user all { proj . PackageName } ") ;
218
+ RunAdbCommand ( $ "shell am kill --user all { proj . PackageName } ") ;
219
+
216
220
var appStartupLogcatFile = Path . Combine ( Root , builder . ProjectDirectory , $ "startup-logcat-{ locale . Replace ( "/" , "-" ) } .log") ;
217
221
string deviceLocale = RunAdbCommand ( "shell getprop persist.sys.locale" ) ? . Trim ( ) ;
218
222
TestContext . Out . WriteLine ( $ "test value:{ locale } , prop value:{ deviceLocale } ") ;
219
223
220
- if ( deviceLocale != locale ) {
221
- for ( int attempt = 0 ; attempt < 5 ; attempt ++ ) {
222
- TestContext . Out . WriteLine ( $ "{ nameof ( CheckLocalizationIsCorrect ) } : Setting Locale to { locale } , attempt { attempt } ...") ;
223
- ClearAdbLogcat ( ) ;
224
- var rebootLogcatFile = Path . Combine ( Root , builder . ProjectDirectory , $ "reboot{ attempt } -logcat-{ locale . Replace ( "/" , "-" ) } .log") ;
225
-
226
- // https://developer.android.com/guide/topics/resources/localization#changing-the-emulator-locale-from-the-adb-shell
227
- RunAdbCommand ( $ "shell \" su root setprop persist.sys.locale { locale } ;su root stop;sleep 5;su root start;\" ") ;
228
-
229
- if ( ! MonitorAdbLogcat ( ( l ) => {
230
- if ( l . Contains ( "ActivityManager: Finished processing BOOT_COMPLETED" ) )
231
- return true ;
232
- return false ;
233
- } , rebootLogcatFile , timeout : 60 ) ) {
234
- TestContext . Out . WriteLine ( $ "{ nameof ( CheckLocalizationIsCorrect ) } : wating for boot to complete failed or timed out.") ;
235
- }
236
- deviceLocale = RunAdbCommand ( "shell getprop persist.sys.locale" ) ? . Trim ( ) ;
237
- if ( deviceLocale == locale ) {
238
- break ;
239
- }
224
+ for ( int attempt = 0 ; attempt < 5 ; attempt ++ ) {
225
+ TestContext . Out . WriteLine ( $ "{ nameof ( CheckLocalizationIsCorrect ) } : Setting Locale to { locale } , attempt { attempt } ...") ;
226
+ ClearAdbLogcat ( ) ;
227
+ var rebootLogcatFile = Path . Combine ( Root , builder . ProjectDirectory , $ "reboot{ attempt } -logcat-{ locale . Replace ( "/" , "-" ) } .log") ;
228
+
229
+ // https://developer.android.com/guide/topics/resources/localization#changing-the-emulator-locale-from-the-adb-shell
230
+ RunAdbCommand ( $ "shell \" su root setprop persist.sys.locale { locale } ;su root stop;sleep 5;su root start;\" ") ;
231
+
232
+ if ( ! MonitorAdbLogcat ( ( l ) => {
233
+ if ( l . Contains ( "ActivityManager: Finished processing BOOT_COMPLETED" ) )
234
+ return true ;
235
+ return false ;
236
+ } , rebootLogcatFile , timeout : 60 ) ) {
237
+ TestContext . Out . WriteLine ( $ "{ nameof ( CheckLocalizationIsCorrect ) } : wating for boot to complete failed or timed out.") ;
238
+ }
239
+ deviceLocale = RunAdbCommand ( "shell getprop persist.sys.locale" ) ? . Trim ( ) ;
240
+ if ( deviceLocale == locale ) {
241
+ break ;
240
242
}
241
243
}
242
244
243
245
Assert . AreEqual ( locale , deviceLocale , $ "The command to set the device locale to { locale } failed. Current device locale is { deviceLocale } ") ;
244
246
ClearAdbLogcat ( ) ;
245
- RunAdbCommand ( $ "shell am force-stop --user all { proj . PackageName } ") ;
246
- RunAdbCommand ( $ "shell am kill --user all { proj . PackageName } ") ;
247
- RunProjectAndAssert ( proj , builder , logName : $ "run-{ locale . Replace ( "/" , "-" ) } .log") ;
247
+ Thread . Sleep ( 1000 ) ;
248
+ StartActivityAndAssert ( proj ) ;
248
249
249
250
string logcatSearchString = "Strings.SomeString=" ;
250
251
string expectedLogcatOutput = $ "{ logcatSearchString } { locale } ";
@@ -258,7 +259,6 @@ public void CheckLocalizationIsCorrect (string locale)
258
259
return false ;
259
260
} , appStartupLogcatFile , 45 ) , $ "App output did not contain '{ logcatSearchString } '") ;
260
261
Assert . IsTrue ( logLine . Contains ( expectedLogcatOutput ) , $ "Line '{ logLine } ' did not contain '{ expectedLogcatOutput } '") ;
261
-
262
262
string humanizerLogCatFile = Path . Combine ( Root , builder . ProjectDirectory , $ "humanizer-logcat-{ locale . Replace ( "/" , "-" ) } .log") ;
263
263
var culture = new CultureInfo ( locale ) ;
264
264
expectedLogcatOutput = DateTime . UtcNow . AddHours ( - 30 ) . Humanize ( culture : culture ) ;
@@ -271,7 +271,6 @@ public void CheckLocalizationIsCorrect (string locale)
271
271
return false ;
272
272
} , humanizerLogCatFile , timeout : 45 ) , $ "App output did not contain '{ logcatSearchString } '") ;
273
273
Assert . IsTrue ( logLine . Contains ( expectedLogcatOutput ) , $ "Line '{ logLine } ' did not contain '{ expectedLogcatOutput } '") ;
274
-
275
274
}
276
275
}
277
276
}
0 commit comments