ProfileTester.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using Emby.Dlna.PlayTo;
  8. using MediaBrowser.Model.Dlna;
  9. using Xunit;
  10. namespace Jellyfin.Dlna.Tests
  11. {
  12. public class ProfileTester
  13. {
  14. private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
  15. {
  16. if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
  17. {
  18. if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
  19. {
  20. return false;
  21. }
  22. }
  23. if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
  24. {
  25. if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
  26. {
  27. return false;
  28. }
  29. }
  30. if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
  31. {
  32. if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
  33. {
  34. return false;
  35. }
  36. }
  37. if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
  38. {
  39. if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
  40. {
  41. return false;
  42. }
  43. }
  44. if (!string.IsNullOrEmpty(profileInfo.ModelName))
  45. {
  46. if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
  47. {
  48. return false;
  49. }
  50. }
  51. if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
  52. {
  53. if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
  54. {
  55. return false;
  56. }
  57. }
  58. if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
  59. {
  60. if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
  61. {
  62. return false;
  63. }
  64. }
  65. if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
  66. {
  67. if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
  68. {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. private bool IsRegexOrSubstringMatch(string input, string pattern)
  75. {
  76. return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
  77. }
  78. [Fact]
  79. public void Test_Profile_Matches()
  80. {
  81. var source = new DeviceInfo()
  82. {
  83. Name = "HelloWorld"
  84. };
  85. var dest = new DeviceProfile()
  86. {
  87. Name = "Test Subject 1",
  88. FriendlyName = "HelloWorld",
  89. Manufacturer = "LG Electronics",
  90. ManufacturerUrl = "http://www.lge.com",
  91. ModelDescription = "LG WebOSTV DMRplus",
  92. ModelName = "LG TV",
  93. ModelNumber = "1.0",
  94. Identification = new DeviceIdentification()
  95. {
  96. FriendlyName = "HelloWorld",
  97. Manufacturer = "LG Electronics",
  98. ManufacturerUrl = "http://www.lge.com",
  99. ModelDescription = "LG WebOSTV DMRplus",
  100. ModelName = "LG TV",
  101. ModelNumber = "1.0",
  102. }
  103. };
  104. Assert.True(IsMatch(dest.Identification, source.ToDeviceIdentification()));
  105. }
  106. }
  107. }