package net.grandcentrix.tray.provider;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.mock.MockContext;
import android.test.mock.MockPackageManager;

import org.mockito.Mockito;

import java.util.Collections;
import java.util.List;

/**
 * Created by pascalwelsch on 4/12/15.
 */
public class TrayContractTest extends AndroidTestCase {

    public void testConstruction() throws Exception {
        new TrayContract();
    }

    public void testGenerateContentUri() throws Exception {
        Uri uri = TrayContract.generateContentUri(getContext());
        assertEquals("content://com.example.preferences/preferences", uri.toString());

        TrayContract.setAuthority("asdf");
        uri = TrayContract.generateContentUri(Mockito.mock(Context.class));
        assertEquals("content://asdf/preferences", uri.toString());
    }

    public void testGenerateInternalContentUri() throws Exception {
        Uri uri = TrayContract.generateInternalContentUri(getContext());
        assertEquals("content://com.example.preferences/internal_preferences", uri.toString());

        TrayContract.setAuthority("blubb");
        uri = TrayContract.generateInternalContentUri(Mockito.mock(Context.class));
        assertEquals("content://blubb/internal_preferences", uri.toString());
    }

    public void testGenerateContentUri2() throws Exception {
        MockContext mockContext = new MockContext() {
            @Override
            public Context getApplicationContext() {
                return this;
            }

            @Override
            public String getPackageName() {
                return getContext().getPackageName();
            }

            @Override
            public PackageManager getPackageManager() {
                return new MockPackageManager() {
                    @Override
                    public List<PackageInfo> getInstalledPackages(int flags) {
                        if (flags == PackageManager.GET_PROVIDERS) {
                            PackageInfo packageInfo = new PackageInfo();
                            ProviderInfo providerInfo = new ProviderInfo();
                            providerInfo.authority = getContext().getPackageName() + ".tray";
                            packageInfo.providers = new ProviderInfo[]{providerInfo};
                            return Collections.singletonList(packageInfo);
                        }
                        return super.getInstalledPackages(flags);
                    }
                };
            }
        };
        Uri uri = TrayContract.generateContentUri(mockContext);
        assertEquals("content://" + getContext().getPackageName() + ".tray/preferences", uri.toString());
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        TrayContract.setAuthority(null);
    }


}
@@ -761,35 +761,45 @@
 (%22content://
-com.example
+net.grandcentrix.tray
 .preferences
@@ -798,16 +798,21 @@
 ferences
+.test
 /prefere
@@ -1207,19 +1207,29 @@
 t://
-com.example
+net.grandcentrix.tray
 .pre
@@ -1236,16 +1236,21 @@
 ferences
+.test
 /interna
@@ -1403,32 +1403,32 @@
 ontext.class));%0A
+
         assertEq
@@ -1500,1423 +1500,8 @@
  %7D%0A%0A
-    public void testGenerateContentUri2() throws Exception %7B%0A        MockContext mockContext = new MockContext() %7B%0A            @Override%0A            public Context getApplicationContext() %7B%0A                return this;%0A            %7D%0A%0A            @Override%0A            public String getPackageName() %7B%0A                return getContext().getPackageName();%0A            %7D%0A%0A            @Override%0A            public PackageManager getPackageManager() %7B%0A                return new MockPackageManager() %7B%0A                    @Override%0A                    public List%3CPackageInfo%3E getInstalledPackages(int flags) %7B%0A                        if (flags == PackageManager.GET_PROVIDERS) %7B%0A                            PackageInfo packageInfo = new PackageInfo();%0A                            ProviderInfo providerInfo = new ProviderInfo();%0A                            providerInfo.authority = getContext().getPackageName() + %22.tray%22;%0A                            packageInfo.providers = new ProviderInfo%5B%5D%7BproviderInfo%7D;%0A                            return Collections.singletonList(packageInfo);%0A                        %7D%0A                        return super.getInstalledPackages(flags);%0A                    %7D%0A                %7D;%0A            %7D%0A        %7D;%0A        Uri uri = TrayContract.generateContentUri(mockContext);%0A        assertEquals(%22content://%22 + getContext().getPackageName() + %22.tray/preferences%22, uri.toString());%0A    %7D%0A%0A
     

Fix tests