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-09-03 23:31:26 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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-10-01 19:28:29 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
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;
|
|
|
|
|
|
|
|
@RunWith(PowerMockRunner.class)
|
2015-10-01 19:28:29 +00:00
|
|
|
@PrepareForTest({ Log.class, Handler.class, Looper.class })
|
2015-09-03 23:31:26 +00:00
|
|
|
public abstract class BaseUnitTest {
|
2015-10-01 19:28:29 +00:00
|
|
|
protected Context context;
|
|
|
|
protected MasterSecret masterSecret;
|
|
|
|
|
2015-09-03 23:31:26 +00:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2015-10-01 19:28:29 +00:00
|
|
|
context = mock(Context.class);
|
|
|
|
masterSecret = new MasterSecret(new SecretKeySpec(new byte[16], "AES"),
|
|
|
|
new SecretKeySpec(new byte[16], "HmacSHA1"));
|
2015-09-03 23:31:26 +00:00
|
|
|
mockStatic(Looper.class);
|
|
|
|
mockStatic(Log.class);
|
|
|
|
mockStatic(Handler.class);
|
|
|
|
|
|
|
|
PowerMockito.when(Looper.getMainLooper()).thenReturn(null);
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|