/**
 *    ||          ____  _ __
 * +------+      / __ )(_) /_______________ _____  ___
 * | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
 * +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
 *  ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
 *
 * Copyright (C) 2015 Bitcraze AB
 *
 * Crazyflie Nano Quadcopter Client
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package se.bitcraze.crazyflie.lib.crazyflie;

import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.Test;

import se.bitcraze.crazyflie.lib.TestConnectionAdapter;
import se.bitcraze.crazyflie.lib.crazyradio.RadioDriver;
import se.bitcraze.crazyflie.lib.crtp.CommanderPacket;
import se.bitcraze.crazyflie.lib.crtp.CrtpDriver;
import se.bitcraze.crazyflie.lib.crtp.CrtpPacket;
import se.bitcraze.crazyflie.lib.crtp.CrtpPort;
import se.bitcraze.crazyflie.lib.usb.UsbLinkJava;

public class CrazyflieTest {

    public static int channel = 10;
    public static int datarate = 0;

    private boolean connectionRequested = false;
    private boolean connected = false;
    private boolean setupFinished = false;
//    private boolean connectionFailed = false;
//    private boolean connectionLost = false;
    private boolean disconnected = false;
    private boolean linkQuality = false;

    public static CrtpDriver getConnectionImpl() {
        return new RadioDriver(new UsbLinkJava());
    }

    @Test
    public void testCrazyflie() {
        Crazyflie crazyflie = new Crazyflie(getConnectionImpl(), new File("src/test"));

        crazyflie.connect(channel, datarate);

        for (int i = 0; i < 10; i++) {
            crazyflie.sendPacket(new CommanderPacket(0, 0, 0, (char) 0));
            try {
                Thread.sleep(50, 0);
            } catch (InterruptedException e) {
                break;
            }
        }
        crazyflie.disconnect();
    }

    @Test
    public void testDataListener() {
        Crazyflie crazyflie = new Crazyflie(getConnectionImpl());

        crazyflie.addDataListener(new DataListener(CrtpPort.CONSOLE) {

            @Override
            public void dataReceived(CrtpPacket packet) {
                System.out.println("Received " + packet);
            }

        });

        crazyflie.connect(channel, datarate);

        for (int i = 0; i < 30; i++) {
            crazyflie.sendPacket(new CommanderPacket(0, 0, 0, (char) 0));
            try {
                Thread.sleep(50, 0);
            } catch (InterruptedException e) {
                break;
            }
        }
        crazyflie.disconnect();
    }

    @Test
    public void testConnectionListener() {
        final Crazyflie crazyflie = new Crazyflie(getConnectionImpl());

        crazyflie.getDriver().addConnectionListener(new TestConnectionAdapter() {

            @Override
            public void connectionRequested(String connectionInfo) {
                super.connectionRequested(connectionInfo);
                connectionRequested = true;
            }

            @Override
            public void connected(String connectionInfo) {
                super.connected(connectionInfo);
                connected = true;
            }

            @Override
            public void setupFinished(String connectionInfo) {
                super.setupFinished(connectionInfo);
                setupFinished = true;
            }

            @Override
            public void disconnected(String connectionInfo) {
                super.disconnected(connectionInfo);
                disconnected = true;
            }

            public void linkQualityUpdated(int percent) {
                System.out.println("LINK QUALITY: " + percent);
                linkQuality = true;
            }

        });

        crazyflie.connect(channel, datarate);

        int timeout = 5000;
        
        while (!setupFinished && timeout > 0) {
            try {
                Thread.sleep(50, 0);
            } catch (InterruptedException e) {
                break;
            }
            timeout -= 50;
        }

        crazyflie.disconnect();
        
        assertTrue(connectionRequested);
        assertTrue(connected);
        assertTrue(setupFinished);
        assertTrue(disconnected);
        assertTrue(linkQuality);
    }
}

@@ -1096,24 +1096,68 @@
 crazyflie;%0A%0A
+import static org.junit.Assert.assertFalse;%0A
 import stati
@@ -1204,15 +1204,22 @@
 ava.
-io.File
+util.ArrayList
 ;%0A%0Ai
@@ -1608,16 +1608,73 @@
 kJava;%0A%0A
+//TODO: use MockDriver if no real Crazyflie is available%0A
 public c
@@ -2190,489 +2190,8 @@
  %7D%0A%0A
-    @Test%0A    public void testCrazyflie() %7B%0A        Crazyflie crazyflie = new Crazyflie(getConnectionImpl(), new File(%22src/test%22));%0A%0A        crazyflie.connect(channel, datarate);%0A%0A        for (int i = 0; i %3C 10; i++) %7B%0A            crazyflie.sendPacket(new CommanderPacket(0, 0, 0, (char) 0));%0A            try %7B%0A                Thread.sleep(50, 0);%0A            %7D catch (InterruptedException e) %7B%0A                break;%0A            %7D%0A        %7D%0A        crazyflie.disconnect();%0A    %7D%0A%0A
     
@@ -2292,32 +2292,119 @@
 ectionImpl());%0A%0A
+        final ArrayList%3CCrtpPacket%3E packetList = new ArrayList%3CCrtpPacket%3E();%0A        %0A
         crazyfli
@@ -2535,24 +2535,64 @@
 t packet) %7B%0A
+                packetList.add(packet);%0A
             
@@ -3002,24 +3002,110 @@
 sconnect();%0A
+        %0A        assertFalse(%22PacketList should not be empty%22, packetList.isEmpty());%0A
     %7D%0A%0A    @

Remove redundant testCrazyflie(), fix testDataListener()