From 487f8cbb38d1d3474c2ab7d013c69e41268ea86c Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Mon, 16 Nov 2015 12:15:15 -0800 Subject: [PATCH] fix broken unit tests fixes #4534 // FREEBIE --- .../thoughtcrime/securesms/BaseUnitTest.java | 32 +++++++++++++++++-- .../jobs/AttachmentDownloadJobTest.java | 4 +-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/test/unitTest/java/org/thoughtcrime/securesms/BaseUnitTest.java b/test/unitTest/java/org/thoughtcrime/securesms/BaseUnitTest.java index 030a969684..18ad13b1aa 100644 --- a/test/unitTest/java/org/thoughtcrime/securesms/BaseUnitTest.java +++ b/test/unitTest/java/org/thoughtcrime/securesms/BaseUnitTest.java @@ -1,8 +1,10 @@ package org.thoughtcrime.securesms; import android.content.Context; +import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; +import android.text.TextUtils; import android.util.Log; import org.junit.Before; @@ -14,14 +16,23 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.thoughtcrime.securesms.crypto.MasterSecret; +import java.util.Set; + import javax.crypto.spec.SecretKeySpec; +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; import static org.mockito.Matchers.anyString; +import static org.powermock.api.mockito.PowerMockito.doReturn; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({ Log.class, Handler.class, Looper.class }) +@PrepareForTest({ Log.class, Handler.class, Looper.class, TextUtils.class }) public abstract class BaseUnitTest { protected Context context; protected MasterSecret masterSecret; @@ -34,8 +45,9 @@ public abstract class BaseUnitTest { mockStatic(Looper.class); mockStatic(Log.class); mockStatic(Handler.class); + mockStatic(TextUtils.class); - PowerMockito.when(Looper.getMainLooper()).thenReturn(null); + when(Looper.getMainLooper()).thenReturn(null); PowerMockito.whenNew(Handler.class).withAnyArguments().thenReturn(null); Answer logAnswer = new Answer() { @@ -51,5 +63,21 @@ public abstract class BaseUnitTest { 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()); + + PowerMockito.doAnswer(new Answer() { + @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()); + + SharedPreferences mockSharedPreferences = mock(SharedPreferences.class); + when(mockSharedPreferences.getString(anyString(), anyString())).thenReturn(""); + when(mockSharedPreferences.getLong(anyString(), anyLong())).thenReturn(0L); + when(mockSharedPreferences.getInt(anyString(), anyInt())).thenReturn(0); + when(mockSharedPreferences.getBoolean(anyString(), anyBoolean())).thenReturn(false); + when(mockSharedPreferences.getFloat(anyString(), anyFloat())).thenReturn(0f); + when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(mockSharedPreferences); } } diff --git a/test/unitTest/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJobTest.java b/test/unitTest/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJobTest.java index 0f483dec90..5ca7ddebd8 100644 --- a/test/unitTest/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJobTest.java +++ b/test/unitTest/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJobTest.java @@ -1,7 +1,5 @@ package org.thoughtcrime.securesms.jobs; -import android.content.Context; - import org.junit.Before; import org.junit.Test; import org.thoughtcrime.securesms.BaseUnitTest; @@ -19,7 +17,7 @@ public class AttachmentDownloadJobTest extends BaseUnitTest { @Override public void setUp() throws Exception { super.setUp(); - job = new AttachmentDownloadJob(mock(Context.class), 1L, new AttachmentId(1L, 1L)); + job = new AttachmentDownloadJob(context, 1L, new AttachmentId(1L, 1L)); } @Test(expected = InvalidPartException.class)