// Path: sdk/src/com/sharethis/loopy/sdk/util/AppUtils.java
// public class AppUtils {
// 
//     static final AppUtils instance = new AppUtils();
// 
//     AppUtils() {}
// 
//     public static AppUtils getInstance() {
//         return instance;
//     }
// 
//     /**
//      * Determines inclusive permissions.
//      * @param context The current context.
//      * @param permissions The permissions sought.
//      * @return Returns true iff the app has ALL the given permissions.
//      */
//     public boolean hasAndPermission(Context context, String...permissions) {
//         for (String permission : permissions) {
//             int res = context.checkCallingOrSelfPermission(permission);
//             if(res != PackageManager.PERMISSION_GRANTED) {
//                 return false;
//             }
//         }
//         return true;
//     }
// 
//     /**
//      * Determines exclusive permissions.
//      * @param context The current context.
//      * @param permissions The permissions sought.
//      * @return Returns true iff the app has ANY the given permissions.
//      */
//     public boolean hasOrPermission(Context context, String...permissions) {
//         for (String permission : permissions) {
//             int res = context.checkCallingOrSelfPermission(permission);
//             if(res == PackageManager.PERMISSION_GRANTED) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     /**
//      * Returns the app info for the apps which support the ACTION_SEND intent with the given types.
//      * @param context The current context.
//      * @param contentTypes The content types supported.
//      * @return A Collection (set) of ResolveInfo objects corresponding to the apps installed on the device that
//      *          support the content type(s).
//      */
//     public Collection<ResolveInfo> getAppsForContentType(Context context, String...contentTypes) {
//         Set<ResolveInfo> apps = new LinkedHashSet<ResolveInfo>();
//         for (String type : contentTypes) {
//             List<ResolveInfo> appsByType = listAppsForType(context, type);
//             if(appsByType != null && appsByType.size() > 0) {
//                 apps.addAll(appsByType);
//             }
//         }
//         return apps;
//     }
// 
//     List<ResolveInfo> listAppsForType(Context context, String type) {
//         Intent intent = getSendIntent();
//         intent.setType(type);
// 
//         PackageManager p = context.getPackageManager();
// 
//         List<ResolveInfo> appList = p.queryIntentActivities(intent, 0);
// 
//         // Sort alphabetically.
//         Collections.sort(appList, new android.content.pm.ResolveInfo.DisplayNameComparator(p));
// 
//         return appList;
//     }
// 
//     // mockable
//     Intent getSendIntent() {
//         return new Intent(Intent.ACTION_SEND);
//     }
// 
// }
// 
// Path: test/src/com/sharethis/loopy/sdk/util/MockAppUtils.java
// public class MockAppUtils extends AppUtils {
//     public MockAppUtils() {
//         super();
//     }
// 
//     @Override
//     public List<ResolveInfo> listAppsForType(Context context, String type) {
//         return super.listAppsForType(context, type);
//     }
// 
//     @Override
//     public Intent getSendIntent() {
//         return super.getSendIntent();
//     }
// }


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.test.mock.MockContext;
import com.sharethis.loopy.sdk.util.AppUtils;
import com.sharethis.loopy.sdk.util.MockAppUtils;
import org.mockito.Mockito;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
package com.sharethis.loopy.test;



/**
 * @author Jason Polites
 */
public class AppUtilsTest extends LoopyAndroidTestCase {

