Esempio n. 1
0
        public async Task TestStartScanning()
        {
            // Throw if we try to scan with no SubtypeManagers
            // _server.Awaiting(async s => s.SendMessageAsync(new StartScanning())).Should()
            //    .Throw<ButtplugDeviceException>();
            var dm = new TestDeviceSubtypeManager();

            _server.AddDeviceSubtypeManager(aLogger => dm);
            var r = await _server.SendMessageAsync(new StartScanning());

            r.Should().BeOfType <Ok>();
            dm.StartScanningCalled.Should().BeTrue();
            // Throw if we try to scan again
            _server.Awaiting(async s => await s.SendMessageAsync(new StartScanning())).Should()
            .Throw <ButtplugDeviceException>();

            var finishTask = new TaskCompletionSource <bool>();

            void ScanningFinishedHandler(object aObj, MessageReceivedEventArgs aMsg)
            {
                if (aMsg.Message is ScanningFinished)
                {
                    finishTask.TrySetResult(true);
                }
            }

            _server.MessageReceived += ScanningFinishedHandler;
            _server.SendMessageAsync(new StopScanning());
            await finishTask.Task;

            // Now we should be able to call StartScanning again.
            r = await _server.SendMessageAsync(new StartScanning());

            r.Should().BeOfType <Ok>();
        }
        public async Task TestValidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(d);

            _server.AddDeviceSubtypeManager(aLogger => m);
            (await _server.SendMessageAsync(new StartScanning())).Should().BeOfType <Ok>();
            (await _server.SendMessageAsync(new StopScanning())).Should().BeOfType <Ok>();
            (await _server.SendMessageAsync(new SingleMotorVibrateCmd(1, .2))).Should().BeOfType <Ok>();
        }
Esempio n. 3
0
        public async Task TestStopScanning()
        {
            var dm = new TestDeviceSubtypeManager();

            _server.AddDeviceSubtypeManager(aLogger => dm);
            var r = await _server.SendMessageAsync(new StopScanning());

            r.Should().BeOfType <Ok>();
            dm.StopScanningCalled.Should().BeTrue();
        }
        public async Task TestInvalidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(d);

            _server.AddDeviceSubtypeManager(aLogger => m);
            (await _server.SendMessageAsync(new StartScanning())).Should().BeOfType <Ok>();
            (await _server.SendMessageAsync(new StopScanning())).Should().BeOfType <Ok>();
            _server.Awaiting(async aServer => await aServer.SendMessageAsync(new FleshlightLaunchFW12Cmd(1, 0, 0)))
            .Should().Throw <ButtplugDeviceException>();
        }
        public async void TestInvalidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestServer();

            s.AddDeviceSubtypeManager(aLogger => { return(m); });
            Assert.True(await s.SendMessage(new StartScanning()) is Ok);
            Assert.True(await s.SendMessage(new StopScanning()) is Ok);
            Assert.True(await s.SendMessage(new FleshlightLaunchFW12Cmd(1, 0, 0)) is Error);
        }
        public async void TestValidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestServer();

            s.AddDeviceSubtypeManager(aLogger => { return(m); });
            Assert.True(await s.SendMessage(new StartScanning()) is Ok);
            Assert.True(await s.SendMessage(new StopScanning()) is Ok);
            Assert.True(await s.SendMessage(new SingleMotorVibrateCmd(1, .2)) is Ok);
        }
