@@ -35,12 +35,18 @@ public void BeforeDeploymentTests ()
35
35
if ( debuggable != "1" ) {
36
36
Assert . Ignore ( "TimeZone tests need to use `su root` and this device does not support that feature. Try using an emulator." ) ;
37
37
}
38
+ // Disable auto timezone
39
+ RunAdbCommand ( "shell settings put global auto_time_zone 0" ) ;
38
40
39
41
proj = new XamarinFormsAndroidApplicationProject ( ) ;
40
42
proj . SetAndroidSupportedAbis ( "armeabi-v7a" , "x86" ) ;
41
43
var mainPage = proj . Sources . First ( x => x . Include ( ) == "MainPage.xaml.cs" ) ;
42
44
var source = mainPage . TextContent ( ) . Replace ( "InitializeComponent ();" , @"InitializeComponent ();
45
+ Console.WriteLine ($""TimeZoneInfoNative={Java.Util.TimeZone.Default.ID}"");
43
46
Console.WriteLine ($""TimeZoneInfo={TimeZoneInfo.Local.DisplayName}"");
47
+ " ) ;
48
+ source = source . Replace ( "Console.WriteLine (\" Button was Clicked!\" );" , @"Console.WriteLine (""Button was Clicked!"");
49
+ Console.WriteLine ($""TimeZoneInfoClick={TimeZoneInfo.Local.DisplayName}"");
44
50
" ) ;
45
51
mainPage . TextContent = ( ) => source ;
46
52
builder = CreateApkBuilder ( Path . Combine ( "temp" , "DeploymentTests" ) ) ;
@@ -183,7 +189,7 @@ public void CheckXamarinFormsAppDeploysAndAButtonWorks ()
183
189
} , Path . Combine ( Root , builder . ProjectDirectory , "button-logcat.log" ) ) , "Button Should have been Clicked." ) ;
184
190
}
185
191
186
- private const int NODE_COUNT = 3 ;
192
+ private const int NODE_COUNT = 4 ;
187
193
188
194
static object [ ] GetTimeZoneTestCases ( int node )
189
195
{
@@ -203,38 +209,72 @@ static object [] GetTimeZoneTestCases (int node)
203
209
return tests . Where ( p => tests . IndexOf ( p ) % NODE_COUNT == node ) . ToArray ( ) ;
204
210
}
205
211
206
- [ Test ]
212
+ [ Test , NonParallelizable ]
207
213
[ TestCaseSource ( nameof ( GetTimeZoneTestCases ) , new object [ ] { 0 } ) ]
208
214
[ Category ( "TimeZoneInfo" ) ]
215
+ [ Retry ( 2 ) ]
209
216
public void CheckTimeZoneInfoIsCorrectNode1 ( string timeZone ) => CheckTimeZoneInfoIsCorrect ( timeZone ) ;
210
217
211
- [ Test ]
218
+ [ Test , NonParallelizable ]
212
219
[ TestCaseSource ( nameof ( GetTimeZoneTestCases ) , new object [ ] { 1 } ) ]
213
220
[ Category ( "TimeZoneInfo" ) ]
221
+ [ Retry ( 2 ) ]
214
222
public void CheckTimeZoneInfoIsCorrectNode2 ( string timeZone ) => CheckTimeZoneInfoIsCorrect ( timeZone ) ;
215
223
216
- [ Test ]
224
+ [ Test , NonParallelizable ]
217
225
[ TestCaseSource ( nameof ( GetTimeZoneTestCases ) , new object [ ] { 2 } ) ]
218
226
[ Category ( "TimeZoneInfo" ) ]
227
+ [ Retry ( 2 ) ]
219
228
public void CheckTimeZoneInfoIsCorrectNode3 ( string timeZone ) => CheckTimeZoneInfoIsCorrect ( timeZone ) ;
220
229
230
+ [ Test , NonParallelizable ]
231
+ [ TestCaseSource ( nameof ( GetTimeZoneTestCases ) , new object [ ] { 3 } ) ]
232
+ [ Category ( "TimeZoneInfo" ) ]
233
+ [ Retry ( 2 ) ]
234
+ public void CheckTimeZoneInfoIsCorrectNode4 ( string timeZone ) => CheckTimeZoneInfoIsCorrect ( timeZone ) ;
235
+
221
236
public void CheckTimeZoneInfoIsCorrect ( string timeZone )
222
237
{
223
238
AssertHasDevices ( ) ;
224
239
225
240
string currentTimeZone = RunAdbCommand ( "shell getprop persist.sys.timezone" ) ? . Trim ( ) ;
241
+ string deviceTz = string . Empty ;
242
+ string logFile = Path . Combine ( Root , builder . ProjectDirectory , $ "startup-logcat-{ timeZone . Replace ( "/" , "-" ) } .log") ;
226
243
try {
227
- RunAdbCommand ( $ "shell su root setprop persist.sys.timezone \" { timeZone } \" ") ;
244
+ for ( int attempt = 0 ; attempt < 5 ; attempt ++ ) {
245
+ RunAdbCommand ( $ "shell su root setprop persist.sys.timezone \" { timeZone } \" ") ;
246
+ deviceTz = RunAdbCommand ( "shell getprop persist.sys.timezone" ) ? . Trim ( ) ;
247
+ if ( deviceTz == timeZone ) {
248
+ break ;
249
+ }
250
+ }
251
+ Assert . AreEqual ( timeZone , deviceTz , $ "The command to set the device timezone to { timeZone } failed. Current device timezone is { deviceTz } ") ;
252
+ ClearAdbLogcat ( ) ;
253
+ RunAdbCommand ( $ "shell am force-stop --user all { proj . PackageName } ") ;
254
+ RunAdbCommand ( $ "shell am kill --user all { proj . PackageName } ") ;
255
+ WaitFor ( ( int ) TimeSpan . FromSeconds ( 2 ) . TotalMilliseconds ) ;
228
256
ClearAdbLogcat ( ) ;
229
257
AdbStartActivity ( $ "{ proj . PackageName } /{ proj . JavaPackageName } .MainActivity") ;
230
- Assert . IsTrue ( WaitForActivityToStart ( proj . PackageName , "MainActivity" ,
231
- Path . Combine ( Root , builder . ProjectDirectory , $ "startup-logcat-{ timeZone . Replace ( "/" , "-" ) } .log") ) , "Activity should have started" ) ;
258
+ Assert . IsTrue ( WaitForActivityToStart ( proj . PackageName , "MainActivity" , logFile ) , "Activity should have started" ) ;
259
+ string line = "" ;
260
+ string logCatFile = Path . Combine ( Root , builder . ProjectDirectory , $ "timezone-logcat-{ timeZone . Replace ( "/" , "-" ) } .log") ;
261
+ ClickButton ( proj . PackageName , "myXFButton" , "CLICK ME" ) ;
232
262
Assert . IsTrue ( MonitorAdbLogcat ( ( l ) => {
233
- return l . Contains ( $ "TimeZoneInfo={ timeZone } ") ;
234
- } , Path . Combine ( Root , builder . ProjectDirectory , $ "timezone-logcat-{ timeZone . Replace ( "/" , "-" ) } .log") ) , $ "TimeZone should have been { timeZone } ") ;
263
+ if ( l . Contains ( "TimeZoneInfoClick=" ) ) {
264
+ line = l ;
265
+ return l . Contains ( $ "{ timeZone } ") ;
266
+ }
267
+ return false ;
268
+ } , logCatFile , timeout : 30 ) , $ "TimeZone should have been { timeZone } . We found : { line } ") ;
235
269
} finally {
236
- if ( ! string . IsNullOrEmpty ( currentTimeZone ) )
270
+ RunAdbCommand ( $ "shell am force-stop --user all { proj . PackageName } ") ;
271
+ RunAdbCommand ( $ "shell am kill --user all { proj . PackageName } ") ;
272
+ if ( ! string . IsNullOrEmpty ( currentTimeZone ) ) {
237
273
RunAdbCommand ( $ "shell su root setprop persist.sys.timezone \" { currentTimeZone } \" ") ;
274
+ }
275
+ if ( File . Exists ( logFile ) ) {
276
+ TestContext . AddTestAttachment ( logFile ) ;
277
+ }
238
278
}
239
279
}
240
280
}
0 commit comments