/** * Enables internet connection check. * when it's not called, WIFI_CONNECTED_HAS_INTERNET * and WIFI_CONNECTED_HAS_NO_INTERNET ConnectivityStatus will never be set * Please, be careful! Internet connection check may contain bugs * that's why it's disabled by default. * * @return NetworkEvents object */ public NetworkEvents enableInternetCheck() { networkConnectionChangeReceiver.enableInternetCheck(); return this; }
@Override public void onReceive(final Context context, Intent intent) { onPostReceive(getConnectivityStatus(context)); }
public void onPostReceive(boolean connectedToInternet, Context context) { ConnectivityStatus connectivityStatus = (connectedToInternet) ? ConnectivityStatus.WIFI_CONNECTED_HAS_INTERNET : ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET; if (statusNotChanged(connectivityStatus)) { return; } // we are checking if device is connected to WiFi again, // because connectivityStatus may change in a short period of time // after receiving it if (context != null && !isConnectedToWifi(context)) { return; } postConnectivityChanged(connectivityStatus); }
/** * Initializes NetworkEvents object. * * @param context Android context * @param busWrapper Wrapper fo event bus * @param logger message logger (NetworkEventsLogger logs messages to LogCat) */ public NetworkEvents(Context context, BusWrapper busWrapper, Logger logger) { checkNotNull(context, "context == null"); checkNotNull(busWrapper, "busWrapper == null"); checkNotNull(logger, "logger == null"); this.context = context; this.onlineChecker = new OnlineCheckerImpl(context); this.networkConnectionChangeReceiver = new NetworkConnectionChangeReceiver(busWrapper, logger, context, onlineChecker); this.internetConnectionChangeReceiver = new InternetConnectionChangeReceiver(busWrapper, logger, context); this.wifiSignalStrengthChangeReceiver = new WifiSignalStrengthChangeReceiver(busWrapper, logger, context); }
@Test public void testReceiverShouldReceiveAnEventWhenGoingOffline() throws Exception { // given connectivityChangeEvents.clear(); Object eventCatcher = TestUtils.getConnectivityEventCatcher(connectivityChangeEvents); setUnknownNetworkStatusAndRegisterBus(eventCatcher); ConnectivityStatus expectedConnectivityStatus = ConnectivityStatus.OFFLINE; // when onPostReceiveAndSleep(expectedConnectivityStatus); // then assertExpectedStatusEqualsCurrent(expectedConnectivityStatus, eventCatcher); }
@Test public void testShouldReceiveAnEventWhenWifiSignalStrengthChanged() throws InterruptedException { // given final List<WifiSignalStrengthChanged> connectivityChangeEvents = new ArrayList<>(); Object eventCatcher = getWifiEventCatcher(connectivityChangeEvents); busWrapper.register(eventCatcher); // when receiver.onPostReceive(); Thread.sleep(2000); // wait a while for async operation // then assertThat(connectivityChangeEvents).isNotEmpty(); busWrapper.unregister(eventCatcher); }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(INTENT)) { boolean connectedToInternet = intent.getBooleanExtra(INTENT_EXTRA, false); onPostReceive(connectedToInternet, context); } }
@Test public void testShouldReceiveAnEventWhenDeviceIsConnectedToWifiWithInternetAccess() throws InterruptedException { // given connectivityChangeEvents.clear(); boolean connectedToInternet = true; NetworkState.status = ConnectivityStatus.UNKNOWN; Object eventCatcher = TestUtils.getConnectivityEventCatcher(connectivityChangeEvents); busWrapper.register(eventCatcher); // when onPostReceiveAndSleep(connectedToInternet); // then ConnectivityStatus expectedConnectivityStatus = ConnectivityStatus.WIFI_CONNECTED_HAS_INTERNET; assertExpectedStatusEqualsCurrent(expectedConnectivityStatus, eventCatcher); }
public void onPostReceive(final ConnectivityStatus connectivityStatus) { if (statusNotChanged(connectivityStatus)) { return; } boolean isConnectedToWifi = connectivityStatus == ConnectivityStatus.WIFI_CONNECTED; if (internetCheckEnabled && isConnectedToWifi) { onlineChecker.check(); } else { postConnectivityChanged(connectivityStatus); } }
@Override public void onReceive(Context context, Intent intent) { // We need to start WiFi scan after receiving an Intent // in order to get update with fresh data as soon as possible WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.startScan(); onPostReceive(); }
private void onPostReceiveAndSleep(ConnectivityStatus status) throws InterruptedException { receiver.onPostReceive(status); Thread.sleep(2000); // wait a while for async operation }
@Before public void setUp() throws Exception { this.busWrapper = new OttoBusWrapper(new Bus(ThreadEnforcer.ANY)); Logger logger = Mockito.mock(Logger.class); Context context = Mockito.mock(Context.class); OnlineChecker onlineChecker = Mockito.mock(OnlineChecker.class); this.receiver = new NetworkConnectionChangeReceiver(busWrapper, logger, context, onlineChecker); this.connectivityChangeEvents = new ArrayList<>(); }
@Before public void setUp() throws Exception { this.busWrapper = new OttoBusWrapper(new Bus(ThreadEnforcer.ANY)); Logger logger = Mockito.mock(Logger.class); Context context = Mockito.mock(Context.class); this.receiver = new WifiSignalStrengthChangeReceiver(busWrapper, logger, context); }
@Before public void setUp() throws Exception { this.busWrapper = new OttoBusWrapper(new Bus(ThreadEnforcer.ANY)); Logger logger = Mockito.mock(Logger.class); Context context = Mockito.mock(Context.class); this.receiver = new InternetConnectionChangeReceiver(busWrapper, logger, context); this.connectivityChangeEvents = new ArrayList<>(); }
protected void postConnectivityChanged(ConnectivityStatus connectivityStatus) { NetworkState.status = connectivityStatus; postFromAnyThread(new ConnectivityChanged(connectivityStatus, logger, context)); }
public void onPostReceive() { postFromAnyThread(new WifiSignalStrengthChanged(logger, context)); } }
@Test public void testReceiverShouldReceiveAnEventWhenGoingOnlineViaWifi() throws Exception { // given connectivityChangeEvents.clear(); Object eventCatcher = TestUtils.getConnectivityEventCatcher(connectivityChangeEvents); setUnknownNetworkStatusAndRegisterBus(eventCatcher); ConnectivityStatus expectedConnectivityStatus = ConnectivityStatus.WIFI_CONNECTED; // when onPostReceiveAndSleep(expectedConnectivityStatus); // then assertExpectedStatusEqualsCurrent(expectedConnectivityStatus, eventCatcher); }
private void onPostReceiveAndSleep(boolean connectedToInternet) throws InterruptedException { // in this case, for unit tests, we should pass Context as null in onPostReceive(...) // to avoid non-deterministic calls to Android SDK receiver.onPostReceive(connectedToInternet, null); Thread.sleep(2000); // wait a while for async operation }
@Test public void testShouldReceiveAnEventWhenDeviceIsConnectedToWifiWithNoInternetAccess() throws InterruptedException { // given connectivityChangeEvents.clear(); boolean connectedToInternet = false; NetworkState.status = ConnectivityStatus.UNKNOWN; Object eventCatcher = TestUtils.getConnectivityEventCatcher(connectivityChangeEvents); busWrapper.register(eventCatcher); // when onPostReceiveAndSleep(connectedToInternet); // then ConnectivityStatus expectedStatus = ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET; assertExpectedStatusEqualsCurrent(expectedStatus, eventCatcher); }
@Test public void testReceiverShouldReceiveAnEventWhenGoingOnlineViaMobile() throws Exception { // given connectivityChangeEvents.clear(); Object eventCatcher = TestUtils.getConnectivityEventCatcher(connectivityChangeEvents); setUnknownNetworkStatusAndRegisterBus(eventCatcher); ConnectivityStatus expectedConnectivityStatus = ConnectivityStatus.MOBILE_CONNECTED; // when onPostReceiveAndSleep(expectedConnectivityStatus); // then assertExpectedStatusEqualsCurrent(expectedConnectivityStatus, eventCatcher); }