/*
 * Android Scan, an app for scanning and configuring HBM devices.
 *
 * The MIT License (MIT)
 *
 * Copyright (C) Stephan Gatzka
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.hbm.devices.scan.ui.android;

import com.hbm.devices.scan.announce.Announce;
import com.hbm.devices.scan.announce.AnnounceDeserializer;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

public class DisplayUpdateEventGeneratorTest  implements DisplayNotifier, Observer {

    private final List<Announce> oldList = new ArrayList<>();
    private final List<Announce> newList = new ArrayList<>();
    private List<Announce> oldListClone;

    private boolean fillNewList;

    private static final String DEVICE1;
    private static final String DEVICE1UPDATE;
    private static final String DEVICE2;
    private static final String DEVICE3;
    private static final String DEVICE4;
    private static final String DEVICE5;
    private static final String DEVICE6;

    @Test
    public void testCompareLists() throws Exception {
        final AnnounceDeserializer parser = new AnnounceDeserializer();
        parser.addObserver(this);
        parser.update(null, DEVICE1);
        parser.update(null, DEVICE2);
        parser.update(null, DEVICE3);
        parser.update(null, DEVICE4);
        parser.update(null, DEVICE5);

        fillNewList = true;
        parser.update(null, DEVICE1UPDATE);
        parser.update(null, DEVICE2);
        parser.update(null, DEVICE6);
        parser.update(null, DEVICE5);
        parser.update(null, DEVICE4);

        assertEquals(oldList.size(), 5);
        assertEquals(newList.size(), 5);
        oldListClone = new ArrayList<>(oldList.size());
        for (final Announce item: oldList) {
            oldListClone.add(item);
        }
        assertEquals("Cloned old list not equals to oldList", oldList, oldListClone);
        final DisplayUpdateEventGenerator eventGenerator = new DisplayUpdateEventGenerator(this);
        eventGenerator.compareLists(oldList, newList);
        assertEquals("oldList not the same as newList after compareList", oldList, newList);
        assertEquals("updated events list not the same as newList after compareList", oldListClone, newList);
    }

    @Override
    public void notifyRemoveAt(int position) {
        oldListClone.remove(position);
    }

    @Override
    public void notifyMoved(int fromPosition, int toPosition) {
        final Announce announce = oldListClone.remove(fromPosition);
        oldListClone.add(toPosition, announce);
    }

    @Override
    public void notifyChangeAt(int position) {
        oldListClone.set(position, newList.get(position));
    }

    @Override
    public void notifyAddAt(int position) {
        oldListClone.add(newList.get(position));
    }

    @Override
    public void update(Observable o, Object arg) {
        final Announce announce = (Announce)arg;
        if (fillNewList) {
            newList.add(announce);
        } else {
            oldList.add(announce);
        }
    }

    static {
        try (final InputStream is = FakeMessageReceiver.class.getResourceAsStream("/devices.properties")) {
            final Properties props = new Properties();
            props.load(is);
            DEVICE1 = props.getProperty("scan.announce.device1");
            DEVICE1UPDATE = props.getProperty("scan.announce.device1update");
            DEVICE2 = props.getProperty("scan.announce.device2");
            DEVICE3 = props.getProperty("scan.announce.device3");
            DEVICE4 = props.getProperty("scan.announce.device4");
            DEVICE5 = props.getProperty("scan.announce.device5");
            DEVICE6 = props.getProperty("scan.announce.device6");
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
}

@@ -1623,16 +1623,82 @@
 quals;%0A%0A
+/**%0A * Unit test for testing the DisplayUpdateEventGenerator.%0A */%0A
 public c
@@ -2277,67 +2277,510 @@
     
-@Test%0A    public void testCompareLists() throws Exception %7B
+/**%0A     * Default constructor. This is called by the junit environment.%0A     */%0A    public DisplayUpdateEventGeneratorTest() %7B%0A        super();%0A    %7D%0A%0A    /**%0A     * Test case to ensure that a list is correctly updated by the events fired from the DisplayUpdateEventGenerator.%0A     */%0A    @Test%0A    public void testCompareLists()  %7B%0A        final String%5B%5D oldArray = %7BDEVICE1, DEVICE2, DEVICE3, DEVICE4, DEVICE5%7D;%0A        final String%5B%5D newArray = %7BDEVICE1UPDATE, DEVICE2, DEVICE6, DEVICE5, DEVICE4%7D;%0A
 %0A   
@@ -2882,24 +2882,25 @@
 (this);%0A
+%0A
         
 parser.u
@@ -2895,114 +2895,49 @@
     
-parser.update(null, DEVICE1);%0A        parser.update(null, DEVICE2);%0A        parser.update(null, DEVICE3);%0A
+for (final String elem : oldArray) %7B%0A    
     
@@ -2960,23 +2960,20 @@
 e(null, 
-DEVICE4
+elem
 );%0A     
@@ -2979,38 +2979,9 @@
     
-parser.update(null, DEVICE5);%0A
+%7D
 %0A   
@@ -3017,128 +3017,57 @@
     
-parser.update(null, DEVICE1UPDATE);%0A        parser.update(null, DEVICE2);%0A        parser.update(null, DEVICE6);%0A
+for (final String elem : newArray) %7B%0A      
       
-  
 pars
@@ -3082,23 +3082,20 @@
 e(null, 
-DEVICE5
+elem
 );%0A     
@@ -3101,37 +3101,9 @@
     
-parser.update(null, DEVICE4);
+%7D
 %0A%0A  
@@ -3129,33 +3129,47 @@
 oldList.size(), 
-5
+oldArray.length
 );%0A        asser
@@ -3192,17 +3192,31 @@
 size(), 
-5
+newArray.length
 );%0A     

Document and cleanup unit test.