Skip to content

Commit

Permalink
Changed MediaChannel queue calls to not use mediasessionid as a param…
Browse files Browse the repository at this point in the history
…eter
  • Loading branch information
Tapanila committed Jul 20, 2024
1 parent dbf5c87 commit 70b4df8
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 233 deletions.
31 changes: 15 additions & 16 deletions Sharpcaster.Test/ChromecastApplicationTester.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Sharpcaster.Models;
using Sharpcaster.Test.customChannel;
using Sharpcaster.Test.helper;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -25,7 +24,7 @@ public async Task ConnectToChromecastAndLaunchApplication(ChromecastReceiver rec
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

Assert.Equal("B3419EF5", status.Applications[0].AppId);
Assert.Equal("B3419EF5", status.Application.AppId);

await client.DisconnectAsync();
}
Expand All @@ -38,13 +37,13 @@ public async Task ConnectToChromecastAndLaunchApplicationTwice(ChromecastReceive
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

var firstLaunchTransportId = status.Applications[0].TransportId;
var firstLaunchTransportId = status.Application.TransportId;
await client.DisconnectAsync();

status = await client.ConnectChromecast(receiver);
_ = await client.ConnectChromecast(receiver);
status = await client.LaunchApplicationAsync("B3419EF5", true);

Assert.Equal(firstLaunchTransportId, status.Applications[0].TransportId);
Assert.Equal(firstLaunchTransportId, status.Application.TransportId);
}


Expand All @@ -56,15 +55,15 @@ public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining1(Ch
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

var firstLaunchTransportId = status.Applications[0].TransportId;
var firstLaunchTransportId = status.Application.TransportId;
await client.DisconnectAsync();

status = await client.ConnectChromecast(receiver);
_ = await client.ConnectChromecast(receiver);
status = await client.LaunchApplicationAsync("B3419EF5", false);


// My JBL Device (almost every time - but not always ) makes a new ID here!!!! (The other device - ChromecastAudio DOES NOT!?)
Assert.NotEqual(firstLaunchTransportId, status.Applications[0].TransportId);
Assert.NotEqual(firstLaunchTransportId, status.Application.TransportId);
}

[Theory]
Expand All @@ -74,14 +73,14 @@ public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining2(Ch
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

var firstLaunchTransportId = status.Applications[0].TransportId;
var firstLaunchTransportId = status.Application.TransportId;
await client.DisconnectAsync();

status = await client.ConnectChromecast(receiver);
_ = await client.ConnectChromecast(receiver);
status = await client.LaunchApplicationAsync("B3419EF5", false);

// My ChromecastAudio device keeps the same transport session here!
Assert.Equal(firstLaunchTransportId, status.Applications[0].TransportId);
Assert.Equal(firstLaunchTransportId, status.Application.TransportId);
}


Expand All @@ -93,13 +92,13 @@ public async Task ConnectToChromecastAndLaunchApplicationAThenLaunchApplicationB
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("A9BCCB7C"); //Youtube

var firstLaunchTransportId = status.Applications[0].TransportId;
var firstLaunchTransportId = status.Application.TransportId;
await client.DisconnectAsync();

status = await client.ConnectChromecast(receiver);
_ = await client.ConnectChromecast(receiver);
status = await client.LaunchApplicationAsync("B3419EF5"); //My sample Application

Assert.NotEqual(firstLaunchTransportId, status.Applications[0].TransportId);
Assert.NotEqual(firstLaunchTransportId, status.Application.TransportId);
}

[Theory]
Expand All @@ -110,11 +109,11 @@ public async Task ConnectToChromecastAndLaunchApplicationOnceAndJoinIt(Chromecas
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

var firstLaunchTransportId = status.Applications[0].TransportId;
var firstLaunchTransportId = status.Application.TransportId;

status = await client.LaunchApplicationAsync("B3419EF5");

Assert.Equal(firstLaunchTransportId, status.Applications[0].TransportId);
Assert.Equal(firstLaunchTransportId, status.Application.TransportId);
}

