2015-09-03 23:31:26 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2015-10-01 19:28:29 +00:00
|
|
|
import android.content.Context;
|
2015-11-16 20:15:15 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-09-03 23:31:26 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2015-11-19 02:09:39 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-16 20:15:15 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2015-09-03 23:31:26 +00:00
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.mockito.invocation.InvocationOnMock;
|
|
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
import org.powermock.api.mockito.PowerMockito;
|
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
|
|
2015-11-16 20:15:15 +00:00
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
import static org.mockito.Matchers.anyBoolean;
|
|
|
|
import static org.mockito.Matchers.anyFloat;
|
|
|
|
import static org.mockito.Matchers.anyInt;
|
|
|
|
import static org.mockito.Matchers.anyLong;
|
2015-09-03 23:31:26 +00:00
|
|
|
import static org.mockito.Matchers.anyString;
|
2015-10-01 19:28:29 +00:00
|
|
|
import static org.powermock.api.mockito.PowerMockito.mock;
|
2015-09-03 23:31:26 +00:00
|
|
|
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
2015-11-16 20:15:15 +00:00
|
|
|
import static org.powermock.api.mockito.PowerMockito.when;
|
2015-09-03 23:31:26 +00:00
|
|
|
|
|
|
|
@RunWith(PowerMockRunner.class)
|
2015-11-19 02:09:39 +00:00
|
|
|
@PrepareForTest({ Log.class, Handler.class, Looper.class, TextUtils.class, PreferenceManager.class })
|
2015-09-03 23:31:26 +00:00
|
|
|
public abstract class BaseUnitTest {
|
2015-10-01 19:28:29 +00:00
|
|
|
|
2015-11-19 02:09:39 +00:00
|
|
|
protected Context context = mock(Context.class);
|
|
|
|
protected SharedPreferences sharedPreferences = mock(SharedPreferences.class);
|
|
|
|
|
2015-09-03 23:31:26 +00:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
mockStatic(Looper.class);
|
|
|
|
mockStatic(Log.class);
|
|
|
|
mockStatic(Handler.class);
|
2015-11-16 20:15:15 +00:00
|
|
|
mockStatic(TextUtils.class);
|
2015-11-19 02:09:39 +00:00
|
|
|
mockStatic(PreferenceManager.class);
|
2015-09-03 23:31:26 +00:00
|
|
|
|
2015-11-19 02:09:39 +00:00
|
|
|
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(sharedPreferences);
|
2015-11-16 20:15:15 +00:00
|
|
|
when(Looper.getMainLooper()).thenReturn(null);
|
2015-09-03 23:31:26 +00:00
|
|
|
PowerMockito.whenNew(Handler.class).withAnyArguments().thenReturn(null);
|
|
|
|
|
|
|
|
Answer<?> logAnswer = new Answer<Void>() {
|
|
|
|
@Override public Void answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
PowerMockito.doAnswer(logAnswer).when(Log.class, "d", anyString(), anyString());
|
|
|
|
PowerMockito.doAnswer(logAnswer).when(Log.class, "i", anyString(), anyString());
|
|
|
|
PowerMockito.doAnswer(logAnswer).when(Log.class, "w", anyString(), anyString());
|
|
|
|
PowerMockito.doAnswer(logAnswer).when(Log.class, "e", anyString(), anyString());
|
|
|
|
PowerMockito.doAnswer(logAnswer).when(Log.class, "wtf", anyString(), anyString());
|
2015-11-16 20:15:15 +00:00
|
|
|
|
|
|
|
PowerMockito.doAnswer(new Answer<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public Boolean answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
final String s = (String)invocation.getArguments()[0];
|
|
|
|
return s == null || s.length() == 0;
|
|
|
|
}
|
|
|
|
}).when(TextUtils.class, "isEmpty", anyString());
|
|
|
|
|
2015-11-19 02:09:39 +00: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 23:31:26 +00:00
|
|
|
}
|
|
|
|
}
|