package com.google.android.apps.paco;

import org.junit.Test;

import android.content.Intent;
import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.PowerManager;

import android.test.AndroidTestCase;
import android.test.mock.MockContext;

import java.lang.reflect.Method;

import com.pacoapp.paco.sensors.android.BroadcastTriggerReceiver;

/**
 * TODO: this test class needs to be extended to cover other parts of the
 * BroadcastTriggerReceiver, notably the onReceive() call. This would however
 * require some refactoring of that class, as e.g. the PowerManager is not
 * mockable in its current state.
 */
public class BroadcastTriggerReceiverTest extends AndroidTestCase {
  class FakeContext extends MockContext {
    private Intent startedService;
    
    @Override
    public String getPackageName() {
      return "com.pacoapp.paco";
    }
    @Override
    public ComponentName startService(Intent intent) {
      startedService = intent;
      return null;
    }
    public Intent getStartedService() {
      return startedService;
    }
  }

  @Test
  public void testAppInstall() throws Exception {
    Context context = new FakeContext();
    Intent installIntent = new Intent(Intent.ACTION_PACKAGE_ADDED);
    Uri.Builder uriBuilder = new Uri.Builder();
    installIntent.setData(uriBuilder.build());
    BroadcastTriggerReceiver broadcastTriggerReceiver = new BroadcastTriggerReceiver();
    Method isPackageAdded = broadcastTriggerReceiver.getClass().getDeclaredMethod("isPackageAdded", Context.class, Intent.class);
    isPackageAdded.setAccessible(true);
    assertEquals(isPackageAdded.invoke(broadcastTriggerReceiver, context, installIntent), true);
  }
  
  @Test
  public void testAppUpdate() throws Exception {
    Context context = new FakeContext();
    Intent updateIntent = new Intent(Intent.ACTION_PACKAGE_ADDED);
    Uri.Builder uriBuilder = new Uri.Builder();
    updateIntent.setData(uriBuilder.build());
    updateIntent.putExtra(Intent.EXTRA_REPLACING, true);
    BroadcastTriggerReceiver broadcastTriggerReceiver = new BroadcastTriggerReceiver();
    // Use reflection to test a private method
    Method isPackageAdded = broadcastTriggerReceiver.getClass().getDeclaredMethod("isPackageAdded", Context.class, Intent.class);
    isPackageAdded.setAccessible(true);
    assertEquals(isPackageAdded.invoke(broadcastTriggerReceiver, context, updateIntent), false);
  }
}

@@ -29,16 +29,21 @@
 pps.paco
+.test
 ;%0A%0Aimpor
@@ -48,56 +48,59 @@
 ort 
-org.junit.Test;%0A%0Aimport android.content.Inten
+java.lang.reflect.Method;%0A%0Aimport org.junit.Tes
 t;%0A
+%0A
 impo
@@ -180,23 +180,30 @@
 android.
-net.Uri
+content.Intent
 ;%0Aimport
@@ -222,51 +222,10 @@
 .Uri
-.Builder
 ;%0A
-import android.os.PowerManager;%0A%0A
 impo
@@ -300,42 +300,8 @@
 t;%0A%0A
-import java.lang.reflect.Method;%0A%0A
 impo
@@ -777,20 +777,16 @@
 ervice;%0A
-    
 %0A    @Ov
@@ -1694,18 +1694,16 @@
 e);%0A  %7D%0A
-  
 %0A  @Test

Fix package name