//Seems like this isn't really working anymore and just loading a white screen
Expand Down
10 changes: 1 addition & 9 deletions Sharpcaster.Test/ChromecastConnectionTester.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Sharpcaster.Models;
using Sharpcaster.Models;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Sharpcaster.Test.helper;
using Sharpcaster.Interfaces;
using Sharpcaster.Models.Media;
using System;
using System.Linq;
Expand All @@ -25,8 +23,6 @@ public ChromecastConnectionTester(ITestOutputHelper outputHelper, ChromecastDevi

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
//[MemberData(nameof(CCDevices.GetJblSpeaker), MemberType = typeof(CCDevices))]
//[MemberData(nameof(CCDevices.GetAny), MemberType = typeof(CCDevices))]
public async Task SearchChromecastsAndConnectToIt(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
Expand All @@ -35,8 +31,6 @@ public async Task SearchChromecastsAndConnectToIt(ChromecastReceiver receiver)
}

[Theory(Skip = "Test needs manuell interactions -> skipped for autotestings")]
//[Theory()]
//[MemberData(nameof(CCDevices.GetAny), MemberType = typeof(CCDevices))]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task SearchChromecastsAndConnectToItThenWaitForItToShutdown(ChromecastReceiver receiver)
{
Expand All @@ -59,8 +53,6 @@ public async Task SearchChromecastsAndConnectToItThenWaitForItToShutdown(Chromec
}

[Theory]
//[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
//[MemberData(nameof(ChromecastReceiversFilter.GetJblSpeaker), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestingHeartBeat(ChromecastReceiver receiver)
{
Expand Down
20 changes: 3 additions & 17 deletions Sharpcaster.Test/LoggingTester.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Moq;
using Sharpcaster.Channels;
using Sharpcaster.Interfaces;
using Sharpcaster.Messages;
using Sharpcaster.Models;
using Sharpcaster.Models;
using Sharpcaster.Models.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -29,12 +18,9 @@ public LoggingTester(ITestOutputHelper outputHelper) {
[Fact]
public void TestLogging() {
var TestHelper = new TestHelper();
List<string> logLines = new List<string>();
var client = TestHelper.GetClientWithTestOutput(output, assertableLog: logLines);
List<string> logLines = [];
_ = TestHelper.GetClientWithTestOutput(output, assertableLog: logLines);

//var loggerFactory = TestHelper.CreateMockedLoggerFactory(logLines);

//var client = new ChromecastClient(loggerFactory: loggerFactory);
Assert.Equal("[LAUNCH_ERROR,RECEIVER_STATUS,QUEUE_CHANGE,QUEUE_ITEM_IDS,QUEUE_ITEMS,DEVICE_UPDATED,MULTIZONE_STATUS,INVALID_REQUEST,LOAD_CANCELLED,LOAD_FAILED,MEDIA_STATUS,PING,CLOSE]", logLines[0]);
}

Expand Down
3 changes: 1 addition & 2 deletions Sharpcaster.Test/MdnsChromecastLocatorTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public async Task SearchChromecastsTrickerEvent()
public async Task SearchChromecastsWithTooShortTimeout()
{
IChromecastLocator locator = new MdnsChromecastLocator();
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(0));
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(0));
var chromecasts = await locator.FindReceiversAsync(cancellationTokenSource.Token);
Assert.Empty(chromecasts);
}
Expand Down
54 changes: 24 additions & 30 deletions Sharpcaster.Test/MediaChannelTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task TestWaitForDeviceStopDuringPlayback(ChromecastReceiver receive

mediaChannel.StatusChanged += (object sender, EventArgs e) => {
try {
MediaStatus status = mediaChannel.Status.FirstOrDefault();
MediaStatus status = mediaChannel.MediaStatus;
output.WriteLine(status?.PlayerState.ToString());
} catch (Exception) {
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public async Task TestWaitForDeviceStopDuringPlayback(ChromecastReceiver receive


[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetDefaultDevice), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver receiver) {

var TestHelper = new TestHelper();
Expand All @@ -93,11 +93,13 @@ public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver re
QueueItem[] MyCd = TestHelper.CreateTestCd();

int testSequenceCount = 0;
var mediaStatusChanged = 0;

//We are setting up an event to listen to status change. Because we don't know when the audio has started to play
mediaChannel.StatusChanged += async (object sender, EventArgs e) => {
try {
MediaStatus status = mediaChannel.Status.FirstOrDefault();
mediaStatusChanged += 1;
MediaStatus status = mediaChannel.MediaStatus;
int currentItemId = status?.CurrentItemId ?? -1;

if (currentItemId != -1 && status.PlayerState == PlayerStateType.Playing) {
Expand All @@ -108,7 +110,7 @@ public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver re
output.WriteLine("First Test Track started playin. listen for some seconds....");
await Task.Delay(6000);
output.WriteLine("Lets goto next item");
status = await mediaChannel.QueueNextAsync(status.MediaSessionId);
status = await mediaChannel.QueueNextAsync();
// Asserts
// ...
} else {
Expand All @@ -125,8 +127,8 @@ public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver re
testSequenceCount++;
await Task.Delay(6000);
output.WriteLine("Lets goto back to first one");
status = await mediaChannel.QueuePrevAsync(status.MediaSessionId);
}
status = await mediaChannel.QueuePrevAsync();
}

}
} catch (Exception ex) {
Expand All @@ -144,7 +146,8 @@ public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver re
Assert.Equal(status.CurrentItemId, status.Items[0].ItemId);

//This keeps the test running untill all eventhandler sequence steps are finished. If something goes wrong we get a very slow timeout here.
Assert.True(_autoResetEvent.WaitOne(20000));
await Task.Delay(10000);
Assert.True(_autoResetEvent.WaitOne(5000));
await client.DisconnectAsync();
}

Expand All @@ -165,16 +168,16 @@ public async Task TestLoadMediaQueueAndCheckContent(ChromecastReceiver receiver)

await Task.Delay(2000);

int[] ids = await client.MediaChannel.QueueGetItemIdsAsync(status.MediaSessionId);
int[] ids = await client.MediaChannel.QueueGetItemIdsAsync();

Assert.Equal(4, ids.Length);

foreach (int id in ids) {
QueueItem[] items = await client.MediaChannel.QueueGetItemsAsync(status.MediaSessionId, new int[] {id});
QueueItem[] items = await client.MediaChannel.QueueGetItemsAsync(new int[] {id});
Assert.Single(items);
}

QueueItem[] items2 = await client.MediaChannel.QueueGetItemsAsync(status.MediaSessionId, ids);
QueueItem[] items2 = await client.MediaChannel.QueueGetItemsAsync(ids);
Assert.Equal(4, items2.Length);
await client.DisconnectAsync();
}
Expand Down Expand Up @@ -239,7 +242,7 @@ public async Task StartApplicationAThenStartBAndLoadMedia(ChromecastReceiver rec
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingAndPausingMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
Expand Down Expand Up @@ -277,14 +280,15 @@ public async Task TestLoadingAndPausingMedia(ChromecastReceiver receiver)
runSequence += "2";

//This checks that within 5000 ms we have loaded video and were able to pause it
Assert.True(_autoResetEvent.WaitOne(5000));
await Task.Delay(3000);
Assert.True(_autoResetEvent.WaitOne(2000));
runSequence += "3";
Assert.Equal("R1p2P3", runSequence);
await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingAndStoppingMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
Expand All @@ -303,7 +307,7 @@ public async Task TestLoadingAndStoppingMedia(ChromecastReceiver receiver)
client.MediaChannel.StatusChanged += async (object sender, EventArgs e) =>
{
try {
if (client.MediaChannel.Status.FirstOrDefault()?.PlayerState == PlayerStateType.Playing) {
if (client.MediaChannel.MediaStatus?.PlayerState == PlayerStateType.Playing) {
if (firstPlay) {
firstPlay = false;
await Task.Delay(2000); // Listen for some time
Expand All @@ -319,7 +323,8 @@ public async Task TestLoadingAndStoppingMedia(ChromecastReceiver receiver)
mediaStatus = await client.MediaChannel.LoadAsync(media);

//This checks that within 5000 ms we have loaded video and were able to pause it
Assert.True(_autoResetEvent.WaitOne(10000));
await Task.Delay(3000);
Assert.True(_autoResetEvent.WaitOne(2000));

await client.DisconnectAsync();
}
Expand All @@ -330,7 +335,6 @@ public async Task TestFailingLoadMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

var media = new Media
{
Expand All @@ -348,8 +352,7 @@ public async Task TestFailingLoadMedia(ChromecastReceiver receiver)
loadFailedException = ex;
}

Assert.True(loadFailedException?.Message == "Load failed");

Assert.Equal("Load failed", loadFailedException?.Message);
}

[Theory]
Expand All @@ -358,25 +361,20 @@ public async Task TestJoiningRunningMediaSessionAndPausingMedia(ChromecastReceiv
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
MediaStatus mediaStatus;

var media = new Media
{
ContentUrl = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/DesigningForGoogleCast.mp4"
};

mediaStatus = await client.MediaChannel.LoadAsync(media);
await client.MediaChannel.LoadAsync(media);
await client.MediaChannel.PlayAsync();

client = TestHelper.GetClientWithTestOutput(output);
var status = await client.ConnectChromecast(receiver);

var applicationRunning = status.Applications[0];



var chromecastStatus = await client.LaunchApplicationAsync(applicationRunning.AppId, true);
var applicationRunning = status.Application;
await client.LaunchApplicationAsync(applicationRunning.AppId, true);
await client.MediaChannel.PauseAsync();
}

Expand All @@ -397,7 +395,6 @@ public async Task TestRepeatingAllQueueMedia(ChromecastReceiver receiver)
Media = media
};


await client.MediaChannel.QueueLoadAsync([queueItem], null, RepeatModeType.ALL);
var test = await client.MediaChannel.PlayAsync();

Expand All @@ -421,7 +418,6 @@ public async Task TestRepeatingOffQueueMedia(ChromecastReceiver receiver)
Media = media
};


await client.MediaChannel.QueueLoadAsync([queueItem], null, RepeatModeType.OFF);
var test = await client.MediaChannel.PlayAsync();

Expand All @@ -445,7 +441,6 @@ public async Task TestRepeatingSingleQueueMedia(ChromecastReceiver receiver)
Media = media
};


await client.MediaChannel.QueueLoadAsync([queueItem], null, RepeatModeType.SINGLE);
var test = await client.MediaChannel.PlayAsync();

Expand All @@ -469,7 +464,6 @@ public async Task TestRepeatingAllAndShuffleQueueMedia(ChromecastReceiver receiv
Media = media
};


await client.MediaChannel.QueueLoadAsync([queueItem], null, RepeatModeType.ALL_AND_SHUFFLE);
var test = await client.MediaChannel.PlayAsync();

Expand Down
Loading

0 comments on commit 70b4df8

Please sign in to comment.