package kr.co.zumo.app.lifeplus.network;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * NetworkReceiverTest
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018-09-26]   [최초 작성]
 * @since 2018-09-26
 */

@RunWith(RobolectricTestRunner.class)
public class NetworkReceiverTest {

  @Test
  public void onReceive() {
    Context context = mock(Context.class);
    INetworkReceiverListener listener = mock(INetworkReceiverListener.class);
    NetworkReceiver receiver = new NetworkReceiver(context, listener);

    verify(context).registerReceiver(eq(receiver), any(IntentFilter.class));

    Intent intent = new Intent();
    intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION);

    assertEquals(ConnectivityManager.CONNECTIVITY_ACTION, intent.getAction());

    receiver.onReceive(context, intent);

    verify(listener).onChangedConnection(eq(false), eq(NetworkStatus.DISCONNECTED));

  }

  @Test
  public void dispose() {
    Context context = mock(Context.class);
    INetworkReceiverListener listener = mock(INetworkReceiverListener.class);
    NetworkReceiver receiver = new NetworkReceiver(context, listener);
    receiver.dispose();

    verify(context).unregisterReceiver(eq(receiver));

  }
}