<commit_before>package com.punchthrough.bean.sdk;

import com.punchthrough.bean.sdk.TestingUtils.LooperRunner;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.Suppress;

import com.punchthrough.bean.sdk.message.BeanError;
import com.punchthrough.bean.sdk.message.Callback;
import com.punchthrough.bean.sdk.message.DeviceInfo;
import com.punchthrough.bean.sdk.message.ScratchBank;
import com.punchthrough.bean.sdk.message.ScratchData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Integration tests for the Bean.
 * <p/>
 * Prerequisites:
 * - Bean within range
 * - Android device connected over USB
 */
public class TestBean extends AndroidTestCase {

    private LooperRunner lr = new LooperRunner();
    private Thread lrThread = new Thread(lr);

    protected void setUp() {
        lrThread.start();
    }

    protected void tearDown() throws InterruptedException {
        /*
        * "Speaking very generally, never quit() your looper threads. That method exists
        * mostly for historical and testing reasons. In Real Life™, I recommend that you
        * continue to reuse the same looper thread(s) for the life of the process rather
        * than creating/quitting them."
        * */
//        lr.quit();
//        lrThread.join();
    }

    private List<Bean> getBeans(int num) throws InterruptedException {
        final CountDownLatch beanLatch = new CountDownLatch(num);
        final List<Bean> beans = new ArrayList<>();

        BeanDiscoveryListener listener = new BeanDiscoveryListener() {
            @Override
            public void onBeanDiscovered(Bean bean, int rssi) {
                beans.add(bean);
                beanLatch.countDown();
            }

            @Override
            public void onDiscoveryComplete() {
                beanLatch.countDown();
            }
        };

        boolean startedOK = BeanManager.getInstance().startDiscovery(listener);
        assertThat(startedOK).isTrue();
        beanLatch.await(60, TimeUnit.SECONDS);
        assertThat(beans.size()).isEqualTo(num);
        return beans;
    }

    private Bean getBeanByName(String name) throws InterruptedException {
        final CountDownLatch beanLatch = new CountDownLatch(1);
        final List<Bean> beans = new ArrayList<>();

        final String targetName = name;

        BeanDiscoveryListener listener = new BeanDiscoveryListener() {
            @Override
            public void onBeanDiscovered(Bean bean, int rssi) {
                if (bean.getDevice().getName().equals(targetName)) {
                    System.out.println("Found Bean!!!!!!!");
                    beans.add(bean);
                    beanLatch.countDown();
                }
            }

            @Override
            public void onDiscoveryComplete() {
                System.out.println("Nothing");
                beanLatch.countDown();
            }
        };

        boolean startedOK = BeanManager.getInstance().startDiscovery(listener);
        assertThat(startedOK).isTrue();
        beanLatch.await(60, TimeUnit.SECONDS);
        if (beans.isEmpty()) {
            fail("Couldn't find bean by name: " + name);
        }
        return beans.get(0);
    }

    public void testBeanDeviceInfo() throws InterruptedException {
        Bean bean = this.getBeans(1).get(0);
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        final HashMap testState = new HashMap();
        testState.put("connected", false);

        BeanListener beanListener = new BeanListener() {
            @Override
            public void onConnected() {
                testState.put("connected", true);
                connectionLatch.countDown();
            }

            @Override
            public void onConnectionFailed() {
                fail("Connection failed!");
            }

            @Override
            public void onDisconnected() {
            }

            @Override
            public void onSerialMessageReceived(byte[] data) {
            }

            @Override
            public void onScratchValueChanged(ScratchBank bank, byte[] value) {
            }

            @Override
            public void onError(BeanError error) {
                fail(error.toString());
            }
        };

        bean.connect(getContext(), beanListener);
        connectionLatch.await();
        assertThat(testState.get("connected")).isEqualTo(true);

        final CountDownLatch deviceInfoLatch = new CountDownLatch(1);
        testState.put("device_info", null);
        bean.readDeviceInfo(new Callback<DeviceInfo>() {
            @Override
            public void onResult(DeviceInfo deviceInfo) {
                testState.put("device_info", deviceInfo);
                deviceInfoLatch.countDown();
            }
        });
        deviceInfoLatch.await();
        assertThat(testState.get("device_info")).isNotNull();

        if (bean.isConnected()) {
            bean.disconnect();
        }
    }