Esempio n. 7
0
        public async void CallStartScanning()
        {
            var dm = new TestDeviceSubtypeManager(new ButtplugLogManager());
            var s  = new TestServer();

            s.AddDeviceSubtypeManager(aLogger => { return(dm); });
            var r = await s.SendMessage(new StartScanning());

            Assert.True(r is Ok);
            Assert.True(dm.StartScanningCalled);
        }
        public void CallStartScanning()
        {
            var dm = new TestDeviceSubtypeManager();
            var s  = new TestServer();

            s.AddDeviceSubtypeManager(aLogger => { return(dm); });
            var r = s.SendMessage(new StartScanning()).GetAwaiter().GetResult();

            Assert.True(r is Ok);
            Assert.True(dm.StartScanningCalled);
        }
        public async Task TestDeviceListMessageListDowngrade()
        {
            // If we request a DeviceAdded/DeviceList message on a client with an older spec version
            // than the server, it should remove all non-spec-supported message types.
            _server = new TestServer();
            (await _server.SendMessageAsync(new RequestServerInfo("TestClient", 1, 0))).Should().BeOfType <ServerInfo>();

            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(d);

            _server.AddDeviceSubtypeManager(aLogger => m);
            var msgReceived = false;

            _server.MessageReceived += (aObj, aMsgArgs) =>
            {
                switch (aMsgArgs.Message)
                {
                case DeviceAdded da:
                    msgReceived = true;
                    da.DeviceMessages.Keys.Should().Contain("StopDeviceCmd");
                    da.DeviceMessages.Keys.Should().Contain("SingleMotorVibrateCmd");

                    // Should not contain VibrateCmd, even though it is part of the device otherwise.
                    da.DeviceMessages.Keys.Should().NotContain("VibrateCmd");
                    break;

                case ScanningFinished _:
                    break;

                default:
                    msgReceived = true;
                    aMsgArgs.Message.Should().NotBeOfType <DeviceAdded>();
                    break;
                }
            };
            for (var i = 0; i < 2; ++i)
            {
                (await _server.SendMessageAsync(new StartScanning())).Should().BeOfType <Ok>();
                (await _server.SendMessageAsync(new StopScanning())).Should().BeOfType <Ok>();
                var x = await _server.SendMessageAsync(new RequestDeviceList());

                x.Should().BeOfType <DeviceList>();
                var dl = x as DeviceList;

                dl.Devices.Length.Should().Be(1);
                dl.Devices[0].DeviceIndex.Should().Be(1U);
                dl.Devices[0].DeviceName.Should().Be("TestDevice");

                (i == 0 ? msgReceived : !msgReceived).Should().BeTrue();
                msgReceived = false;
            }
        }
        public async void TestDuplicateDeviceAdded()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestServer();

            s.AddDeviceSubtypeManager(aLogger => { return(m); });
            var msgReceived = false;

            s.MessageReceived += (aObj, aMsgArgs) =>
            {
                switch (aMsgArgs.Message)
                {
                case DeviceAdded da:
                    msgReceived = true;
                    Assert.True(da.DeviceName == "TestDevice");
                    Assert.True(da.DeviceIndex == 1);
                    Assert.True(da.Id == 0);
                    break;

                case ScanningFinished _:
                    break;

                default:
                    msgReceived = true;
                    Assert.False(aMsgArgs.Message is DeviceAdded);
                    break;
                }
            };
            for (var i = 0; i < 2; ++i)
            {
                Assert.True((await s.SendMessage(new StartScanning())) is Ok);
                Assert.True((await s.SendMessage(new StopScanning())) is Ok);
                var x = await s.SendMessage(new RequestDeviceList());

                Assert.True(x is DeviceList);
                switch (x)
                {
                case DeviceList dl:
                    Assert.True(dl.Devices.Length == 1);
                    Assert.True(dl.Devices[0].DeviceIndex == 1);
                    Assert.True(dl.Devices[0].DeviceName == "TestDevice");
                    break;
                }

                Assert.True(i == 0 ? msgReceived : !msgReceived, "DeviceAdded fired at incorrect time!");
                msgReceived = false;
            }
        }
        public async Task TestDuplicateDeviceAdded()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(d);

            _server.AddDeviceSubtypeManager(aLogger => m);
            var msgReceived = false;

            _server.MessageReceived += (aObj, aMsgArgs) =>
            {
                switch (aMsgArgs.Message)
                {
                case DeviceAdded da:
                    msgReceived = true;
                    da.DeviceName.Should().Be("TestDevice");
                    da.DeviceIndex.Should().Be(1);
                    da.Id.Should().Be(0);
                    break;

                case ScanningFinished _:
                    break;

                default:
                    msgReceived = true;
                    aMsgArgs.Message.Should().NotBeOfType <DeviceAdded>();
                    break;
                }
            };
            for (var i = 0; i < 2; ++i)
            {
                (await _server.SendMessageAsync(new StartScanning())).Should().BeOfType <Ok>();
                (await _server.SendMessageAsync(new StopScanning())).Should().BeOfType <Ok>();
                var x = await _server.SendMessageAsync(new RequestDeviceList());

                x.Should().BeOfType <DeviceList>();
                switch (x)
                {
                case DeviceList dl:
                    dl.Devices.Length.Should().Be(1);
                    dl.Devices[0].DeviceIndex.Should().Be(1U);
                    dl.Devices[0].DeviceName.Should().Be("TestDevice");
                    break;
                }

                (i == 0 ? msgReceived : !msgReceived).Should().BeTrue();
                msgReceived = false;
            }
        }