2015-09-03 16:31:26 -07:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2015-10-01 12:28:29 -07:00
|
|
|
import android.content.Context;
|
2015-11-16 12:15:15 -08:00
|
|
|
import android.content.SharedPreferences;
|
2015-09-03 16:31:26 -07:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2015-11-18 18:09:39 -08:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-16 12:15:15 -08:00
|
|
|
import android.text.TextUtils;
|
2015-09-03 16:31:26 -07:00
|
|
|
|
|
|
|
import org.junit.Before;
|
2022-03-10 12:31:48 -04:00
|
|
|
import org.junit.Rule;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.MockedConstruction;
|
|
|
|
import org.mockito.MockedStatic;
|
|
|
|
import org.mockito.junit.MockitoJUnit;
|
|
|
|
import org.mockito.junit.MockitoRule;
|
2015-09-03 16:31:26 -07:00
|
|
|
import org.mockito.stubbing.Answer;
|
2020-12-04 18:31:58 -05:00
|
|
|
import org.signal.core.util.logging.Log;
|
2015-09-03 16:31:26 -07:00
|
|
|
|
2022-03-10 12:31:48 -04:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyFloat;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyInt;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
2015-09-03 16:31:26 -07:00
|
|
|
public abstract class BaseUnitTest {
|
2015-10-01 12:28:29 -07:00
|
|
|
|
2022-03-10 12:31:48 -04:00
|
|
|
@Rule
|
|
|
|
public MockitoRule rule = MockitoJUnit.rule();
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedStatic<Looper> looperMockedStatic;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedStatic<Log> logMockedStatic;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedStatic<Handler> handlerMockedStatic;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedStatic<TextUtils> textUtilsMockedStatic;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedStatic<PreferenceManager> preferenceManagerMockedStatic;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private MockedConstruction<Handler> handlerMockedConstruction;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
protected Context context = mock(Context.class);
|
|
|
|
protected SharedPreferences sharedPreferences = mock(SharedPreferences.class);
|
|
|
|
|
2015-09-03 16:31:26 -07:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2015-11-18 18:09:39 -08:00
|
|
|
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(sharedPreferences);
|
2015-11-16 12:15:15 -08:00
|
|
|
when(Looper.getMainLooper()).thenReturn(null);
|
2022-03-10 12:31:48 -04:00
|
|
|
|
|
|
|
Answer<?> logAnswer = (Answer<Void>) invocation -> {
|
|
|
|
final String tag = (String)invocation.getArguments()[0];
|
|
|
|
final String msg = (String)invocation.getArguments()[1];
|
|
|
|
System.out.println(invocation.getMethod().getName().toUpperCase() + "/[" + tag + "] " + msg);
|
|
|
|
return null;
|
2015-09-03 16:31:26 -07:00
|
|
|
};
|
2022-03-10 12:31:48 -04:00
|
|
|
|
|
|
|
logMockedStatic.when(() -> Log.d(anyString(), anyString())).thenAnswer(logAnswer);
|
|
|
|
logMockedStatic.when(() -> Log.i(anyString(), anyString())).thenAnswer(logAnswer);
|
|
|
|
logMockedStatic.when(() -> Log.w(anyString(), anyString())).thenAnswer(logAnswer);
|
|
|
|
logMockedStatic.when(() -> Log.e(anyString(), anyString())).thenAnswer(logAnswer);
|
|
|
|
|
|
|
|
Answer<Boolean> isEmptyAnswer = invocation -> {
|
|
|
|
final String s = (String)invocation.getArguments()[0];
|
|
|
|
return s == null || s.length() == 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
textUtilsMockedStatic.when(() -> TextUtils.isEmpty(anyString())).thenAnswer(isEmptyAnswer);
|
2015-11-16 12:15:15 -08:00
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
when(sharedPreferences.getString(anyString(), anyString())).thenReturn("");
|
|
|
|
when(sharedPreferences.getLong(anyString(), anyLong())).thenReturn(0L);
|
|
|
|
when(sharedPreferences.getInt(anyString(), anyInt())).thenReturn(0);
|
|
|
|
when(sharedPreferences.getBoolean(anyString(), anyBoolean())).thenReturn(false);
|
|
|
|
when(sharedPreferences.getFloat(anyString(), anyFloat())).thenReturn(0f);
|
|
|
|
when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences);
|
|
|
|
when(context.getPackageName()).thenReturn("org.thoughtcrime.securesms");
|
2015-09-03 16:31:26 -07:00
|
|
|
}
|
|
|
|
}
|