|
| 1 | +namespace AngleSharp.Scripting.JavaScript.Tests |
| 2 | +{ |
| 3 | + using NUnit.Framework; |
| 4 | + using System; |
| 5 | + using System.Net.NetworkInformation; |
| 6 | + |
| 7 | + class Helper |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// Indicates whether any network connection is available |
| 11 | + /// Filter connections below a specified speed, as well as virtual network cards. |
| 12 | + /// Additionally writes an inconclusive message if no network is available. |
| 13 | + /// </summary> |
| 14 | + /// <returns>True if a network connection is available; otherwise false.</returns> |
| 15 | + public static Boolean IsNetworkAvailable() |
| 16 | + { |
| 17 | + if (IsNetworkAvailable(0)) |
| 18 | + { |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + Assert.Inconclusive("No network has been detected. Test skipped."); |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Indicates whether any network connection is available. |
| 28 | + /// Filter connections below a specified speed, as well as virtual network cards. |
| 29 | + /// </summary> |
| 30 | + /// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param> |
| 31 | + /// <returns>True if a network connection is available; otherwise false.</returns> |
| 32 | + public static Boolean IsNetworkAvailable(Int64 minimumSpeed) |
| 33 | + { |
| 34 | + if (NetworkInterface.GetIsNetworkAvailable()) |
| 35 | + { |
| 36 | + foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) |
| 37 | + { |
| 38 | + // discard because of standard reasons |
| 39 | + if ((ni.OperationalStatus != OperationalStatus.Up) || |
| 40 | + (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) || |
| 41 | + (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel)) |
| 42 | + continue; |
| 43 | + |
| 44 | + // this allow to filter modems, serial, etc. |
| 45 | + // I use 10000000 as a minimum speed for most cases |
| 46 | + if (ni.Speed < minimumSpeed) |
| 47 | + continue; |
| 48 | + |
| 49 | + // discard virtual cards (virtual box, virtual pc, etc.) |
| 50 | + if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) || |
| 51 | + (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0)) |
| 52 | + continue; |
| 53 | + |
| 54 | + // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card. |
| 55 | + if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase)) |
| 56 | + continue; |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments