Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 640377c

Browse files
authored
Check for Internet Connection before running tests that Require an Internet Connection (#12790)
* Check for the internet before running internet based tests * - iOS fixes * - uwp fixes * - add additional tests * - extract categories from the class * - add additional tests * Update Xamarin.Forms.ControlGallery.Android.csproj
1 parent b00cb31 commit 640377c

File tree

14 files changed

+131
-10
lines changed

14 files changed

+131
-10
lines changed

Xamarin.Forms.ControlGallery.Android/FormsAppCompatActivity.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Xamarin.Forms.Platform.Android.AppLinks;
1212
using System.Linq;
1313
using Xamarin.Forms.Internals;
14+
using System.Threading.Tasks;
15+
using System.Net.Http;
1416

1517
namespace Xamarin.Forms.ControlGallery.Android
1618
{
@@ -116,6 +118,27 @@ protected override void OnResume()
116118
Profile.Stop();
117119
}
118120

121+
[Export("hasInternetAccess")]
122+
public bool HasInternetAccess()
123+
{
124+
try
125+
{
126+
using (var httpClient = new HttpClient())
127+
using (var httpResponse = httpClient.GetAsync(@"https://www.github.com"))
128+
{
129+
httpResponse.Wait();
130+
if (httpResponse.Result.StatusCode == System.Net.HttpStatusCode.OK)
131+
return true;
132+
else
133+
return false;
134+
}
135+
}
136+
catch
137+
{
138+
return false;
139+
}
140+
}
141+
119142
[Export("IsPreAppCompat")]
120143
public bool IsPreAppCompat()
121144
{

Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,4 @@
415415
</ItemGroup>
416416
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
417417
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
418-
</Project>
418+
</Project>

Xamarin.Forms.ControlGallery.iOS/AppDelegate.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Drawing;
33
using System.Globalization;
44
using System.IO;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
57
using CoreGraphics;
68
using Foundation;
79
using UIKit;
@@ -407,6 +409,27 @@ public int iOSVersion()
407409
{
408410
return App.IOSVersion;
409411
}
412+
413+
[Export("hasInternetAccess")]
414+
public bool HasInternetAccess()
415+
{
416+
try
417+
{
418+
using (var httpClient = new HttpClient())
419+
using (var httpResponse = httpClient.GetAsync(@"https://www.github.com"))
420+
{
421+
httpResponse.Wait();
422+
if (httpResponse.Result.StatusCode == System.Net.HttpStatusCode.OK)
423+
return true;
424+
else
425+
return false;
426+
}
427+
}
428+
catch
429+
{
430+
return false;
431+
}
432+
}
410433
}
411434

412435
[Register("KVOUISlider")]

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue10744.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected override void Init()
6666
#if UITEST
6767
[Test]
6868
[NUnit.Framework.Category(Core.UITests.UITestCategories.UwpIgnore)]
69+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
6970
public void WebViewEvalCrashesOnAndroidWithLongString()
7071
{
7172
RunningApp.WaitForElement("navigatedLabel");

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue12134.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private void SetCookieContainer(WebView wv)
113113

114114
#if UITEST
115115
[Test]
116+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
116117
public void CookiesCorrectlyLoadWithMultipleWebViews()
117118
{
118119
for (int i = 0; i < 10; i++)

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2354.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
#if UITEST
99
using NUnit.Framework;
1010
using Xamarin.UITest;
11+
using Xamarin.Forms.Core.UITests;
1112
#endif
1213

1314
namespace Xamarin.Forms.Controls.Issues
1415
{
1516
#if UITEST
1617
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github5000)]
18+
[NUnit.Framework.Category(Core.UITests.UITestCategories.Image)]
19+
[NUnit.Framework.Category(Core.UITests.UITestCategories.ListView)]
20+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
1721
#endif
1822
[Preserve(AllMembers = true)]
1923
[Issue(IssueTracker.Github, 2354, "ListView, ImageCell and disabled source cache and same image url", PlatformAffected.iOS | PlatformAffected.Android)]
@@ -22,10 +26,17 @@ public class Issue2354 : TestContentPage
2226
protected override void Init()
2327
{
2428
var presidents = new List<President>();
25-
for (int i = 0; i < 10; i++)
26-
{
27-
presidents.Add(new President($"Presidente {44 - i}", 1, $"http://static.c-span.org/assets/images/series/americanPresidents/{43 - i}_400.png"));
28-
}
29+
30+
presidents.Add(new President($"Presidente 44", 1, $"https://github.com/xamarin/Xamarin.Forms/blob/17881ec93d6b3fb0ee5e1a2be46d7eeadef23529/Xamarin.Forms.ControlGallery.Android/Resources/drawable/Fruits.jpg?raw=true"));
31+
presidents.Add(new President($"Presidente 43", 2, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/person.png?raw=true"));
32+
presidents.Add(new President($"Presidente 42", 3, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/photo.jpg?raw=true"));
33+
presidents.Add(new President($"Presidente 41", 4, $"https://github.com/xamarin/Xamarin.Forms/blob/17881ec93d6b3fb0ee5e1a2be46d7eeadef23529/Xamarin.Forms.ControlGallery.Android/Resources/drawable/FlowerBuds.jpg?raw=true"));
34+
presidents.Add(new President($"Presidente 40", 5, $"https://github.com/xamarin/Xamarin.Forms/blob/17881ec93d6b3fb0ee5e1a2be46d7eeadef23529/Xamarin.Forms.ControlGallery.Android/Resources/drawable/games.png?raw=true"));
35+
presidents.Add(new President($"Presidente 39", 6, $"https://github.com/xamarin/Xamarin.Forms/blob/17881ec93d6b3fb0ee5e1a2be46d7eeadef23529/Xamarin.Forms.ControlGallery.Android/Resources/drawable/gear.png?raw=true"));
36+
presidents.Add(new President($"Presidente 38", 7, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/xamarinlogo.png?raw=true"));
37+
presidents.Add(new President($"Presidente 37", 8, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/xamarinstore.jpg?raw=true"));
38+
presidents.Add(new President($"Presidente 36", 9, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/oasis.jpg?raw=true"));
39+
presidents.Add(new President($"Presidente 35", 10, $"https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.ControlGallery.Android/Resources/drawable/Vegetables.jpg?raw=true"));
2940

3041
var header = new Label
3142
{

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue4597.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public void ImageFromFileSourceAppearsAndDisappearsCorrectly()
180180
}
181181

182182
[Test]
183+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
183184
public void ImageFromUriSourceAppearsAndDisappearsCorrectly()
184185
{
185186
RunTest(nameof(Image), true);
@@ -193,6 +194,7 @@ public void ButtonFromFileSourceAppearsAndDisappearsCorrectly()
193194
}
194195

195196
[Test]
197+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
196198
public void ButtonFromUriSourceAppearsAndDisappearsCorrectly()
197199
{
198200
RunTest(nameof(Button), true);
@@ -206,6 +208,7 @@ public void ImageButtonFromFileSourceAppearsAndDisappearsCorrectly()
206208
}
207209

208210
[Test]
211+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
209212
public void ImageButtonFromUriSourceAppearsAndDisappearsCorrectly()
210213
{
211214
RunTest(nameof(ImageButton), true);
@@ -218,6 +221,7 @@ public void ImageCellFromFileSourceAppearsAndDisappearsCorrectly()
218221
}
219222

220223
[Test]
224+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
221225
public void ImageCellFromUriSourceAppearsAndDisappearsCorrectly()
222226
{
223227
ImageCellTest(false);
@@ -235,6 +239,7 @@ void ImageCellTest(bool fileSource)
235239
SetImageSourceToNull();
236240

237241
imageVisible = GetImage();
242+
Assert.AreEqual(0, imageVisible.Length);
238243

239244
UITest.Queries.AppResult[] GetImage()
240245
{
@@ -301,11 +306,9 @@ void SetupTest(string controlType, bool fileSource)
301306
RunningApp.WaitForNoElement(activeTest);
302307
}
303308

304-
var currentSetting = RunningApp.WaitForElement(_switchUriId)[0].ReadText();
305-
306-
if (fileSource && RunningApp.Query(_imageFromUri).Length == 0)
309+
if (fileSource && RunningApp.Query(_imageFromFile).Length == 0)
307310
RunningApp.Tap(_switchUriId);
308-
else if (!fileSource && RunningApp.Query(_imageFromFile).Length == 0)
311+
else if (!fileSource && RunningApp.Query(_imageFromUri).Length == 0)
309312
RunningApp.Tap(_switchUriId);
310313
}
311314
#endif

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue9682.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected override void Init()
4040

4141
#if UITEST
4242
[Test, NUnit.Framework.Category(UITestCategories.Image)]
43+
[NUnit.Framework.Category(UITestCategories.RequiresInternetConnection)]
4344
public void MonkiesShouldLoad()
4445
{
4546
RunningApp.WaitForElement("MonkeyLoadButton");

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/ScreenshotConditionalApp.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,37 @@ public void Restart()
473473
}
474474
#endif
475475

476+
string[] GetTestCategories()
477+
{
478+
var testClassName = TestContext.CurrentContext.Test.ClassName;
479+
480+
// TestContext.CurrentContext.Test.Properties["Category"]
481+
// Only gives you the categories on the test itself
482+
// There isn't a property I could find that gives you the Categories
483+
// on the Test Class
484+
return GetType()
485+
.Assembly
486+
.GetType(testClassName)
487+
.GetCustomAttributes(typeof(NUnit.Framework.CategoryAttribute), true)
488+
.OfType<NUnit.Framework.CategoryAttribute>()
489+
.Select(x => x.Name)
490+
.Union(TestContext.CurrentContext.Test.Properties["Category"].OfType<string>())
491+
.ToArray();
492+
}
493+
476494
public void TestSetup(Type testType, bool isolate)
477495
{
496+
if (GetTestCategories().Contains(UITestCategories.RequiresInternetConnection))
497+
{
498+
var hasInternetAccess = $"{_app.Invoke("hasInternetAccess")}";
499+
bool checkInternet;
500+
501+
if (bool.TryParse(hasInternetAccess, out checkInternet))
502+
{
503+
if (!checkInternet)
504+
Assert.Inconclusive("Device Has No Internet Connection");
505+
}
506+
}
478507

479508
#if __WINDOWS__
480509
RestartIfAppIsClosed();

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33
using Xamarin.Forms.CustomAttributes;
44
using IOPath = System.IO.Path;
5+
using System.Linq;
56
using NUnit.Framework.Interfaces;
67
using Xamarin.Forms.Controls.Issues;
78

0 commit comments

Comments
 (0)