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

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

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

import java.util.HashSet;
import java.util.Set;

import kr.co.zumo.app.lifeplus.activity.ActivityBase;
import kr.co.zumo.app.lifeplus.activity.MainActivity;

import static junit.framework.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
public class BaseSharedPreferencesTest {

  SharedPreferences preferences;
  BaseSharedPreferences baseSharedPreferences;

  @Before
  public void setUp() throws Exception {
    ActivityBase activity = Robolectric.setupActivity(MainActivity.class);
    preferences = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
    baseSharedPreferences = new BaseSharedPreferences(preferences);
  }

  @Test
  public void put() {
    baseSharedPreferences.put("key", "value");

    assertEquals("value", baseSharedPreferences.get("key", ""));
  }

  @Test
  public void put1() {
    baseSharedPreferences.put("key", true);

    assertEquals(true, baseSharedPreferences.get("key", false));
  }

  @Test
  public void put2() {
    baseSharedPreferences.put("key", Integer.MAX_VALUE);

    assertEquals(Integer.MAX_VALUE, baseSharedPreferences.get("key", 0));
  }

  @Test
  public void put3() {
    baseSharedPreferences.put("key", Long.MAX_VALUE);

    assertEquals(Long.MAX_VALUE, baseSharedPreferences.get("key", 0L));
  }

  @Test
  public void put4() {
    baseSharedPreferences.put("key", Float.MAX_VALUE);

    assertEquals(Float.MAX_VALUE, baseSharedPreferences.get("key", 0F));
  }

  @Test
  public void put5() {
    Set<String> stringSet = new HashSet<>();
    stringSet.add("A");
    stringSet.add("B");
    stringSet.add("C");
    baseSharedPreferences.put("key", stringSet);

    Set<String> stringSetDefault = new HashSet<>();
    assertEquals(stringSet.toString(), baseSharedPreferences.get("key", stringSetDefault).toString());
  }

  @Test
  public void remove() {
    baseSharedPreferences.put("key", 111);

    assertEquals(111, baseSharedPreferences.get("key", 0));

    baseSharedPreferences.remove("key");

    assertEquals(0, baseSharedPreferences.get("key", 0));
  }
}