TriggerLocation extends Trigger { @Override public synchronized boolean shouldRun() { Location location = this.getCurrentLocation(); if (location == null) return false; if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; Location a = new Location("Trigger"); a.setLatitude(latitude.getValue()); a.setLongitude(longitude.getValue()); double calculatedDistance = location.distanceTo(a); if ((modeSelected.getValue() == INSIDE) && (calculatedDistance <= distance.getValue()) || ((modeSelected.getValue() == OUTSIDE) && (calculatedDistance > distance.getValue())) || ((modeSelected.getValue() == GOING_IN) && (calculatedDistance <= distance.getValue()) && (lastMode == OUTSIDE)) || ((modeSelected.getValue() == GOING_OUT) && (calculatedDistance > distance.getValue()) && (lastMode == INSIDE)) ) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); lastMode = currentMode(calculatedDistance); return true; } lastMode = currentMode(calculatedDistance); return false; } TriggerLocation(); private  TriggerLocation(TriggerLocation triggerLocation); @Override synchronized boolean shouldRun(); @Override synchronized String toJSON(); @Override int friendlyName(); @Override String friendlyDescription(); @Override Optional<Integer> icon(); @Override Trigger duplicate(); @Override void generateDialog(LinearLayout root, FragmentManager fragmentManager);  }
@Test public void shouldRunTest() { TriggerLocation t = new TriggerLocation(); t.latitude.setValue(213); t.longitude.setValue(212); t.distance.setValue(2); PowerMockito.when(LocationService.getLastLocation()).thenReturn(null); Assert.assertFalse(t.shouldRun()); PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocation()); Assert.assertTrue(t.shouldRun()); t.lastRun(now - 1); Assert.assertFalse(t.shouldRun()); t = new TriggerLocation(); t.distance.setValue(-500); Assert.assertFalse(t.shouldRun()); t = new TriggerLocation(); t.distance.setValue(50); t.lastMode = t.currentMode(55d); PowerMockito.when(LocationService.getLastLocation()).thenReturn(null); PowerMockito.when(LocationService.getLastLocation()).thenReturn(mockedLocationOut()); t.modeSelected.setValue(InputLocationMode.Mode.GOING_IN); Assert.assertEquals(t.lastMode, InputLocationMode.Mode.OUTSIDE); Assert.assertEquals(t.currentMode(5d), InputLocationMode.Mode.INSIDE); Assert.assertTrue(t.shouldRun()); }