package com.tale.prettysharedpreferences;

import android.content.SharedPreferences;

import junit.framework.TestCase;

import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by TALE on 9/11/2014.
 */
public class TypeEditorTest extends TestCase {
    private final String STRING_KEY = "string";
    @Mock
    SharedPreferences mockSharedPref;
    @Mock
    SharedPreferences.Editor mockEditor;

    private TypeEditor autoCommitTypeEditor;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        when(mockSharedPref.edit()).thenReturn(mockEditor);
        autoCommitTypeEditor = new TypeEditor(new PrettySharedPreferences(mockSharedPref) {
        }, mockSharedPref, STRING_KEY) {
            @Override
            protected void putValue(SharedPreferences.Editor editor, String key, Object value) {

            }

            @Override
            protected Object getValue(SharedPreferences sharedPreferences, String key, Object def) {
                return null;
            }
        };
    }

    @Test
    public void testPutAutocommit() {
        String mockValue = "Giang";
        autoCommitTypeEditor.put(mockValue);
        verify(mockEditor, never()).apply(); // Make sure apply() is not called if not apply.
        autoCommitTypeEditor.put(mockValue).apply();
        verify(mockEditor).apply(); // Make sure apply() is called to commit data.
    }

    @Test
    public void testRemoveAutocommit() {
        autoCommitTypeEditor.remove();
        verify(mockEditor, never()).apply(); // Make sure apply() is not called if not apply.
        autoCommitTypeEditor.remove().apply();
        verify(mockEditor).apply(); // Make sure apply() is called to commit data.
    }

}

@@ -494,16 +494,17 @@
 rences m
+M
 ockShare
@@ -550,16 +550,17 @@
 Editor m
+M
 ockEdito
@@ -586,26 +586,17 @@
 eEditor 
-autoCommit
+m
 TypeEdit
@@ -741,16 +741,17 @@
   when(m
+M
 ockShare
@@ -776,16 +776,17 @@
 Return(m
+M
 ockEdito
@@ -793,34 +793,25 @@
 r);%0A        
-autoCommit
+m
 TypeEditor =
@@ -855,16 +855,17 @@
 rences(m
+M
 ockShare
@@ -885,16 +885,17 @@
     %7D, m
+M
 ockShare
@@ -1268,25 +1268,20 @@
 testPutA
-utocommit
+pply
 () %7B%0A   
@@ -1317,34 +1317,25 @@
 g%22;%0A        
-autoCommit
+m
 TypeEditor.p
@@ -1357,32 +1357,33 @@
         verify(m
+M
 ockEditor, never
@@ -1448,34 +1448,25 @@
 ly.%0A        
-autoCommit
+m
 TypeEditor.p
@@ -1496,32 +1496,33 @@
         verify(m
+M
 ockEditor).apply
@@ -1613,26 +1613,234 @@
 test
-RemoveAuto
+PutCommit() %7B%0A        String mockValue = %22Giang%22;%0A        mTypeEditor.put(mockValue);%0A        verify(mMockEditor, never()).commit(); // Make sure commit() is not called if not apply.%0A        mTypeEditor.put(mockValue).
 commit()
  %7B%0A 
@@ -1831,26 +1831,25 @@
 ue).commit()
- %7B
+;
 %0A        aut
@@ -1841,34 +1841,164 @@
 ();%0A        
-autoCommit
+verify(mMockEditor).commit(); // Make sure commit() is called to commit data.%0A    %7D%0A%0A    @Test%0A    public void testRemoveApply() %7B%0A        m
 TypeEditor.r
@@ -2014,32 +2014,33 @@
         verify(m
+M
 ockEditor, never
@@ -2113,18 +2113,9 @@
     
-autoCommit
+m
 Type
@@ -2155,16 +2155,17 @@
 verify(m
+M
 ockEdito
@@ -2230,10 +2230,316 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testRemoveCommit() %7B%0A        mTypeEditor.remove();%0A        verify(mMockEditor, never()).commit(); // Make sure commit() is not called if not apply.%0A        mTypeEditor.remove().commit();%0A        verify(mMockEditor).commit(); // Make sure commit() is called to commit data.%0A    %7D%0A%0A
 %7D%0A

refactor -> method name, variable name. Add test commit method