    public void testBeanReadWriteScratchBank() throws InterruptedException {
        Bean bean = this.getBeans(1).get(0);
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        final HashMap testState = new HashMap();
        testState.put("connected", false);

        BeanListener beanListener = new BeanListener() {
            @Override
            public void onConnected() {
                testState.put("connected", true);
                connectionLatch.countDown();

            }

            @Override
            public void onConnectionFailed() {
                fail("Connection failed!");
            }

            @Override
            public void onDisconnected() {
            }

            @Override<commit_msg>tests: explicitly connect to a bean named TESTBEAN for integration tests<commit_after>package com.punchthrough.bean.sdk;

import com.punchthrough.bean.sdk.TestingUtils.LooperRunner;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.Suppress;

import com.punchthrough.bean.sdk.message.BeanError;
import com.punchthrough.bean.sdk.message.Callback;
import com.punchthrough.bean.sdk.message.DeviceInfo;
import com.punchthrough.bean.sdk.message.ScratchBank;
import com.punchthrough.bean.sdk.message.ScratchData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Integration tests for the Bean.
 * <p/>
 * Prerequisites:
 * - Bean within range
 * - Android device connected over USB
 */
public class TestBean extends AndroidTestCase {

    private LooperRunner lr = new LooperRunner();
    private Thread lrThread = new Thread(lr);

    protected void setUp() {
        lrThread.start();
    }

    protected void tearDown() throws InterruptedException {
        /*
        * "Speaking very generally, never quit() your looper threads. That method exists
        * mostly for historical and testing reasons. In Real Life™, I recommend that you
        * continue to reuse the same looper thread(s) for the life of the process rather
        * than creating/quitting them."
        * */
//        lr.quit();
//        lrThread.join();
    }

    private List<Bean> getBeans(int num) throws InterruptedException {
        final CountDownLatch beanLatch = new CountDownLatch(num);
        final List<Bean> beans = new ArrayList<>();

        BeanDiscoveryListener listener = new BeanDiscoveryListener() {
            @Override
            public void onBeanDiscovered(Bean bean, int rssi) {
                beans.add(bean);
                beanLatch.countDown();
            }

            @Override
            public void onDiscoveryComplete() {
                beanLatch.countDown();
            }
        };

        boolean startedOK = BeanManager.getInstance().startDiscovery(listener);
        assertThat(startedOK).isTrue();
        beanLatch.await(60, TimeUnit.SECONDS);
        assertThat(beans.size()).isEqualTo(num);
        return beans;
    }

    private Bean getBeanByName(String name) throws InterruptedException {
        final CountDownLatch beanLatch = new CountDownLatch(1);
        final List<Bean> beans = new ArrayList<>();

        final String targetName = name;

        BeanDiscoveryListener listener = new BeanDiscoveryListener() {
            @Override
            public void onBeanDiscovered(Bean bean, int rssi) {
                if (bean.getDevice().getName().equals(targetName)) {
                    System.out.println("Found Bean!!!!!!!");
                    beans.add(bean);
                    beanLatch.countDown();
                }
            }

            @Override
            public void onDiscoveryComplete() {
                System.out.println("Nothing");
                beanLatch.countDown();
            }
        };

        boolean startedOK = BeanManager.getInstance().startDiscovery(listener);
        assertThat(startedOK).isTrue();
        beanLatch.await(60, TimeUnit.SECONDS);
        if (beans.isEmpty()) {
            fail("Couldn't find bean by name: " + name);
        }
        return beans.get(0);
    }

    public void testBeanDeviceInfo() throws InterruptedException {
        /** Read device information from a bean
         *
         * Warning: This test requires a nearby bean named "TESTBEAN"
         */

        Bean bean = this.getBeanByName("TESTBEAN");
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        final HashMap testState = new HashMap();
        testState.put("connected", false);

        BeanListener beanListener = new BeanListener() {
            @Override
            public void onConnected() {
                testState.put("connected", true);
                connectionLatch.countDown();
            }

            @Override
            public void onConnectionFailed() {
                fail("Connection failed!");
            }

            @Override
            public void onDisconnected() {
            }

            @Override
            public void onSerialMessageReceived(byte[] data) {
            }

            @Override
            public void onScratchValueChanged(ScratchBank bank, byte[] value) {
            }

            @Override
            public void onError(BeanError error) {
                fail(error.toString());
            }
        };

        bean.connect(getContext(), beanListener);
        connectionLatch.await();
        assertThat(testState.get("connected")).isEqualTo(true);

        final CountDownLatch deviceInfoLatch = new CountDownLatch(1);
        testState.put("device_info", null);
        bean.readDeviceInfo(new Callback<DeviceInfo>() {
            @Override
            public void onResult(DeviceInfo deviceInfo) {
                testState.put("device_info", deviceInfo);
                deviceInfoLatch.countDown();
            }
        });
        deviceInfoLatch.await();
        assertThat(testState.get("device_info")).isNotNull();

        if (bean.isConnected()) {
            bean.disconnect();
        }
    }

    public void testBeanReadWriteScratchBank() throws InterruptedException {
        /** Test Scratch characteristic functionality
         *
         * Warning: This test requires a nearby bean named "TESTBEAN"
         */
        Bean bean = this.getBeanByName("TESTBEAN");
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        final HashMap testState = new HashMap();
        testState.put("connected", false);

        BeanListener beanListener = new BeanListener() {
            @Override
            public void onConnected() {
                testState.put("connected", true);
                connectionLatch.countDown();

            }

            @Override
            public void onConnectionFailed() {
                fail("Connection failed!");
            }

            @Override
            public void onDisconnected() {
            }

            @Override
{'fraction_non_alphanumeric': 0.09676100114510061, 'fraction_numerical': 0.0017994438082774415, 'mean_word_length': 2.0938765182186234, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1115184', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3051, 'n_words': 721}