    public void testHasAndPermission() {

        Context trueContext = Mockito.mock(MockContext.class);
        Context falseContext = Mockito.mock(MockContext.class);

        Mockito.when(trueContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_GRANTED);
        Mockito.when(falseContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

        assertTrue(AppUtils.getInstance().hasAndPermission(trueContext, "foo", "bar"));
        assertFalse(AppUtils.getInstance().hasAndPermission(falseContext, "foo", "bar"));
    }

    public void testHasOrPermission() {

        Context trueContext = Mockito.mock(MockContext.class);
        Context falseContext = Mockito.mock(MockContext.class);

        Mockito.when(trueContext.checkCallingOrSelfPermission("foo")).thenReturn(PackageManager.PERMISSION_GRANTED);
        Mockito.when(trueContext.checkCallingOrSelfPermission("bar")).thenReturn(PackageManager.PERMISSION_DENIED);

        Mockito.when(falseContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

        assertTrue(AppUtils.getInstance().hasOrPermission(trueContext, "foo", "bar"));
        assertFalse(AppUtils.getInstance().hasOrPermission(falseContext, "foo", "bar"));

    }

    public void testListAppsForType() {

        final Intent intent = new Intent();

        PackageManager pm = Mockito.mock(PackageManager.class);
        Context context = Mockito.mock(MockContext.class);

        ResolveInfo foo = Mockito.mock(ResolveInfo.class);
        ResolveInfo bar = Mockito.mock(ResolveInfo.class);

        List<ResolveInfo> infos = Arrays.asList(foo, bar);

        Mockito.when(foo.loadLabel(pm)).thenReturn("foo");
        Mockito.when(bar.loadLabel(pm)).thenReturn("bar");
        Mockito.when(context.getPackageManager()).thenReturn(pm);
        Mockito.when(pm.queryIntentActivities(intent, 0)).thenReturn(infos);

// Path: sdk/src/com/sharethis/loopy/sdk/util/AppUtils.java
// public class AppUtils {
// 
//     static final AppUtils instance = new AppUtils();
// 
//     AppUtils() {}
// 
//     public static AppUtils getInstance() {
//         return instance;
//     }
// 
//     /**
//      * Determines inclusive permissions.
//      * @param context The current context.
//      * @param permissions The permissions sought.
//      * @return Returns true iff the app has ALL the given permissions.
//      */
//     public boolean hasAndPermission(Context context, String...permissions) {
//         for (String permission : permissions) {
//             int res = context.checkCallingOrSelfPermission(permission);
//             if(res != PackageManager.PERMISSION_GRANTED) {
//                 return false;
//             }
//         }
//         return true;
//     }
// 
//     /**
//      * Determines exclusive permissions.
//      * @param context The current context.
//      * @param permissions The permissions sought.
//      * @return Returns true iff the app has ANY the given permissions.
//      */
//     public boolean hasOrPermission(Context context, String...permissions) {
//         for (String permission : permissions) {
//             int res = context.checkCallingOrSelfPermission(permission);
//             if(res == PackageManager.PERMISSION_GRANTED) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     /**
//      * Returns the app info for the apps which support the ACTION_SEND intent with the given types.
//      * @param context The current context.
//      * @param contentTypes The content types supported.
//      * @return A Collection (set) of ResolveInfo objects corresponding to the apps installed on the device that
//      *          support the content type(s).
//      */
//     public Collection<ResolveInfo> getAppsForContentType(Context context, String...contentTypes) {
//         Set<ResolveInfo> apps = new LinkedHashSet<ResolveInfo>();
//         for (String type : contentTypes) {
//             List<ResolveInfo> appsByType = listAppsForType(context, type);
//             if(appsByType != null && appsByType.size() > 0) {
//                 apps.addAll(appsByType);
//             }
//         }
//         return apps;
//     }
// 
//     List<ResolveInfo> listAppsForType(Context context, String type) {
//         Intent intent = getSendIntent();
//         intent.setType(type);
// 
//         PackageManager p = context.getPackageManager();
// 
//         List<ResolveInfo> appList = p.queryIntentActivities(intent, 0);
// 
//         // Sort alphabetically.
//         Collections.sort(appList, new android.content.pm.ResolveInfo.DisplayNameComparator(p));
// 
//         return appList;
//     }
// 
//     // mockable
//     Intent getSendIntent() {
//         return new Intent(Intent.ACTION_SEND);
//     }
// 
// }
// 
// Path: test/src/com/sharethis/loopy/sdk/util/MockAppUtils.java
// public class MockAppUtils extends AppUtils {
//     public MockAppUtils() {
//         super();
//     }
// 
//     @Override
//     public List<ResolveInfo> listAppsForType(Context context, String type) {
//         return super.listAppsForType(context, type);
//     }
// 
//     @Override
//     public Intent getSendIntent() {
//         return super.getSendIntent();
//     }
// }


// Path: test/src/com/sharethis/loopy/test/AppUtilsTest.java
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.test.mock.MockContext;
import com.sharethis.loopy.sdk.util.AppUtils;
import com.sharethis.loopy.sdk.util.MockAppUtils;
import org.mockito.Mockito;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

package com.sharethis.loopy.test;



/**
 * @author Jason Polites
 */
public class AppUtilsTest extends LoopyAndroidTestCase {

    public void testHasAndPermission() {

        Context trueContext = Mockito.mock(MockContext.class);
        Context falseContext = Mockito.mock(MockContext.class);

        Mockito.when(trueContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_GRANTED);
        Mockito.when(falseContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

        assertTrue(AppUtils.getInstance().hasAndPermission(trueContext, "foo", "bar"));
        assertFalse(AppUtils.getInstance().hasAndPermission(falseContext, "foo", "bar"));
    }

    public void testHasOrPermission() {

        Context trueContext = Mockito.mock(MockContext.class);
        Context falseContext = Mockito.mock(MockContext.class);

        Mockito.when(trueContext.checkCallingOrSelfPermission("foo")).thenReturn(PackageManager.PERMISSION_GRANTED);
        Mockito.when(trueContext.checkCallingOrSelfPermission("bar")).thenReturn(PackageManager.PERMISSION_DENIED);

        Mockito.when(falseContext.checkCallingOrSelfPermission(Mockito.anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

        assertTrue(AppUtils.getInstance().hasOrPermission(trueContext, "foo", "bar"));
        assertFalse(AppUtils.getInstance().hasOrPermission(falseContext, "foo", "bar"));

    }

    public void testListAppsForType() {

        final Intent intent = new Intent();

        PackageManager pm = Mockito.mock(PackageManager.class);
        Context context = Mockito.mock(MockContext.class);

        ResolveInfo foo = Mockito.mock(ResolveInfo.class);
        ResolveInfo bar = Mockito.mock(ResolveInfo.class);

        List<ResolveInfo> infos = Arrays.asList(foo, bar);

        Mockito.when(foo.loadLabel(pm)).thenReturn("foo");
        Mockito.when(bar.loadLabel(pm)).thenReturn("bar");
        Mockito.when(context.getPackageManager()).thenReturn(pm);
        Mockito.when(pm.queryIntentActivities(intent, 0)).thenReturn(infos);

        MockAppUtils appUtils = new MockAppUtils() {