/*
 * Copyright (C) 2017 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 */
package org.openecard.gui.android.eac;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import mockit.Mocked;
import mockit.Tested;
import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

/**
 *
 * @author Neil Crossley
 */
public class EacGuiServiceTest {
    
    @Mocked
    EacGui.Stub stub;
    
    @Mocked
    Service service;
    
    @BeforeTest
    public void setUpSuite() {
	EacGuiService.prepare();
    }
    
    @AfterMethod
    public void tearDown() {
	EacGuiService.prepare();
    }
    
    @Test
    public void canDeliverCorrectGui(@Mocked EacGuiImpl input) {
	EacGuiService.setGuiImpl(input);
    }
    
    @Test(expectedExceptions = {IllegalStateException.class})
    public void canDeliverGuiMultipleTimes(@Mocked EacGuiImpl input1, @Mocked EacGuiImpl input2) throws IllegalStateException {
	EacGuiService.setGuiImpl(input1);
	EacGuiService.setGuiImpl(input2);
    }
    
    @Test
    public void GivenNoGuiImplSetThenBindingWaitsForever(@Tested final EacGuiService sut, @Mocked final Intent input)  {
	Future<IBinder> future = callBindAsync(sut, input);
	try {
	    //wait 1 seconds for the task to complete.
	    future.get(1000, TimeUnit.MILLISECONDS);
	    
	    Assert.fail("The call to onBind(...) is supposed to wait forever and not terminate.");
	}
	catch (InterruptedException | ExecutionException | TimeoutException e) {
	    // Pass
	}
    }

    @Test
    public void BindingWaitsForGuiAssignment(@Tested final EacGuiService sut, @Mocked final Intent inputIntent, @Mocked EacGuiImpl inputGui) throws InterruptedException, ExecutionException  {
	
	Future<IBinder> future = callBindAsync(sut, inputIntent);
	TimeUnit.MILLISECONDS.sleep(2);
	EacGuiService.setGuiImpl(inputGui);
	IBinder result = future.get();
	
	assertEquals(result, inputGui);
    }

    private Future<IBinder> callBindAsync(final EacGuiService sut, final Intent input) {
	ExecutorService exec = Executors.newSingleThreadExecutor();
	Future<IBinder> future = exec.submit(new Callable<IBinder>(){
	    @Override
	    public IBinder call() throws Exception {
		return sut.onBind(input);
	    }
	});
	exec.shutdown();
	return future;
    }
}

@@ -1467,20 +1467,16 @@
 eTest %7B%0A
-    
 %0A    @Mo
@@ -1502,20 +1502,16 @@
 b stub;%0A
-    
 %0A    @Mo
@@ -1536,20 +1536,16 @@
 ervice;%0A
-    
 %0A    @Be
@@ -1612,28 +1612,24 @@
 re();%0A    %7D%0A
-    
 %0A    @AfterM
@@ -1687,36 +1687,32 @@
 repare();%0A    %7D%0A
-    
 %0A    @Test%0A    p
@@ -1729,25 +1729,29 @@
  can
-DeliverCorrectGui
+AssignGuiSuccessfully
 (@Mo
@@ -1807,36 +1807,32 @@
 l(input);%0A    %7D%0A
-    
 %0A    @Test(expec
@@ -1897,16 +1897,19 @@
 void can
+not
 DeliverG
@@ -2085,20 +2085,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te
@@ -2116,25 +2116,29 @@
 oid 
-G
+g
 iven
-No
 GuiI
-mplSet
+sNotAssigned
 Then
@@ -2217,17 +2217,16 @@
 t input)
- 
  %7B%0A%09Futu
@@ -2374,21 +2374,16 @@
 CONDS);%0A
-%09    
 %0A%09    As
@@ -2469,18 +2469,17 @@
 e.%22);%0A%09%7D
-%0A%09
+ 
 catch (I
@@ -2592,16 +2592,57 @@
 ic void 
+givenBindingStartsBeforeGuiAssignmentThen
 BindingW
@@ -2686,32 +2686,37 @@
 cGuiService sut,
+%0A%09   
  @Mocked final I
@@ -2733,16 +2733,21 @@
 tIntent,
+%0A%09   
  @Mocked
@@ -2816,20 +2816,18 @@
 ception 
- 
 %7B%0A
-%09
 %0A%09Future
@@ -2980,17 +2980,16 @@
 .get();%0A
-%09
 %0A%09assert
@@ -3232,16 +3232,17 @@
 inder%3E()
+ 
 %7B%0A%09    @

Rename tests for clarity