HaloUtils { @Api(1.0) public static boolean isNetworkConnected(Context context) { boolean networkConnected = false; if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); networkConnected = info != null && info.isAvailable() && info.isConnected(); } return networkConnected; } private  HaloUtils(); @Api(1.0) static boolean isNetworkConnected(Context context); @Api(2.0) static boolean isCharging(@NonNull Context context, @Nullable Intent intent); @Api(1.0) static void hideKeyboard(@Nullable View focusView); @Api(1.0) static void postUI(Runnable runnable); @Api(1.0) static boolean isAvailableForVersion(int sdkVersion); @Api(1.0) @SuppressWarnings("deprecation") static Drawable getDrawable(Context context, @DrawableRes int resource); @Api(1.0) static Uri getUriFromResource(Context context, int resourceId); @Api(1.0) static boolean hasFlag(int flags, int optionFlag); @Api(1.0) static int getArgb(String hexadecimal); @Api(1.0) static boolean isColor(String color); @Api(2.0) static String sha1(@NonNull String origin); @Api(2.0) static String convertToHex(byte[] data); @NonNull @Api(1.3) static String getEventName(@NonNull Context context, @NonNull String eventName); @Api(2.0) static String bytesToHex(@NonNull byte[] bytes); @Api(2.0) static byte[] hexToBytes(@NonNull String data); @Api(2.5) static boolean isServiceRunning(Context context, Class<?> serviceClass);  }
@Test public void thatConnectionCheckNoPermission() { when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)).thenReturn(PackageManager.PERMISSION_DENIED); Assert.assertFalse(HaloUtils.isNetworkConnected(mContext)); }
@Test public void thatConnectionCheckNoConnectionUnavailable() { when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)).thenReturn(PackageManager.PERMISSION_GRANTED); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(null); NetworkInfo info = mock(NetworkInfo.class); when(info.isAvailable()).thenReturn(false); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(info); Assert.assertFalse(HaloUtils.isNetworkConnected(mContext)); }
@Test public void thatConnectionCheckNoConnectionDisconnected() { when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)).thenReturn(PackageManager.PERMISSION_GRANTED); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(null); NetworkInfo info = mock(NetworkInfo.class); when(info.isAvailable()).thenReturn(true); when(info.isConnected()).thenReturn(false); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(info); Assert.assertFalse(HaloUtils.isNetworkConnected(mContext)); }
@Test public void thatConnectionCheckDisconnectedNull() { when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)).thenReturn(PackageManager.PERMISSION_GRANTED); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(null); Assert.assertFalse(HaloUtils.isNetworkConnected(mContext)); }
@Test public void thatConnectionCheckConnected() { when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)).thenReturn(PackageManager.PERMISSION_GRANTED); NetworkInfo info = mock(NetworkInfo.class); when(info.isAvailable()).thenReturn(true); when(info.isConnected()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(info); Assert.assertTrue(HaloUtils.isNetworkConnected(mContext)); }