TriggerWifiSsid extends Trigger { @Override public synchronized boolean shouldRun() { EventNetworkChange eventNetworkChange = NetworkChangeReceiver.getLastEvent(); if (eventNetworkChange == null) return false; if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; if (!eventNetworkChange.getWifiConnected() && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } boolean doRun = eventNetworkChange.getWifiConnected() && comparator.getValue().check(eventNetworkChange.connectedSsid(), getValue()); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } return false; } TriggerWifiSsid(); private  TriggerWifiSsid(TriggerWifiSsid triggerWifiSsid); String getValue(); Comparator getComparator(); @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() { EventNetworkChange e = new EventNetworkChange(); when(NetworkChangeReceiver.getLastEvent()).thenReturn(e); TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL); e.setWifiConnected(false); Assert.assertFalse(t.shouldRun()); e.setWifiConnected(true); e.setSsid("otherSSID"); Assert.assertFalse(t.shouldRun()); e.setWifiConnected(true); e.setSsid("aSSID"); Assert.assertTrue(t.shouldRun()); t.lastRun(now - 1); Assert.assertFalse(t.shouldRun()); t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_NOT_AVAILABLE); e.setWifiConnected(false); Assert.assertTrue(t.shouldRun()); when(NetworkChangeReceiver.getLastEvent()).thenReturn(null); Assert.assertFalse(t.shouldRun()); }