Exemple #1
0
        public async void LoggerSettingsTest()
        {
            var gotMessage = false;
            var s          = new TestService();

            s.MessageReceived += (aObj, aMsg) =>
            {
                if (aMsg.Message.GetType() == typeof(Log))
                {
                    gotMessage = true;
                }
            };

            // Sending error messages will always cause an error, as they are outgoing, not incoming.
            Assert.True(await s.SendMessage(new Error("Error", Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.DefaultMsgId)) is Error);
            Assert.False(gotMessage);
            Assert.True(await s.SendMessage(new RequestLog("Trace")) is Ok);
            Assert.True(await s.SendMessage(new Error("Error", Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.DefaultMsgId)) is Error);
            Assert.True(gotMessage);
            await s.SendMessage(new RequestLog("Off"));

            gotMessage = false;
            await s.SendMessage(new Error("Error", Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.DefaultMsgId));

            Assert.False(gotMessage);
        }
Exemple #2
0
        public async void TestAddListRemoveDevices()
        {
            var d          = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var msgarray   = d.GetAllowedMessageTypes();
            var enumerable = msgarray as Type[] ?? msgarray.ToArray();

            Assert.True(enumerable.Length == 1);
            Assert.True(enumerable.Contains(typeof(SingleMotorVibrateCmd)));
            var s = new TestService();

            s.AddDeviceSubtypeManager(aLogger => new TestDeviceSubtypeManager(new ButtplugLogManager(), d));
            ButtplugMessage msgReceived = null;

            s.MessageReceived += (aObj, aMsgArgs) =>
            {
                if (!(aMsgArgs.Message is ScanningFinished))
                {
                    msgReceived = aMsgArgs.Message;
                }

                CheckDeviceMessages(msgReceived);
            };
            CheckDeviceCount(s, 0);
            Assert.True(await s.SendMessage(new StartScanning()) is Ok);
            Assert.True(await s.SendMessage(new StopScanning()) is Ok);
            Assert.True(msgReceived is DeviceAdded);
            CheckDeviceCount(s, 1);
            msgReceived = await s.SendMessage(new RequestDeviceList());

            d.RemoveDevice();
            Assert.True(msgReceived is DeviceRemoved);
            CheckDeviceCount(s, 0);
        }
Exemple #3
0
        public async void TestIncomingSystemIdMessage()
        {
            var s = new TestService();

            // Test echos back a test message with the same string and id
            Assert.True(await s.SendMessage(new Core.Messages.Test("Right", 2)) is Core.Messages.Test);
            Assert.True(await s.SendMessage(new Core.Messages.Test("Wrong", 0)) is Error);
        }
Exemple #4
0
        public async void TestValidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestService();

            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);
        }
Exemple #5
0
        public async void TestInvalidDeviceMessage()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestService();

            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);
        }
Exemple #6
0
        public async void CheckMessageReturnId()
        {
            var s = new TestService();

            s.MessageReceived += (aObj, aMsg) =>
            {
                Assert.True(aMsg.Message is RequestServerInfo);
                Assert.True(aMsg.Message.Id == 12345);
            };
            var m = new RequestServerInfo("TestClient", 12345);
            await s.SendMessage(m);

            await s.SendMessage("{\"RequestServerInfo\":{\"Id\":12345}}");
        }
Exemple #7
0
        public async void TestDuplicateDeviceAdded()
        {
            var d = new TestDevice(new ButtplugLogManager(), "TestDevice");
            var m = new TestDeviceSubtypeManager(new ButtplugLogManager(), d);
            var s = new TestService();

            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;
            }
        }
Exemple #8
0
        public async void RequestLogTraceLevelTest()
        {
            var s = new TestService();

            s.MessageReceived += s.OnMessageReceived;
            var res = await s.SendMessage("[{\"RequestLog\": {\"LogLevel\":\"Trace\",\"Id\":1}}]");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Ok);
            res = await s.SendMessage("[{\"Test\": {\"TestString\":\"Echo\",\"Id\":2}}]");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Core.Messages.Test);
            Assert.True(s.OutgoingAsync.Count == 3);
        }
Exemple #9
0
        public async void SendUnhandledMessage()
        {
            var s = new TestService();
            var r = await s.SendMessage(new FakeMessage(1));

            Assert.True(r is Error);
        }
Exemple #10
0
        public async void TestPing()
        {
            var s = new TestService(100);

            // Timeout is set to 100ms
            for (int i = 0; i < 8; i++)
            {
                Thread.Sleep(50);
                Assert.True(await s.SendMessage(new Ping()) is Ok);
            }

            // If we're still getting OK, we've suvived 400ms

            // Now lets ensure we can actually timeout
            Thread.Sleep(150);
            Assert.True(await s.SendMessage(new Ping()) is Error);
        }
Exemple #11
0
        public async void RequestLogWrongLevelTest()
        {
            var s   = new TestService();
            var res = await s.SendMessage("[{\"RequestLog\": {\"LogLevel\":\"NotALevel\",\"Id\":1}}]");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Error);
        }
Exemple #12
0
        public async void RequestLogWithoutArrayWrapperTest()
        {
            var s   = new TestService();
            var res = await s.SendMessage("{\"RequestLog\": {\"LogLevel\":\"Off\",\"Id\":1}}");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Error);
        }
Exemple #13
0
        public async void RequestNothingTest()
        {
            var s   = new TestService();
            var res = await s.SendMessage("[]");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Error);
        }
Exemple #14
0
        public async void RequestLogJsonTest()
        {
            var s   = new TestService();
            var res = await s.SendMessage("[{\"RequestLog\": {\"LogLevel\":\"Off\",\"Id\":1}}]");

            Assert.True(res.Length == 1);
            Assert.True(res[0] is Ok);
        }
Exemple #15
0
        public async void CallStartScanning()
        {
            var dm = new TestDeviceSubtypeManager(new ButtplugLogManager());
            var s  = new TestService();

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

            Assert.True(r is Ok);
            Assert.True(dm.StartScanningCalled);
        }
Exemple #16
0
        public async void SerializeUnhandledMessage()
        {
            var logger = new ButtplugLogManager();
            var r      = new ButtplugJsonMessageParser(logger).Serialize(new FakeMessage(1));

            // Even though the message is defined outside the core library, it should at least serialize
            Assert.True(r.Length > 0);

            // However it shouldn't be taken by the server.
            var s = new TestService();
            var e = await s.SendMessage(r);

            Assert.True(e.Length == 1);
            Assert.True(e[0] is Error);
        }
Exemple #17
0
        public async void TestInvalidDeviceIdMessage()
        {
            var s = new TestService();

            Assert.True((await s.SendMessage(new SingleMotorVibrateCmd(1, .2, 0))) is Error);
        }