package com.aragaer.yama;

import java.io.*;
import java.util.*;

import android.content.Context;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.*;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.*;
import static android.support.test.espresso.matcher.ViewMatchers.*;

import static org.hamcrest.CoreMatchers.equalTo;


@RunWith(AndroidJUnit4.class)
@LargeTest
public class CreateActivityTest {

    @Rule public ActivityTestRule<MemoCreateActivity> mActivityRule =
	    new ActivityTestRule<MemoCreateActivity>(MemoCreateActivity.class);

    private List<String> readMemosFile() throws Exception {
	List<String> result = new LinkedList<String>();
	InputStream file = mActivityRule.getActivity().openFileInput("memo");
	BufferedReader reader = new BufferedReader(new InputStreamReader(file));
	while (true) {
	    String line = reader.readLine();
	    if (line == null)
		break;
	    result.add(line);
	}
	file.close();
	return result;
    }

    private void writeMemosFile(List<String> lines) throws Exception {
	OutputStream file = mActivityRule.getActivity().openFileOutput("memo",
	       Context.MODE_PRIVATE | Context.MODE_APPEND);
	for (String line : lines) {
	    file.write(line.getBytes());
	    file.write("\n".getBytes());
	}
	file.close();
    }

    @After public void tearDown() {
	mActivityRule.getActivity().deleteFile("memo");
    }

    @Test public void shouldWriteOneLineMemosToFile() throws Exception {
	onView(supportsInputMethods()).perform(typeText("Have a cup of Espresso."));
	onView(withText("Done")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Have a cup of Espresso.")));
    }

    @Test public void shouldWriteMultiLineMemosToFile() throws Exception {
	onView(supportsInputMethods())
	    .perform(typeText("Have a cup of Espresso.\n"
			      +"Write a second test."));
	onView(withText("Done")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Have a cup of Espresso.",
					 "Write a second test.")));
    }

    @Test public void shouldDiscardEmptyLines() throws Exception {
	onView(supportsInputMethods())
	    .perform(typeText("\n"
			      +"Have a cup of Espresso.\n"
			      +"\n"
			      +"  \n"
			      +"\n"
			      +"Write a second test.\n"
			      +"\n"));
	onView(withText("Done")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Have a cup of Espresso.",
					 "Write a second test.")));
    }

    @Test public void shouldTrimStrings() throws Exception {
	onView(supportsInputMethods())
	    .perform(typeText("  Have a cup of Espresso.   \n"
			      +"\n"
			      +"  \n"
			      +"  Write a second test.\n"
			      +"\n"));
	onView(withText("Done")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Have a cup of Espresso.",
					 "Write a second test.")));
    }

    @Test public void shouldAppendToExistingMemos() throws Exception {
	writeMemosFile(Arrays.asList("Some initial memo", "Two of them"));

	onView(supportsInputMethods()).perform(typeText("Have a cup of Espresso."));
	onView(withText("Done")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Some initial memo",
					 "Two of them",
					 "Have a cup of Espresso.")));
    }

    @Test public void shouldDiscardIfNotSaved() throws Exception {
	writeMemosFile(Arrays.asList("Some initial memo", "Two of them"));

	onView(supportsInputMethods()).perform(typeText("Have a cup of Espresso."));
	onView(withText("Cancel")).perform(click());

	assertThat(readMemosFile(),
		   equalTo(Arrays.asList("Some initial memo",
					 "Two of them")));
    }
}

@@ -1615,20 +1615,21 @@
 ould
-WriteOneLine
+AppendTrimmed
 Memo
@@ -1662,651 +1662,75 @@
  %7B%0A%09
-onView(supportsInputMethods()).perform(typeText(%22Have a cup of Espresso.%22));%0A%09onView(withText(%22Done%22)).perform(click());%0A%0A%09assertThat(readMemosFile(),%0A%09%09   equalTo(Arrays.asList(%22Have a cup of Espresso.%22)));%0A    %7D%0A%0A    @Test public void shouldWriteMultiLineMemosToFile() throws Exception %7B%0A%09onView(supportsInputMethods())%0A%09    .perform(typeText(%22Have a cup of Espresso.%5Cn%22%0A%09%09%09      +%22Write a second test.%22));%0A%09onView(withText(%22Done%22)).perform(click());%0A%0A%09assertThat(readMemosFile(),%0A%09%09   equalTo(Arrays.asList(%22Have a cup of Espresso.%22,%0A%09%09%09%09%09 %22Write a second test.%22)));%0A    %7D%0A%0A    @Test public void shouldDiscardEmptyLines() throws Exception %7B
+writeMemosFile(Arrays.asList(%22Some initial memo%22, %22Two of them%22));%0A
 %0A%09on
@@ -1797,16 +1797,18 @@
       +%22
+  
 Have a c
@@ -1822,16 +1822,18 @@
 spresso.
+  
 %5Cn%22%0A%09%09%09 
@@ -1898,32 +1898,34 @@
 e a second test.
+  
 %5Cn%22%0A%09%09%09      +%22%5C
@@ -1979,798 +1979,8 @@
 );%0A%0A
-%09assertThat(readMemosFile(),%0A%09%09   equalTo(Arrays.asList(%22Have a cup of Espresso.%22,%0A%09%09%09%09%09 %22Write a second test.%22)));%0A    %7D%0A%0A    @Test public void shouldTrimStrings() throws Exception %7B%0A%09onView(supportsInputMethods())%0A%09    .perform(typeText(%22  Have a cup of Espresso.   %5Cn%22%0A%09%09%09      +%22%5Cn%22%0A%09%09%09      +%22  %5Cn%22%0A%09%09%09      +%22  Write a second test.%5Cn%22%0A%09%09%09      +%22%5Cn%22));%0A%09onView(withText(%22Done%22)).perform(click());%0A%0A%09assertThat(readMemosFile(),%0A%09%09   equalTo(Arrays.asList(%22Have a cup of Espresso.%22,%0A%09%09%09%09%09 %22Write a second test.%22)));%0A    %7D%0A%0A    @Test public void shouldAppendToExistingMemos() throws Exception %7B%0A%09writeMemosFile(Arrays.asList(%22Some initial memo%22, %22Two of them%22));%0A%0A%09onView(supportsInputMethods()).perform(typeText(%22Have a cup of Espresso.%22));%0A%09onView(withText(%22Done%22)).perform(click());%0A%0A
 %09ass
@@ -2104,16 +2104,46 @@
 presso.%22
+,%0A%09%09%09%09%09 %22Write a second test.%22
 )));%0A   

Consolidate some tests