package org.altbeacon.beacon.service;

import android.content.Context;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.logging.LogManager;
import org.altbeacon.beacon.logging.Loggers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.Collection;

import static org.junit.Assert.assertEquals;

/**
 * Created by dyoung on 7/1/16.
 */
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 28)
public class MonitoringStatusTest {
    private static final String TAG = MonitoringStatusTest.class.getSimpleName();
    @Before
    public void before() {
        org.robolectric.shadows.ShadowLog.stream = System.err;
        LogManager.setLogger(Loggers.verboseLogger());
        LogManager.setVerboseLoggingEnabled(true);
        BeaconManager.setManifestCheckingDisabled(true);
    }

    @Test
    public void savesStatusOfUpTo50RegionsTest() throws Exception {
        Context context = RuntimeEnvironment.application;
        MonitoringStatus monitoringStatus = new MonitoringStatus(context);
        for (int i = 0; i < 50; i++) {
            Region region = new Region(""+i, null, null, null);
            monitoringStatus.addRegion(region, null);
        }
        monitoringStatus.saveMonitoringStatusIfOn();
        MonitoringStatus monitoringStatus2 = new MonitoringStatus(context);
        assertEquals("restored regions should be same number as saved", 50, monitoringStatus2.regions().size());
    }

    @Test
    public void clearsStatusOfOver50RegionsTest() throws Exception {
        Context context = RuntimeEnvironment.application;
        MonitoringStatus monitoringStatus = new MonitoringStatus(context);
        for (int i = 0; i < 51; i++) {
            Region region = new Region(""+i, null, null, null);
            monitoringStatus.addRegion(region, null);
        }
        monitoringStatus.saveMonitoringStatusIfOn();
        MonitoringStatus monitoringStatus2 = new MonitoringStatus(context);
        assertEquals("restored regions should be none", 0, monitoringStatus2.regions().size());
    }

    @Test
    public void refusesToRestoreRegionsIfTooMuchTimeHasPassedSinceSavingTest() throws Exception {
        Context context = RuntimeEnvironment.application;
        MonitoringStatus monitoringStatus = new MonitoringStatus(context);
        for (int i = 0; i < 50; i++) {
            Region region = new Region(""+i, null, null, null);
            monitoringStatus.addRegion(region, null);
        }
        monitoringStatus.saveMonitoringStatusIfOn();
        // Set update time to one hour ago
        monitoringStatus.updateMonitoringStatusTime(System.currentTimeMillis() - 1000*3600l);
        MonitoringStatus monitoringStatus2 = new MonitoringStatus(context);
        assertEquals("restored regions should be none", 0, monitoringStatus2.regions().size());
    }

    @Test
    public void allowsAccessToRegionsAfterRestore() throws Exception {
        Context context = RuntimeEnvironment.application;
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(context);
        MonitoringStatus.getInstanceForApplication(context).clear();
        MonitoringStatus monitoringStatus = new MonitoringStatus(context);
        for (int i = 0; i < 50; i++) {
            Region region = new Region(""+i, null, null, null);
            monitoringStatus.addRegion(region, null);
        }
        monitoringStatus.saveMonitoringStatusIfOn();
        MonitoringStatus.getInstanceForApplication(context).restoreMonitoringStatus();
        Collection<Region> regions = beaconManager.getMonitoredRegions();
        assertEquals("beaconManager should return restored regions", 50, regions.size());
    }


}

@@ -1001,24 +1001,129 @@
 bled(true);%0A
+        Context context = RuntimeEnvironment.application;%0A        new MonitoringStatus(context).clear();%0A
     %7D%0A%0A    @
@@ -3387,62 +3387,10 @@
 atus
-.getInstanceForApplication(context).clear();%0A        M
+ m
 onit
@@ -3393,33 +3393,35 @@
 onitoringStatus 
-m
+= M
 onitoringStatus 
@@ -3419,39 +3419,42 @@
 ngStatus
- = new MonitoringStatus
+.getInstanceForApplication
 (context
@@ -3676,33 +3676,33 @@
 IfOn();%0A        
-M
+m
 onitoringStatus.
@@ -3705,43 +3705,8 @@
 tus.
-getInstanceForApplication(context).
 rest

Fix intermittent failures on MonitoringStatusTest by clearly setting initial state