Further clean up of ActivityListenerTest

* \u0020 is a hack required to have spaces at the beginning of resource
  strings. But there's no need to replicate this inside our tests.
* Dependencies shouldn't be static finals.
* Initialization of dependencies is usually boring. I like to extract that
  code to methods and put them at the end of the file.
* Using different values for 'current progress' and 'total' has the
  advantage of breaking tests when the two accidentally get mixed up.
This commit is contained in:
cketti 2016-10-12 16:16:52 +02:00
parent 0fff2ffd08
commit e511b8b7c1

View file

@ -1,10 +1,10 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import android.content.Context; import android.content.Context;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -20,26 +20,30 @@ import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml", sdk = 21) @Config(manifest = "src/main/AndroidManifest.xml", sdk = 21)
public class ActivityListenerTest { public class ActivityListenerTest {
private static final Account ACCOUNT = mock(Account.class);
private static final String FOLDER = "folder"; private static final String FOLDER = "folder";
private static final String ERROR_MESSAGE = "errorMessage"; private static final String ERROR_MESSAGE = "errorMessage";
private static final Message MESSAGE = mock(Message.class); private static final int COUNT = 23;
private static final int COUNT = 1;
private ActivityListener activityListener;
private Context context;
private Context context;
private Account account;
private Message message;
private ActivityListener activityListener;
@Before @Before
public void before() { public void before() {
when(ACCOUNT.getDescription()).thenReturn("account");
activityListener = new ActivityListener();
context = RuntimeEnvironment.application; context = RuntimeEnvironment.application;
account = createAccount();
message = mock(Message.class);
activityListener = new ActivityListener();
} }
@Test @Test
public void getOperation__whenFolderStatusChanged() { public void getOperation__whenFolderStatusChanged() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.folderStatusChanged(ACCOUNT, FOLDER, COUNT); activityListener.folderStatusChanged(account, FOLDER, COUNT);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -48,7 +52,7 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxStarted() { public void getOperation__whenSynchronizeMailboxStarted() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -57,18 +61,18 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxProgress_shouldResultInValidStatus() { public void getOperation__whenSynchronizeMailboxProgress_shouldResultInValidStatus() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.synchronizeMailboxProgress(ACCOUNT, FOLDER, COUNT, COUNT); activityListener.synchronizeMailboxProgress(account, FOLDER, 1, 2);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
assertEquals("Poll account:folder\u00201/1", operation); assertEquals("Poll account:folder 1/2", operation);
} }
@Test @Test
public void getOperation__whenSynchronizeMailboxFailed_shouldResultInValidStatus() { public void getOperation__whenSynchronizeMailboxFailed_shouldResultInValidStatus() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.synchronizeMailboxFailed(ACCOUNT, FOLDER, ERROR_MESSAGE); activityListener.synchronizeMailboxFailed(account, FOLDER, ERROR_MESSAGE);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -77,8 +81,8 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxFinished() { public void getOperation__whenSynchronizeMailboxFinished() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.synchronizeMailboxFinished(ACCOUNT, FOLDER, COUNT, COUNT); activityListener.synchronizeMailboxFinished(account, FOLDER, COUNT, COUNT);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -87,7 +91,7 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxHeadersStarted_shouldResultInValidStatus() { public void getOperation__whenSynchronizeMailboxHeadersStarted_shouldResultInValidStatus() {
activityListener.synchronizeMailboxHeadersStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxHeadersStarted(account, FOLDER);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -96,18 +100,18 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxHeadersProgress() { public void getOperation__whenSynchronizeMailboxHeadersProgress() {
activityListener.synchronizeMailboxHeadersStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxHeadersStarted(account, FOLDER);
activityListener.synchronizeMailboxHeadersProgress(ACCOUNT, FOLDER, COUNT, COUNT); activityListener.synchronizeMailboxHeadersProgress(account, FOLDER, 2, 3);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
assertEquals("Fetching headers account:folder\u00201/1", operation); assertEquals("Fetching headers account:folder 2/3", operation);
} }
@Test @Test
public void getOperation__whenSynchronizeMailboxHeadersFinished() { public void getOperation__whenSynchronizeMailboxHeadersFinished() {
activityListener.synchronizeMailboxHeadersStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxHeadersStarted(account, FOLDER);
activityListener.synchronizeMailboxHeadersFinished(ACCOUNT, FOLDER, COUNT, COUNT); activityListener.synchronizeMailboxHeadersFinished(account, FOLDER, COUNT, COUNT);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -116,8 +120,8 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxAddOrUpdateMessage() { public void getOperation__whenSynchronizeMailboxAddOrUpdateMessage() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.synchronizeMailboxAddOrUpdateMessage(ACCOUNT, FOLDER, MESSAGE); activityListener.synchronizeMailboxAddOrUpdateMessage(account, FOLDER, message);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
@ -126,11 +130,17 @@ public class ActivityListenerTest {
@Test @Test
public void getOperation__whenSynchronizeMailboxNewMessage() { public void getOperation__whenSynchronizeMailboxNewMessage() {
activityListener.synchronizeMailboxStarted(ACCOUNT, FOLDER); activityListener.synchronizeMailboxStarted(account, FOLDER);
activityListener.synchronizeMailboxNewMessage(ACCOUNT, FOLDER, MESSAGE); activityListener.synchronizeMailboxNewMessage(account, FOLDER, message);
String operation = activityListener.getOperation(context); String operation = activityListener.getOperation(context);
assertEquals("Poll account:folder", operation); assertEquals("Poll account:folder", operation);
} }
private Account createAccount() {
Account account = mock(Account.class);
when(account.getDescription()).thenReturn("account");
return account;
}
} }