public void OneFailedIncomingConnection()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSink);

            lsnr.ServiceName = "weeee";
            lsnr.Start();
            IAsyncResult       ar    = lsnr.BeginAcceptBluetoothClient(null, null);
            TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port1);
            port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS); // now begun immediately
            port0.NewEvent(PORT_EV.CONNECT_ERR);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            port0.AssertOpenServerCalledAndClear(29);
            try {
                try {
                    BluetoothClient cli = lsnr.EndAcceptBluetoothClient(ar);
                } catch (System.IO.IOException ioexShouldNotWrapSEx) { //HACK ioexShouldNotWrapSEx
                    throw ioexShouldNotWrapSEx.InnerException;
                }
                Assert.Fail("should have thrown!");
            } catch (SocketException) {
            }
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            //
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port1.AssertCloseCalledOnce("second acceptor closed");
            //port0.AssertCloseCalledOnce("first failed connection now closed");
            //
            BluetoothEndPoint lep = lsnr.LocalEndPoint;

            sdpSvc.AssertCalls(
                "AddServiceClassIdList: 00001304-0000-1000-8000-00805f9b34fb" + NewLine
                + "AddRFCommProtocolDescriptor: " + lep.Port + NewLine
                + "AddServiceName: weeee" + NewLine
                );
        }
        public void OneConnection_PendingBeforeSyncAccept()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            try {
                Assert.IsFalse(lsnr.Pending(), "!Pending before Start");
                lsnr.Start();
                port0.AssertOpenServerCalledAndClear(29);            //adter Start?
                TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();
                port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS); // now begun immediately
                f.queueIRfCommPort.Enqueue(port1);
                Assert.IsFalse(lsnr.Pending(), "!Pending before");
                port0.NewEvent(PORT_EV.CONNECTED);
                Thread.Sleep(100);
                Assert.IsTrue(lsnr.Pending(), "Pending");
                BluetoothClient cli    = lsnr.AcceptBluetoothClient();
                TestSdpService2 sdpSvc = f.GetTestSdpService();
                Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
                lsnr.Stop();
                Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
                //
                Assert.IsTrue(cli.Connected, "cli.Connected");
                //TODOAssert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
                //TODO port1.AssertCloseCalledOnce("second acceptor closed");
                cli.Close();
                port0.AssertCloseCalledOnce("first accepted connection now closed");
                //
                BluetoothEndPoint lep = lsnr.LocalEndPoint;
                sdpSvc.AssertCalls(
                    "AddServiceClassIdList: 00001303-0000-1000-8000-00805f9b34fb" + NewLine
                    + "AddRFCommProtocolDescriptor: " + lep.Port + NewLine
                    );
                //
                //
            } finally {
                lsnr.Stop(); // See errors that might otherwise occur on the Finalizer.
            }
        }
        public void OneConnection()
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            BluetoothFactory.SetFactory(f);
            TestLsnrRfcommPort port1 = AddSomeCreatablePorts(f);

            //
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            lsnr.Start();
            IAsyncResult ar = lsnr.BeginAcceptBluetoothClient(null, null);

            port0.AssertOpenServerCalledAndClear(29);//adter Start?
            port0.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli    = lsnr.EndAcceptBluetoothClient(ar);
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            //
            Assert.IsTrue(cli.Connected, "cli.Connected");
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port1.AssertCloseCalledOnce("second acceptor closed");
            cli.Close();
            port0.AssertCloseCalledOnce("first accepted connection now closed");
            //
            BluetoothEndPoint lep = lsnr.LocalEndPoint;

            sdpSvc.AssertCalls(
                "AddServiceClassIdList: 00001303-0000-1000-8000-00805f9b34fb" + NewLine
                + "AddRFCommProtocolDescriptor: " + lep.Port + NewLine
                );
        }
        void MultipleConnection_(bool auth, bool encrypt,
                                 BTM_SEC expectedSecurityLevel)
        {
            TestWcLsnrBluetoothFactory f      = new TestWcLsnrBluetoothFactory();
            TestLsnrRfCommIf           commIf = new TestLsnrRfCommIf();

            f.queueIRfCommIf.Enqueue(commIf);
            TestLsnrRfcommPort port0 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port0);
            port0.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            BluetoothFactory.SetFactory(f);
            //
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.VideoSource);

            if (auth)
            {
                lsnr.Authenticate = true;
            }
            if (encrypt)
            {
                lsnr.Encrypt = true;
            }
            Assert.AreEqual(auth, lsnr.Authenticate, ".Authenticate 1");
            Assert.AreEqual(encrypt, lsnr.Encrypt, ".Encrypt 1");
            lsnr.Start();
            IAsyncResult ar;

            commIf.AssertSetSecurityLevel(expectedSecurityLevel, true);
            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port0.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port1 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port1);
            port1.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            port0.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli0 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port1.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port2 = new TestLsnrRfcommPort();

            port2.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            f.queueIRfCommPort.Enqueue(port2);
            port1.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli1 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port2.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port3 = new TestLsnrRfcommPort();

            port3.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            f.queueIRfCommPort.Enqueue(port3);
            port2.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli2 = lsnr.EndAcceptBluetoothClient(ar);

            //
            ar = lsnr.BeginAcceptBluetoothClient(null, null);
            port3.AssertOpenServerCalledAndClear(29);//adter Start?
            TestLsnrRfcommPort port4 = new TestLsnrRfcommPort();

            f.queueIRfCommPort.Enqueue(port4);
            port4.SetOpenServerResult(PORT_RETURN_CODE.SUCCESS);
            port3.NewEvent(PORT_EV.CONNECTED);
            TestsApmUtils.SafeNoHangWaitShort(ar, "Accept");
            Assert.IsTrue(ar.IsCompleted, "IsCompleted");
            BluetoothClient cli3 = lsnr.EndAcceptBluetoothClient(ar);
            //
            TestSdpService2 sdpSvc = f.GetTestSdpService();

            Assert.AreEqual(0, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            lsnr.Stop();
            Assert.AreEqual(1, sdpSvc.NumDisposeCalls, "NumDisposeCalls");
            Assert.AreEqual(auth, lsnr.Authenticate, ".Authenticate 2");
            Assert.AreEqual(encrypt, lsnr.Encrypt, ".Encrypt 2");
            //
            Assert.IsTrue(cli0.Connected, "0 cli.Connected");
            Assert.AreEqual(0, f.queueIRfCommPort.Count, "Used both ports");
            port4.AssertCloseCalledOnce("4 acceptor closed");
            cli0.Close();
            port0.AssertCloseCalledOnce("0 accepted connection now closed");
            //
            Assert.IsTrue(cli1.Connected, "1 cli.Connected");
            cli1.Close();
            port1.AssertCloseCalledOnce("1 accepted connection now closed");
            //
            Assert.IsTrue(cli2.Connected, "2 cli.Connected");
            cli2.Close();
            port2.AssertCloseCalledOnce("2 accepted connection now closed");
            //
            Assert.IsTrue(cli3.Connected, "3 cli.Connected");
            cli3.Close();
            port3.AssertCloseCalledOnce("3 accepted connection now closed");
        }