HdHomerunHostTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using AutoFixture;
  7. using AutoFixture.AutoMoq;
  8. using Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun;
  9. using MediaBrowser.Model.LiveTv;
  10. using Moq;
  11. using Moq.Protected;
  12. using Xunit;
  13. namespace Jellyfin.Server.Implementations.Tests.LiveTv
  14. {
  15. public class HdHomerunHostTests
  16. {
  17. private const string TestIp = "http://192.168.1.182";
  18. private readonly Fixture _fixture;
  19. private readonly HdHomerunHost _hdHomerunHost;
  20. public HdHomerunHostTests()
  21. {
  22. const string BaseResourcePath = "Jellyfin.Server.Implementations.Tests.LiveTv.";
  23. var messageHandler = new Mock<HttpMessageHandler>();
  24. messageHandler.Protected()
  25. .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
  26. .Returns<HttpRequestMessage, CancellationToken>(
  27. (m, _) =>
  28. {
  29. var resource = BaseResourcePath + m.RequestUri?.Segments[^1];
  30. var stream = typeof(HdHomerunHostTests).Assembly.GetManifestResourceStream(resource);
  31. if (stream == null)
  32. {
  33. throw new NullReferenceException("Resource doesn't exist: " + resource);
  34. }
  35. return Task.FromResult(new HttpResponseMessage()
  36. {
  37. Content = new StreamContent(stream)
  38. });
  39. });
  40. var http = new Mock<IHttpClientFactory>();
  41. http.Setup(x => x.CreateClient(It.IsAny<string>()))
  42. .Returns(new HttpClient(messageHandler.Object));
  43. _fixture = new Fixture();
  44. _fixture.Customize(new AutoMoqCustomization
  45. {
  46. ConfigureMembers = true
  47. }).Inject(http);
  48. _hdHomerunHost = _fixture.Create<HdHomerunHost>();
  49. }
  50. [Fact]
  51. public async Task GetModelInfo_Valid_Success()
  52. {
  53. var host = new TunerHostInfo()
  54. {
  55. Url = TestIp
  56. };
  57. var modelInfo = await _hdHomerunHost.GetModelInfo(host, true, CancellationToken.None).ConfigureAwait(false);
  58. Assert.Equal("HDHomeRun PRIME", modelInfo.FriendlyName);
  59. Assert.Equal("HDHR3-CC", modelInfo.ModelNumber);
  60. Assert.Equal("hdhomerun3_cablecard", modelInfo.FirmwareName);
  61. Assert.Equal("20160630atest2", modelInfo.FirmwareVersion);
  62. Assert.Equal("FFFFFFFF", modelInfo.DeviceID);
  63. Assert.Equal("FFFFFFFF", modelInfo.DeviceAuth);
  64. Assert.Equal(3, modelInfo.TunerCount);
  65. Assert.Equal("http://192.168.1.182:80", modelInfo.BaseURL);
  66. Assert.Equal("http://192.168.1.182:80/lineup.json", modelInfo.LineupURL);
  67. }
  68. [Fact]
  69. public async Task GetModelInfo_EmptyUrl_ArgumentException()
  70. {
  71. var host = new TunerHostInfo()
  72. {
  73. Url = string.Empty
  74. };
  75. await Assert.ThrowsAsync<ArgumentException>(() => _hdHomerunHost.GetModelInfo(host, true, CancellationToken.None));
  76. }
  77. [Fact]
  78. public async Task GetLineup_Valid_Success()
  79. {
  80. var host = new TunerHostInfo()
  81. {
  82. Url = TestIp
  83. };
  84. var channels = await _hdHomerunHost.GetLineup(host, CancellationToken.None).ConfigureAwait(false);
  85. Assert.Equal(6, channels.Count);
  86. Assert.Equal("4.1", channels[0].GuideNumber);
  87. Assert.Equal("WCMH-DT", channels[0].GuideName);
  88. Assert.True(channels[0].HD);
  89. Assert.True(channels[0].Favorite);
  90. Assert.Equal("http://192.168.1.111:5004/auto/v4.1", channels[0].URL);
  91. }
  92. [Fact]
  93. public async Task GetLineup_ImportFavoritesOnly_Success()
  94. {
  95. var host = new TunerHostInfo()
  96. {
  97. Url = TestIp,
  98. ImportFavoritesOnly = true
  99. };
  100. var channels = await _hdHomerunHost.GetLineup(host, CancellationToken.None).ConfigureAwait(false);
  101. Assert.Single(channels);
  102. Assert.Equal("4.1", channels[0].GuideNumber);
  103. Assert.Equal("WCMH-DT", channels[0].GuideName);
  104. Assert.True(channels[0].HD);
  105. Assert.True(channels[0].Favorite);
  106. Assert.Equal("http://192.168.1.111:5004/auto/v4.1", channels[0].URL);
  107. }
  108. [Fact]
  109. public async Task TryGetTunerHostInfo_Valid_Success()
  110. {
  111. var host = await _hdHomerunHost.TryGetTunerHostInfo(TestIp, CancellationToken.None).ConfigureAwait(false);
  112. Assert.Equal(_hdHomerunHost.Type, host.Type);
  113. Assert.Equal(TestIp, host.Url);
  114. Assert.Equal("HDHomeRun PRIME", host.FriendlyName);
  115. Assert.Equal("FFFFFFFF", host.DeviceId);
  116. Assert.Equal(3, host.TunerCount);
  117. }
  118. }
  119. }