package com.robomorphine.prefs.remote;

import com.robomorphine.prefs.InternalPreferenceManager;
import com.robomorphine.prefs.PreferencesRemover;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri.Builder;
import android.test.ProviderTestCase2;

import java.io.File;

public class RemotePrefsProviderTest extends ProviderTestCase2<RemotePrefsProvider>{
    
    private static String AUTHORITY = "com.robomorphine.prefs.remote";
    
    public RemotePrefsProviderTest() {
        super(RemotePrefsProvider.class, AUTHORITY);
    }
    
    private static class MyContextWrapper extends ContextWrapper {
        private final String mPrefix;
        
        public MyContextWrapper(Context context, String prefix) {
            super(context);
            mPrefix = prefix; 
        }
        
        @Override
        public File getFilesDir() {
            return new File(super.getFilesDir(), mPrefix);
        }
    }
    
    private Context mContext;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();       
        mContext = new MyContextWrapper(getContext(), "file");
        PreferencesRemover remover = new PreferencesRemover(getContext());
        remover.clear();
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }
    
    public void testQuery() {
        Uri.Builder builder = new Builder();
        builder.scheme("content");
        builder.authority(AUTHORITY);
        
        mContext.getSharedPreferences("test1", Context.MODE_PRIVATE).edit().commit();
        mContext.getSharedPreferences("test2", Context.MODE_PRIVATE).edit().commit();
        
        Cursor cursor = getContext().getContentResolver().query(builder.build(), null, null, null, null);
        int nameIndex = cursor.getColumnIndex(RemotePrefsContract.SharedPrefs.NAME);
        if(cursor.moveToFirst()) {
            String value = cursor.getString(nameIndex);
            assertNotNull(value);
        }
        cursor.close();
    }
    
    public void testPrefManager() throws Exception {
        InternalPreferenceManager manager = new InternalPreferenceManager(getContext());
        manager.startTracking();        
        mContext.getSharedPreferences("test1", Context.MODE_PRIVATE).edit().commit();
        
        Thread.sleep(1000);
        manager.stopTracking();
        
    }
}

@@ -208,24 +208,66 @@
 extWrapper;%0A
+import android.content.SharedPreferences;%0A
 import andro
@@ -402,16 +402,74 @@
 io.File;
+%0Aimport java.lang.reflect.Field;%0Aimport java.util.HashMap;
 %0A%0Apublic
@@ -2574,10 +2574,532 @@
  %0A    %7D%0A
+    %0A    public void testStaticPrivate() throws Exception %7B%0A        Class%3C?%3E clz = Class.forName(%22android.app.ContextImpl%22);%0A        %0A        Field field = clz.getDeclaredField(%22sSharedPrefs%22);%0A        field.setAccessible(true);%0A        HashMap%3CString, SharedPreferences%3E prefs = new HashMap%3CString, SharedPreferences%3E();%0A        field.set(null, prefs);%0A        %0A        assertSame(prefs, field.get(null));%0A        getContext().getSharedPreferences(%22test%22, 0).edit().commit();%0A        assertEquals(1, prefs.size());%0A    %7D%0A
 %7D%0A

Add experimental way of trackable HashMap.