// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; -*-
// vim: et ts=4 sts=4 sw=4 syntax=java
package com.aragaer.jtt.android;

import org.junit.*;
import org.junit.runner.RunWith;

import android.content.*;
import android.content.pm.*;
import android.support.test.runner.AndroidJUnit4;

import com.aragaer.jtt.JttService;

import static org.junit.Assert.*;

import static android.support.test.InstrumentationRegistry.getTargetContext;


@RunWith(AndroidJUnit4.class)
public class TimeChangeReceiverTest {

    private Context context;
    private MyReceiver receiver;

    @Before public void setUp() {
	context = getTargetContext();
	receiver = new MyReceiver();
	context.registerReceiver(receiver, new IntentFilter(AndroidTicker.ACTION_JTT_TICK));
    }

    @Test public void testListensForTimeChange() {
	Intent intent = new Intent(Intent.ACTION_TIME_CHANGED);
	PackageManager pm = context.getPackageManager();
	for (ResolveInfo ri : pm.queryBroadcastReceivers(intent, 0)) {
	    ActivityInfo ai = ri.activityInfo;
	    if (ai.name.equals("com.aragaer.jtt.android.TimeChangeReceiver"))
		return;
	}
	fail("Not listening for time change event");
    }

    @Test public void testListensForDateChange() {
	Intent intent = new Intent(Intent.ACTION_DATE_CHANGED);
	PackageManager pm = context.getPackageManager();
	for (ResolveInfo ri : pm.queryBroadcastReceivers(intent, 0)) {
	    ActivityInfo ai = ri.activityInfo;
	    if (ai.name.equals("com.aragaer.jtt.android.TimeChangeReceiver"))
		return;
	}
	fail("Not listening for date change event");
    }

    @Test public void testTriggersTick() throws Exception {
	new TimeChangeReceiver().onReceive(context, new Intent(Intent.ACTION_DATE_CHANGED));
	synchronized (receiver) {
	    receiver.wait();
	}
	assertEquals("Actually got correct event",
		     AndroidTicker.ACTION_JTT_TICK, receiver.triggered.getAction());
    }

    @After public void tearDown() {
	context.unregisterReceiver(receiver);
    }

    private class MyReceiver extends BroadcastReceiver {
	Intent triggered;

	@Override public void onReceive(Context context, Intent intent) {
	    triggered = intent;
	    synchronized (this) {
		this.notify();
	    }
	}
    }
}

@@ -598,25 +598,32 @@
 d setUp() %7B%0A
-%09
+        
 context = ge
@@ -640,17 +640,24 @@
 text();%0A
-%09
+        
 receiver
@@ -677,17 +677,24 @@
 iver();%0A
-%09
+        
 context.
@@ -820,33 +820,40 @@
 rTimeChange() %7B%0A
-%09
+        
 Intent intent = 
@@ -884,33 +884,40 @@
 _TIME_CHANGED);%0A
-%09
+        
 PackageManager p
@@ -941,33 +941,40 @@
 ckageManager();%0A
-%09
+        
 for (ResolveInfo
@@ -1012,33 +1012,40 @@
 s(intent, 0)) %7B%0A
-%09
+        
     ActivityInfo
@@ -1059,33 +1059,40 @@
 i.activityInfo;%0A
-%09
+        
     if (ai.name.
@@ -1141,38 +1141,66 @@
 Receiver%22))%0A
-%09%09return;%0A%09%7D%0A%09
+                return;%0A        %7D%0A        
 fail(%22Not li
@@ -1290,17 +1290,24 @@
 nge() %7B%0A
-%09
+        
 Intent i
@@ -1354,17 +1354,24 @@
 ANGED);%0A
-%09
+        
 PackageM
@@ -1411,17 +1411,24 @@
 ager();%0A
-%09
+        
 for (Res
@@ -1478,25 +1478,32 @@
 tent, 0)) %7B%0A
-%09
+        
     Activity
@@ -1529,17 +1529,24 @@
 tyInfo;%0A
-%09
+        
     if (
@@ -1611,22 +1611,50 @@
 %22))%0A
-%09%09return;%0A%09%7D%0A%09
+                return;%0A        %7D%0A        
 fail
@@ -1761,17 +1761,24 @@
 ption %7B%0A
-%09
+        
 new Time
@@ -1854,17 +1854,24 @@
 NGED));%0A
-%09
+        
 synchron
@@ -1884,25 +1884,32 @@
 receiver) %7B%0A
-%09
+        
     receiver
@@ -1921,12 +1921,26 @@
 ();%0A
-%09%7D%0A%09
+        %7D%0A        
 asse
@@ -1982,15 +1982,29 @@
 t%22,%0A
-%09%09
+              
      
+  
 Andr
@@ -2106,17 +2106,24 @@
 own() %7B%0A
-%09
+        
 context.
@@ -2216,17 +2216,24 @@
 eiver %7B%0A
-%09
+        
 Intent t
@@ -2243,17 +2243,24 @@
 gered;%0A%0A
-%09
+        
 @Overrid
@@ -2317,17 +2317,24 @@
 tent) %7B%0A
-%09
+        
     trig
@@ -2349,17 +2349,24 @@
 intent;%0A
-%09
+        
     sync
@@ -2387,10 +2387,24 @@
 ) %7B%0A
-%09%09
+                
 this
@@ -2418,16 +2418,30 @@
 ();%0A
-%09
+       
     
+ 
 %7D%0A
-%09
+        
 %7D%0A  

Fix indentation