<commit_before>﻿using System.Threading;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Rssdp;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ChromeCast.Desktop.AudioStreamer.Application;
using ChromeCast.Desktop.AudioStreamer.Classes;
using ChromeCast.Desktop.AudioStreamer.Communication;
using ChromeCast.Desktop.AudioStreamer.Communication.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Application.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Discover.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Discover;
using ChromeCast.Desktop.AudioStreamer.Streaming;
using ChromeCast.Desktop.AudioStreamer.Streaming.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Test.Classes;
using ChromeCast.Desktop.AudioStreamer.Communication.Classes;
using ChromeCast.Desktop.AudioStreamer.ProtocolBuffer;

namespace ChromeCast.Desktop.AudioStreamer.Test.Application
{
    [TestClass]
    public class DevicesTest
    {
        AutoResetEvent asyncEvent;
        Device device;

        [TestInitialize]
        public void Initialize()
        {
            DependencyFactory.Container
                .RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
                .RegisterType<IApplicationLogic, ApplicationLogic>(new ContainerControlledLifetimeManager())
                .RegisterType<IMainForm, MainForm>(new ContainerControlledLifetimeManager())
                .RegisterType<IDevices, Devices>(new ContainerControlledLifetimeManager())
                .RegisterType<IDiscoverDevices, DiscoverDevices>()
                .RegisterType<IChromeCastMessages, ChromeCastMessages>()
                .RegisterType<IDeviceConnection, TestDeviceConnection>()
                .RegisterType<IDeviceCommunication, DeviceCommunication>()
                .RegisterType<IStreamingConnection, StreamingConnection>()
                .RegisterType<IDiscoverServiceSSDP, DiscoverServiceSSDP>()
                .RegisterType<IDeviceReceiveBuffer, DeviceReceiveBuffer>()
                .RegisterType<ILoopbackRecorder, LoopbackRecorder>()
                .RegisterType<IDeviceStatusTimer, DeviceStatusTimer>()
                .RegisterType<IConfiguration, Configuration>()
                .RegisterType<IStreamingRequestsListener, StreamingRequestsListener>()
                .RegisterType<IRiff, Riff>()
                .RegisterType<IDevice, Device>();
        }

        [TestMethod]
        public void TestDevices()
        {
            asyncEvent = new AutoResetEvent(false);

            // Add a device
            var devices = new Devices();
            devices.SetCallback(OnAddDeviceCallback);
            var discoveredSsdpDevice = new DiscoveredSsdpDevice { Usn = "usn", DescriptionLocation = new System.Uri("http://192.168.111.111") };
            var ssdpDevice = new SsdpRootDevice { FriendlyName = "Device_Name" };
            devices.OnDeviceAvailable(discoveredSsdpDevice, ssdpDevice);

            asyncEvent.WaitOne(100);

            Assert.AreEqual(ssdpDevice.FriendlyName, device.GetFriendlyName());
            Assert.AreEqual(discoveredSsdpDevice.Usn, device.GetUsn());
            Assert.AreEqual(discoveredSsdpDevice.DescriptionLocation.Host, device.GetHost());

            // Not connected, volume messages are not send.
            TestMessagesWhenNotConnected(devices);

            // Connect and launch app
            TestConnectAndLoadMedia(devices);

            // Test the other messages
            TestMessages(devices);
        }

