ConnectivityUtilsTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Edvard Holst
  5. * Copyright (C) 2019 Edvard Holst
  6. * Copyright (C) 2019 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.utils;
  22. import android.accounts.Account;
  23. import android.content.Context;
  24. import android.net.ConnectivityManager;
  25. import android.net.NetworkInfo;
  26. import com.owncloud.android.authentication.AccountUtils;
  27. import com.owncloud.android.lib.common.OwnCloudAccount;
  28. import com.owncloud.android.lib.common.OwnCloudClient;
  29. import com.owncloud.android.lib.common.OwnCloudClientFactory;
  30. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  31. import org.apache.commons.httpclient.HttpStatus;
  32. import org.apache.commons.httpclient.methods.GetMethod;
  33. import org.json.JSONObject;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.mockito.Mock;
  38. import org.mockito.MockitoAnnotations;
  39. import org.powermock.api.mockito.PowerMockito;
  40. import org.powermock.core.classloader.annotations.PowerMockIgnore;
  41. import org.powermock.core.classloader.annotations.PrepareForTest;
  42. import org.powermock.modules.junit4.PowerMockRunner;
  43. import static org.junit.Assert.*;
  44. import static org.mockito.ArgumentMatchers.eq;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.powermock.api.mockito.PowerMockito.mockStatic;
  48. @RunWith(PowerMockRunner.class)
  49. @PowerMockIgnore({"org.slf4j.*"})
  50. @PrepareForTest({AccountUtils.class, OwnCloudClientFactory.class, ConnectivityUtils.class})
  51. public class ConnectivityUtilsTest {
  52. @Mock
  53. private Context mContext;
  54. @Mock
  55. private ConnectivityManager mConnectivityManager;
  56. @Mock
  57. private NetworkInfo mNetworkInfo;
  58. @Mock
  59. private Account mAccount;
  60. @Mock
  61. private OwnCloudAccount mOcAccount;
  62. @Mock
  63. private OwnCloudClient mClient;
  64. @Mock
  65. private GetMethod mGetMethod;
  66. @Before
  67. public void setUp() {
  68. MockitoAnnotations.initMocks(this);
  69. when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityManager);
  70. when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(mNetworkInfo);
  71. mockStatic(AccountUtils.class);
  72. mockStatic(OwnCloudClientFactory.class);
  73. }
  74. @Test
  75. public void isOnlineWithWifi_assertTrueWhenOnWifi() {
  76. when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  77. when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
  78. assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
  79. }
  80. @Test
  81. public void isOnlineWithWifi_assertTrueWhenOnVPNWithAdditionalWiFiConnection() {
  82. when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  83. when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN);
  84. NetworkInfo[] networkInfoList = new NetworkInfo[1];
  85. NetworkInfo wifiNetworkInfo = mock(NetworkInfo.class);
  86. when(wifiNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  87. when(wifiNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
  88. networkInfoList[0] = wifiNetworkInfo;
  89. when(mConnectivityManager.getAllNetworkInfo()).thenReturn(networkInfoList);
  90. assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
  91. }
  92. @Test
  93. public void isOnlineWithWifi_assertFalseWhenNotOnWifi() {
  94. when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  95. when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
  96. assertFalse("Falsely indicated connection on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
  97. }
  98. @Test
  99. public void isInternetWalled_assertFalseWhenOnOlderNC() throws Exception {
  100. // Ensure we are on WiFi
  101. when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  102. when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
  103. PowerMockito.when(AccountUtils.getCurrentOwnCloudAccount(eq(mContext))).thenReturn(mAccount);
  104. PowerMockito.whenNew(OwnCloudAccount.class).withAnyArguments().thenReturn(mOcAccount);
  105. PowerMockito.when(AccountUtils.getServerVersion(eq(mAccount))).thenReturn(OwnCloudVersion.nextcloud_13);
  106. PowerMockito.when(OwnCloudClientFactory.createOwnCloudClient(eq(mAccount), eq(mContext))).thenReturn(mClient);
  107. PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(mGetMethod);
  108. // Return SC_OK
  109. when(mClient.executeMethod(mGetMethod)).thenReturn(HttpStatus.SC_OK);
  110. // Content length should be > 0.
  111. when(mGetMethod.getResponseContentLength()).thenReturn(1024L);
  112. JSONObject jsonObj = new JSONObject();
  113. jsonObj.put("maintenance", false);
  114. when(mGetMethod.getResponseBodyAsString()).thenReturn(jsonObj.toString());
  115. assertFalse("internet was falsely claimed to be walled",
  116. ConnectivityUtils.isInternetWalled(mContext));
  117. }
  118. @Test
  119. public void isInternetWalled_assertFalseWhenOnNewerNC() throws Exception {
  120. // Ensure we are on WiFi
  121. when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
  122. when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
  123. PowerMockito.when(AccountUtils.getCurrentOwnCloudAccount(eq(mContext))).thenReturn(mAccount);
  124. PowerMockito.whenNew(OwnCloudAccount.class).withAnyArguments().thenReturn(mOcAccount);
  125. PowerMockito.when(AccountUtils.getServerVersion(eq(mAccount))).thenReturn(OwnCloudVersion.nextcloud_14);
  126. PowerMockito.when(OwnCloudClientFactory.createOwnCloudClient(eq(mAccount), eq(mContext))).thenReturn(mClient);
  127. PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(mGetMethod);
  128. // Return SC_NO_CONTENT
  129. when(mClient.executeMethod(mGetMethod)).thenReturn(HttpStatus.SC_NO_CONTENT);
  130. // Content length should be 0.
  131. when(mGetMethod.getResponseContentLength()).thenReturn(0L);
  132. assertFalse("internet was falsely claimed to be walled",
  133. ConnectivityUtils.isInternetWalled(mContext));
  134. }
  135. }