package ca.six.tomato.util;

import org.json.JSONArray;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowEnvironment;

import java.io.File;

import ca.six.tomato.BuildConfig;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

/**
 * Created by songzhw on 2016-10-03
 */
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class DataUtilsTest {

    @Test
    public void testFile() {
        File dstFile = ShadowEnvironment.getExternalStorageDirectory();
        assertTrue(dstFile.exists());

        System.out.println("szw isDirectory = " + dstFile.isDirectory()); //=> true
        System.out.println("szw isExisting = " + dstFile.exists()); //=> true
        System.out.println("szw path = " + dstFile.getPath()); //=> C:\Users\***\AppData\Local\Temp\android-tmp-robolectric7195966122073188215
    }

    @Test
    public void testWriteString(){
        File dstFile = ShadowEnvironment.getExternalStorageDirectory();
        String path = dstFile.getPath() +"/tmp";
        DataUtilKt.write("abc", path);

        String ret = DataUtilKt.read(path, false);
        assertEquals("abc", ret);
    }

    @Test
    public void testWriteJSON(){
        File dstFile = ShadowEnvironment.getExternalStorageDirectory();
        String path = dstFile.getPath() +"/tmp";
        String content = " {\"clock\": \"clock1\", \"work\": 40, \"short\": 5, \"long\": 15}\n" +
                " {\"clock\": \"clock2\", \"work\": 30, \"short\": 5, \"long\": 15}\n" +
                " {\"clock\": \"clock3\", \"work\": 45, \"short\": 5, \"long\": 15}";
        DataUtilKt.write(content, path);

        JSONArray ret = DataUtilKt.read(path);
        System.out.println("szw, ret = "+ret.toString());
        assertNotNull(ret);
    }
}