        private void TestConnectAndLoadMedia(Devices devices)
        {
            var testDeviceConnection = (TestDeviceConnection)device.GetDeviceConnection();

            // Connect & Launch
            device.OnClickDeviceButton(null, null);

            // Connect & Load Media
            device.OnReceiveMessage(ReceiverStatusMessageFromDevice());

            Assert.AreEqual(4, testDeviceConnection.messagesSend.Count);
            Assert.AreEqual("CONNECT", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[0].PayloadUtf8).type);
            Assert.AreEqual("LAUNCH", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[1].PayloadUtf8).type);
            Assert.AreEqual("CONNECT", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[2].PayloadUtf8).type);
            Assert.AreEqual("LOAD", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[3].PayloadUtf8).type);
        }

        private void TestMessages(Devices devices)
        {
            var testDeviceConnection = (TestDeviceConnection)device.GetDeviceConnection();

            testDeviceConnection.messagesSend.Clear();
            devices.VolumeUp();
            devices.VolumeDown();
            devices.VolumeMute();
            devices.OnGetStatus();
<commit_msg>Fixed unittest: Volume messages are blocked when the last one was less then one second ago.<commit_after>﻿using System.Threading;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Rssdp;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ChromeCast.Desktop.AudioStreamer.Application;
using ChromeCast.Desktop.AudioStreamer.Classes;
using ChromeCast.Desktop.AudioStreamer.Communication;
using ChromeCast.Desktop.AudioStreamer.Communication.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Application.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Discover.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Discover;
using ChromeCast.Desktop.AudioStreamer.Streaming;
using ChromeCast.Desktop.AudioStreamer.Streaming.Interfaces;
using ChromeCast.Desktop.AudioStreamer.Test.Classes;
using ChromeCast.Desktop.AudioStreamer.Communication.Classes;
using ChromeCast.Desktop.AudioStreamer.ProtocolBuffer;

namespace ChromeCast.Desktop.AudioStreamer.Test.Application
{
    [TestClass]
    public class DevicesTest
    {
        AutoResetEvent asyncEvent;
        Device device;

        [TestInitialize]
        public void Initialize()
        {
            DependencyFactory.Container
                .RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
                .RegisterType<IApplicationLogic, ApplicationLogic>(new ContainerControlledLifetimeManager())
                .RegisterType<IMainForm, MainForm>(new ContainerControlledLifetimeManager())
                .RegisterType<IDevices, Devices>(new ContainerControlledLifetimeManager())
                .RegisterType<IDiscoverDevices, DiscoverDevices>()
                .RegisterType<IChromeCastMessages, ChromeCastMessages>()
                .RegisterType<IDeviceConnection, TestDeviceConnection>()
                .RegisterType<IDeviceCommunication, DeviceCommunication>()
                .RegisterType<IStreamingConnection, StreamingConnection>()
                .RegisterType<IDiscoverServiceSSDP, DiscoverServiceSSDP>()
                .RegisterType<IDeviceReceiveBuffer, DeviceReceiveBuffer>()
                .RegisterType<ILoopbackRecorder, LoopbackRecorder>()
                .RegisterType<IDeviceStatusTimer, DeviceStatusTimer>()
                .RegisterType<IConfiguration, Configuration>()
                .RegisterType<IStreamingRequestsListener, StreamingRequestsListener>()
                .RegisterType<IRiff, Riff>()
                .RegisterType<IDevice, Device>();
        }

        [TestMethod]
        public void TestDevices()
        {
            asyncEvent = new AutoResetEvent(false);

            // Add a device
            var devices = new Devices();
            devices.SetCallback(OnAddDeviceCallback);
            var discoveredSsdpDevice = new DiscoveredSsdpDevice { Usn = "usn", DescriptionLocation = new System.Uri("http://192.168.111.111") };
            var ssdpDevice = new SsdpRootDevice { FriendlyName = "Device_Name" };
            devices.OnDeviceAvailable(discoveredSsdpDevice, ssdpDevice);

            asyncEvent.WaitOne(100);

            Assert.AreEqual(ssdpDevice.FriendlyName, device.GetFriendlyName());
            Assert.AreEqual(discoveredSsdpDevice.Usn, device.GetUsn());
            Assert.AreEqual(discoveredSsdpDevice.DescriptionLocation.Host, device.GetHost());

            // Not connected, volume messages are not send.
            TestMessagesWhenNotConnected(devices);

            // Connect and launch app
            TestConnectAndLoadMedia(devices);

            // Test the other messages
            TestMessages(devices);
        }

        private void TestConnectAndLoadMedia(Devices devices)
        {
            var testDeviceConnection = (TestDeviceConnection)device.GetDeviceConnection();

            // Connect & Launch
            device.OnClickDeviceButton(null, null);

            // Connect & Load Media
            device.OnReceiveMessage(ReceiverStatusMessageFromDevice());

            Assert.AreEqual(4, testDeviceConnection.messagesSend.Count);
            Assert.AreEqual("CONNECT", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[0].PayloadUtf8).type);
            Assert.AreEqual("LAUNCH", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[1].PayloadUtf8).type);
            Assert.AreEqual("CONNECT", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[2].PayloadUtf8).type);
            Assert.AreEqual("LOAD", GetMessage<PayloadMessageBase>(testDeviceConnection.messagesSend[3].PayloadUtf8).type);
        }

        private void TestMessages(Devices devices)
        {
            var testDeviceConnection = (TestDeviceConnection)device.GetDeviceConnection();
            asyncEvent = new AutoResetEvent(false);

            testDeviceConnection.messagesSend.Clear();
            devices.VolumeUp();
            asyncEvent.WaitOne(1000);
            devices.VolumeDown();
            devices.VolumeMute();
            devices.OnGetStatus();

{'fraction_non_alphanumeric': 0.08870478884623156, 'fraction_numerical': 0.005253586583148111, 'mean_word_length': 3.1263026260942057, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4825160', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2393, 'n_words': 385}