package com.apple.dnssd;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Mockito.after;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({DNSSDEmbedded.class})
public class DNSSDEmbeddedTest {

    private static final int DEFAULT_TIMEOUT = 50;
    private static final int EXIT_TIMEOUT = 500;
    private static final int EXIT_TIMEOUT_TIMEOUT = 1000;

    DNSSDEmbedded mDNSSDEmbedded;

    @Before
    public void setup() {
        mDNSSDEmbedded = new DNSSDEmbedded(EXIT_TIMEOUT);
        mockStatic(DNSSDEmbedded.class);
    }

    @Test
    public void init() throws InterruptedException {
        mDNSSDEmbedded.init();
        PowerMockito.verifyStatic();
        DNSSDEmbedded.Init();
        PowerMockito.verifyStatic(timeout(DEFAULT_TIMEOUT));
        DNSSDEmbedded.Loop();
    }

    @Test
    public void initWithError() throws InterruptedException {
        PowerMockito.when(DNSSDEmbedded.Init()).thenReturn(-1);
        mDNSSDEmbedded.init();
        PowerMockito.verifyStatic();
        DNSSDEmbedded.Init();
        PowerMockito.verifyStatic(after(EXIT_TIMEOUT_TIMEOUT).never());
        DNSSDEmbedded.Loop();
    }

    @Test
    public void exit() throws InterruptedException {
        mDNSSDEmbedded.exit();
        PowerMockito.verifyStatic(timeout(EXIT_TIMEOUT + DEFAULT_TIMEOUT));
        DNSSDEmbedded.Exit();
    }

    @Test
    public void cancelStopTimer() throws InterruptedException {
        mDNSSDEmbedded.init();
        PowerMockito.verifyStatic();
        DNSSDEmbedded.Init();
        PowerMockito.verifyStatic(timeout(DEFAULT_TIMEOUT));
        DNSSDEmbedded.Loop();
        mDNSSDEmbedded.exit();
        Thread.sleep(EXIT_TIMEOUT/2);
        mDNSSDEmbedded.init();
        Thread.sleep(EXIT_TIMEOUT/2);
        PowerMockito.verifyStatic(never());
        DNSSDEmbedded.Exit();
        mDNSSDEmbedded.exit();
        PowerMockito.verifyStatic(timeout(EXIT_TIMEOUT_TIMEOUT));
        DNSSDEmbedded.Exit();
    }

    @After
    public void tearDown() {
    }
}

@@ -128,92 +128,8 @@
 th;%0A
-import org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;%0A
 impo
@@ -336,52 +336,8 @@
 er;%0A
-import static org.mockito.Mockito.doAnswer;%0A
 impo
@@ -420,49 +420,8 @@
 ut;%0A
-import static org.mockito.Mockito.times;%0A
 impo
@@ -616,24 +616,16 @@
 nal int 
-DEFAULT_
 TIMEOUT 
@@ -632,113 +632,8 @@
 = 50
-;%0A    private static final int EXIT_TIMEOUT = 500;%0A    private static final int EXIT_TIMEOUT_TIMEOUT = 10
 00;%0A
@@ -749,21 +749,16 @@
 mbedded(
-EXIT_
 TIMEOUT)
@@ -861,32 +861,33 @@
 uptedException %7B
+ 
 %0A        mDNSSDE
@@ -1007,32 +1007,24 @@
 tic(timeout(
-DEFAULT_
 TIMEOUT));%0A 
@@ -1333,29 +1333,16 @@
 c(after(
-EXIT_TIMEOUT_
 TIMEOUT)
@@ -1529,31 +1529,12 @@
 out(
-EXIT_TIMEOUT + DEFAULT_
+2 * 
 TIME
@@ -1795,16 +1795,8 @@
 out(
-DEFAULT_
 TIME
@@ -1880,37 +1880,34 @@
 hread.sleep(
-EXIT_
 TIMEOUT
-/
+ / 
 2);%0A        
@@ -1954,21 +1954,18 @@
 eep(
-EXIT_
 TIMEOUT
-/
+ / 
 2);%0A
@@ -2115,21 +2115,12 @@
 out(
-EXIT_TIMEOUT_
+2 * 
 TIME

fix DNSSDEmbeddedTest