Search in sources :

Example 1 with ShuttleApplication

use of com.simplecity.amp_library.ShuttleApplication in project Shuttle by timusus.

the class ShuttleUtilsPowerMockTest method testGetIpAddr.

@Test
public void testGetIpAddr() throws Exception {
    ShuttleApplication mockApplication = mock(ShuttleApplication.class);
    WifiManager mockWifiManager = mock(WifiManager.class);
    WifiInfo mockWifiInfo = mock(WifiInfo.class);
    // Setup mocked IP Address of 192.168.1.1
    mockStatic(ShuttleApplication.class);
    when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
    when(mockApplication.getSystemService(Context.WIFI_SERVICE)).thenReturn(mockWifiManager);
    when(mockWifiManager.getConnectionInfo()).thenReturn(mockWifiInfo);
    when(mockWifiInfo.getIpAddress()).thenReturn(16885952);
    assertThat(ShuttleUtils.getIpAddr()).isEqualTo("192.168.1.1");
}
Also used : WifiManager(android.net.wifi.WifiManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) WifiInfo(android.net.wifi.WifiInfo) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with ShuttleApplication

use of com.simplecity.amp_library.ShuttleApplication in project Shuttle by timusus.

the class ShuttleUtilsPowerMockTest method testIsOnlineWifiConnected.

@Test
public void testIsOnlineWifiConnected() {
    ShuttleApplication mockApplication = mock(ShuttleApplication.class);
    SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
    ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
    NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
    mockStatic(PreferenceManager.class);
    mockStatic(ShuttleApplication.class);
    when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
    when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
    // Mock the connection to Wi-Fi
    when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
    when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(mockNetworkInfo);
    when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
    // Test when we care about Wi-Fi (and it's connected), regardless of user preference
    assertThat(ShuttleUtils.isOnline(true)).isTrue();
    // Test when we don't care about Wi-Fi (but it's connected anyway), regardless of user preference
    assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with ShuttleApplication

use of com.simplecity.amp_library.ShuttleApplication in project Shuttle by timusus.

the class ShuttleUtilsPowerMockTest method testIsOnlineCellularConnected.

@Test
public void testIsOnlineCellularConnected() {
    ShuttleApplication mockApplication = mock(ShuttleApplication.class);
    SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
    ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
    NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
    mockStatic(PreferenceManager.class);
    mockStatic(ShuttleApplication.class);
    when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
    when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
    // Mock the connection to cellular data, but not Wi-Fi
    when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
    when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(null);
    when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo);
    when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
    // Test when we care about Wi-Fi and so does the user (but only cellular is connected)
    when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
    assertThat(ShuttleUtils.isOnline(true)).isFalse();
    // Test when we care about Wi-Fi, but the user doesn't (and only cellular is connected)
    when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
    assertThat(ShuttleUtils.isOnline(true)).isTrue();
    // Test when we don't care about Wi-Fi, even if the user does (and only cellular is connected)
    when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
    assertThat(ShuttleUtils.isOnline(false)).isTrue();
    // Test when we don't care about Wi-Fi and neither does the user (and only cellular is connected)
    when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
    assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with ShuttleApplication

use of com.simplecity.amp_library.ShuttleApplication in project Shuttle by timusus.

the class ShuttleUtilsPowerMockTest method testIsUpgraded.

@Test
public void testIsUpgraded() throws Exception {
    ShuttleApplication mockApplication = mock(ShuttleApplication.class);
    mockStatic(ShuttleApplication.class);
    when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
    when(mockApplication.getIsUpgraded()).thenReturn(true);
    assertThat(ShuttleUtils.isUpgraded()).isTrue();
    when(mockApplication.getIsUpgraded()).thenReturn(false);
    when(mockApplication.getPackageName()).thenReturn("com.simplecity.amp_pro");
    assertThat(ShuttleUtils.isUpgraded()).isTrue();
    when(mockApplication.getPackageName()).thenReturn("bad.package.name");
    assertThat(ShuttleUtils.isUpgraded()).isFalse();
}
Also used : ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ShuttleApplication (com.simplecity.amp_library.ShuttleApplication)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Context (android.content.Context)2 SharedPreferences (android.content.SharedPreferences)2 ConnectivityManager (android.net.ConnectivityManager)2 NetworkInfo (android.net.NetworkInfo)2 WifiInfo (android.net.wifi.WifiInfo)1 WifiManager (android.net.wifi.WifiManager)1