Skip to content

Commit

Permalink
Make Response Exception work (#297)
Browse files Browse the repository at this point in the history
* fix Logging for Heartbeat channel.

* refactored all unit tests to use Theory iso Fact for allowing to test more than one Receiver if appropriate

* proposal for channel logging

* some more channellog refactorings

* Changed the way logger is set on all channels, fixed bug with heartbeat timer and wrote a test for it

* make TestPlayMediaWorksWithoutLogging work again ;-)

* add LOAD_ERROR message
make Channel Loggers to pick up the according receive messages.

* make tests run always
- got rid of stic TestHelper (didn't really help)
- shutdown client in all Media Channel tests to keep device in idle(?) -> this helped!!!

* make load error work correct
(really use task completion object to Enter the Exception in await task)

* check some log messages and levels

* correct 3 _logger.Log with _logger?.Log

---------

Co-authored-by: Teemu Tapanila <[email protected]>
  • Loading branch information
RobertK66 and Tapanila authored Jul 13, 2024
1 parent 8744e48 commit b74f52f
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 88 deletions.
11 changes: 10 additions & 1 deletion Sharpcaster.Test/ChromecastApplicationTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ public ChromecastApplicationTester(ITestOutputHelper outputHelper, ChromecastDev
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplication(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

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

await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplicationTwice(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

Expand All @@ -44,10 +48,11 @@ public async Task ConnectToChromecastAndLaunchApplicationTwice(ChromecastReceive
}


[Theory]
[Theory(Skip = "This does not pass any more. Now my JBL reacts as the other device - not changing the Transport ID !?")]
[MemberData(nameof(ChromecastReceiversFilter.GetJblSpeaker), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining1(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

Expand All @@ -65,6 +70,7 @@ public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining1(Ch
[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetDefaultDevice), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining2(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

Expand All @@ -83,6 +89,7 @@ public async Task ConnectToChromecastAndLaunchApplicationTwiceWithoutJoining2(Ch
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplicationAThenLaunchApplicationB(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("A9BCCB7C"); //Youtube

Expand All @@ -99,6 +106,7 @@ public async Task ConnectToChromecastAndLaunchApplicationAThenLaunchApplicationB
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task ConnectToChromecastAndLaunchApplicationOnceAndJoinIt(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var status = await client.LaunchApplicationAsync("B3419EF5");

Expand All @@ -113,6 +121,7 @@ public async Task ConnectToChromecastAndLaunchApplicationOnceAndJoinIt(Chromecas
[Fact(Skip = "Seems like this isn't really working anymore and just loading a white screen")]
public async Task ConnectToChromecastAndLaunchWebPage()
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateConnectAndLoadAppClient(output, "5CB45E5A");

var req = new WebMessage
Expand Down
12 changes: 8 additions & 4 deletions Sharpcaster.Test/ChromecastConnectionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public ChromecastConnectionTester(ITestOutputHelper outputHelper, ChromecastDevi
//[MemberData(nameof(CCDevices.GetAny), MemberType = typeof(CCDevices))]
public async Task SearchChromecastsAndConnectToIt(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var status = await TestHelper.CreateAndConnectClient(output, receiver);
Assert.NotNull(status);
}

[Theory(Skip = "Test needs manuell interactions -> skipped for autotestings")]
//[Theory()]
//[MemberData(nameof(CCDevices.GetAny), MemberType = typeof(CCDevices))]
[MemberData(nameof(ChromecastReceiversFilter.GetJblSpeaker), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task SearchChromecastsAndConnectToItThenWaitForItToShutdown(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
var client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);

Assert.NotNull(client.GetChromecastStatus());
Expand All @@ -56,11 +59,12 @@ public async Task SearchChromecastsAndConnectToItThenWaitForItToShutdown(Chromec
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
//[MemberData(nameof(CCDevices.GetJblSpeaker), MemberType = typeof(CCDevices))]
//[MemberData(nameof(CCDevices.GetAny), MemberType = typeof(CCDevices))]
//[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)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

Expand Down
3 changes: 2 additions & 1 deletion Sharpcaster.Test/LoggingTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ 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);

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

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

[Theory]
Expand Down
53 changes: 25 additions & 28 deletions Sharpcaster.Test/MediaChannelTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MediaChannelTester(ITestOutputHelper outputHelper, ChromecastDevicesFixtu
[Theory(Skip ="Skipped for autotesting because manuell intervention on device is needed for this test!")]
[MemberData(nameof(ChromecastReceiversFilter.GetJblSpeaker), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestWaitForDeviceStopDuringPlayback(ChromecastReceiver receiver) {

// To get this test Passing, you have to manually operate the used Chromecast device!
// I use it with a JBL speaker device. This device has 5 buttons. (ON/OFF, Vol-, Vol+, Play/Pause, (and WLAN-Connect))
// Vol+/- and Play/Pause do operate and trigger 'unasked' MediaStatusChanged events which work as designed.
Expand All @@ -40,6 +40,7 @@ public async Task TestWaitForDeviceStopDuringPlayback(ChromecastReceiver receive
//
// after the test media starts playing you have 20 seconds to press the device stop button. Then this should pass as green!
//
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
if (receiver.Model == "JBL Playlist") {

Expand Down Expand Up @@ -78,12 +79,15 @@ public async Task TestWaitForDeviceStopDuringPlayback(ChromecastReceiver receive
} else {
Assert.Fail("This test only runs with a 'JBL Playlist' device and also needs manual operations!");
}
await client.DisconnectAsync();
}


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

var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);

AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
Expand Down Expand Up @@ -143,15 +147,16 @@ public async Task TestLoadingMediaQueueAndNavigateNextPrev(ChromecastReceiver re

//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 client.DisconnectAsync();
}

[Theory]
//[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))] // This sometimes give a INVALID_MEDIA_SESSION_ID on my Chromecast Audio ....
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadMediaQueueAndCheckContent(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);

QueueItem[] MyCd = TestHelper.CreateTestCd();

MediaStatus status = await client.GetChannel<IMediaChannel>().QueueLoadAsync(MyCd);
Expand All @@ -173,14 +178,15 @@ public async Task TestLoadMediaQueueAndCheckContent(ChromecastReceiver receiver)

QueueItem[] items2 = await client.GetChannel<IMediaChannel>().QueueGetItemsAsync(status.MediaSessionId, ids);
Assert.Equal(4, items2.Length);

await client.DisconnectAsync();
}



[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingMediaQueue(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);

QueueItem[] MyCd = TestHelper.CreateTestCd();
Expand All @@ -190,13 +196,14 @@ public async Task TestLoadingMediaQueue(ChromecastReceiver receiver) {
Assert.Equal(PlayerStateType.Playing, status.PlayerState);
Assert.Equal(2, status.Items.Count()); // The status message only contains the next (and if available Prev) Track/QueueItem!
Assert.Equal(status.CurrentItemId, status.Items[0].ItemId);

await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);

var media = new Media
Expand All @@ -209,32 +216,16 @@ public async Task TestLoadingMedia(ChromecastReceiver receiver)
Assert.Equal(PlayerStateType.Playing, status.PlayerState);
Assert.Single(status.Items);
Assert.Equal(status.CurrentItemId, status.Items[0].ItemId);

await client.DisconnectAsync();
}

//[Theory]
//[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
//public async Task TestLoadingMediaWithoutLogging(ChromecastReceiver receiver) {
// ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(receiver); // No output -> No LoggingFactory mocked

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

// MediaStatus status = await client.GetChannel<IMediaChannel>().LoadAsync(media);

// Assert.Equal(PlayerStateType.Playing, status.PlayerState);
// Assert.Single(status.Items);
// Assert.Equal(status.CurrentItemId, status.Items[0].ItemId);

//}


[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task StartApplicationAThenStartBAndLoadMedia(ChromecastReceiver receiver)
{
var client = await TestHelper.CreateAndConnectClient(output, receiver);
var th = new TestHelper();
var client = await th.CreateAndConnectClient(output, receiver);

_ = await client.LaunchApplicationAsync("A9BCCB7C", false);

Expand All @@ -246,12 +237,14 @@ public async Task StartApplicationAThenStartBAndLoadMedia(ChromecastReceiver rec
ContentUrl = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/DesigningForGoogleCast.mp4"
};
_ = await client.GetChannel<IMediaChannel>().LoadAsync(media);
await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingAndPausingMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

Expand Down Expand Up @@ -289,12 +282,14 @@ public async Task TestLoadingAndPausingMedia(ChromecastReceiver receiver)
Assert.True(_autoResetEvent.WaitOne(5000));
runSequence += "3";
Assert.Equal("R1p2P3", runSequence);
await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestLoadingAndStoppingMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

Expand Down Expand Up @@ -326,14 +321,16 @@ public async Task TestLoadingAndStoppingMedia(ChromecastReceiver receiver)
mediaStatus = await client.GetChannel<IMediaChannel>().LoadAsync(media);

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

await client.DisconnectAsync();
}

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetChromecastUltra), MemberType = typeof(ChromecastReceiversFilter))]
[MemberData(nameof(ChromecastReceiversFilter.GetAny), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestFailingLoadMedia(ChromecastReceiver receiver)
{
var TestHelper = new TestHelper();
ChromecastClient client = await TestHelper.CreateConnectAndLoadAppClient(output, receiver);
AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

Expand Down
16 changes: 8 additions & 8 deletions Sharpcaster.Test/ReceiverChannelTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public ReceiverChannelTester(ITestOutputHelper outputHelper, ChromecastDevicesFi

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestMute(ChromecastReceiver receiver)
{
public async Task TestMute(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);

var status = await client.GetChannel<ReceiverChannel>().SetMute(true);
Expand All @@ -30,8 +30,8 @@ public async Task TestMute(ChromecastReceiver receiver)

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestUnMute(ChromecastReceiver receiver)
{
public async Task TestUnMute(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);

var status = await client.GetChannel<ReceiverChannel>().SetMute(false);
Expand All @@ -40,8 +40,8 @@ public async Task TestUnMute(ChromecastReceiver receiver)

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestVolume(ChromecastReceiver receiver)
{
public async Task TestVolume(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);

var status = await client.GetChannel<ReceiverChannel>().SetVolume(0.1);
Expand All @@ -57,8 +57,8 @@ public async Task TestVolume(ChromecastReceiver receiver)

[Theory]
[MemberData(nameof(ChromecastReceiversFilter.GetAll), MemberType = typeof(ChromecastReceiversFilter))]
public async Task TestStoppingApplication(ChromecastReceiver receiver)
{
public async Task TestStoppingApplication(ChromecastReceiver receiver) {
var TestHelper = new TestHelper();
var client = await TestHelper.CreateAndConnectClient(output, receiver);

var status = await client.LaunchApplicationAsync("B3419EF5");
Expand Down
Loading

0 comments on commit b74f52f

Please sign in to comment.