146 lines
5.0 KiB
Java
146 lines
5.0 KiB
Java
|
/*
|
||
|
* Copyright (C) 2009 The Android Open Source Project
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package com.android.email;
|
||
|
|
||
|
import com.android.email.mail.FetchProfile;
|
||
|
import com.android.email.mail.Flag;
|
||
|
import com.android.email.mail.Folder;
|
||
|
import com.android.email.mail.Message;
|
||
|
import com.android.email.mail.MessageRetrievalListener;
|
||
|
import com.android.email.mail.MessagingException;
|
||
|
import com.android.email.mail.MockFolder;
|
||
|
import com.android.email.mail.Store;
|
||
|
import com.android.email.mail.Folder.FolderRole;
|
||
|
|
||
|
import android.content.SharedPreferences;
|
||
|
import android.test.AndroidTestCase;
|
||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||
|
|
||
|
/**
|
||
|
* This is a series of unit tests for the MessagingController class.
|
||
|
*
|
||
|
* Technically these are functional because they use the underlying preferences framework.
|
||
|
*/
|
||
|
@SmallTest
|
||
|
public class MessagingControllerUnitTests extends AndroidTestCase {
|
||
|
|
||
|
private Preferences mPreferences;
|
||
|
|
||
|
private String mUuid;
|
||
|
private Account mAccount;
|
||
|
|
||
|
@Override
|
||
|
protected void setUp() throws Exception {
|
||
|
super.setUp();
|
||
|
|
||
|
mPreferences = Preferences.getPreferences(getContext());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Delete any dummy accounts we set up for this test
|
||
|
*/
|
||
|
@Override
|
||
|
protected void tearDown() throws Exception {
|
||
|
super.tearDown();
|
||
|
|
||
|
if (mAccount != null && mPreferences != null) {
|
||
|
mAccount.delete(mPreferences);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test the code that copies server-supplied folder names into the account data
|
||
|
*/
|
||
|
public void testUpdateAccountFolderNames() {
|
||
|
// Create a dummy account
|
||
|
createTestAccount();
|
||
|
// Refresh it to fill in all fields (many will have default values)
|
||
|
mAccount.refresh(mPreferences);
|
||
|
|
||
|
// Replace one entry, others are not included
|
||
|
Folder[] folders1 = new Folder[] {
|
||
|
new MyMockFolder(Folder.FolderRole.DRAFTS, "DRAFTS_1"),
|
||
|
};
|
||
|
MessagingController.updateAccountFolderNames(mAccount, folders1);
|
||
|
checkServerFolderNames("folders1", "DRAFTS_1", "Sent", "Trash", "Outbox");
|
||
|
|
||
|
// Replace one entry, others are included but called out as unknown
|
||
|
Folder[] folders2 = new Folder[] {
|
||
|
new MyMockFolder(Folder.FolderRole.UNKNOWN, "DRAFTS_2"),
|
||
|
new MyMockFolder(Folder.FolderRole.SENT, "SENT_2"),
|
||
|
new MyMockFolder(Folder.FolderRole.UNKNOWN, "TRASH_2"),
|
||
|
new MyMockFolder(Folder.FolderRole.UNKNOWN, "OUTBOX_2"),
|
||
|
};
|
||
|
MessagingController.updateAccountFolderNames(mAccount, folders2);
|
||
|
checkServerFolderNames("folders2", "DRAFTS_1", "SENT_2", "Trash", "Outbox");
|
||
|
|
||
|
// Replace one entry, check that "other" is ignored, check that Outbox is ignored
|
||
|
Folder[] folders3 = new Folder[] {
|
||
|
new MyMockFolder(Folder.FolderRole.OTHER, "OTHER_3a"),
|
||
|
new MyMockFolder(Folder.FolderRole.TRASH, "TRASH_3"),
|
||
|
new MyMockFolder(Folder.FolderRole.OTHER, "OTHER_3b"),
|
||
|
new MyMockFolder(Folder.FolderRole.OUTBOX, "OUTBOX_3"),
|
||
|
};
|
||
|
MessagingController.updateAccountFolderNames(mAccount, folders3);
|
||
|
checkServerFolderNames("folders2", "DRAFTS_1", "SENT_2", "TRASH_3", "Outbox");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Quickly check all four folder name slots in mAccount
|
||
|
*/
|
||
|
private void checkServerFolderNames(String diagnostic,
|
||
|
String drafts, String sent, String trash, String outbox) {
|
||
|
assertEquals(diagnostic, drafts, mAccount.getDraftsFolderName());
|
||
|
assertEquals(diagnostic, sent, mAccount.getSentFolderName());
|
||
|
assertEquals(diagnostic, trash, mAccount.getTrashFolderName());
|
||
|
assertEquals(diagnostic, outbox, mAccount.getOutboxFolderName());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* MockFolder allows setting and retrieving role & name
|
||
|
*/
|
||
|
private static class MyMockFolder extends MockFolder {
|
||
|
private FolderRole mRole;
|
||
|
private String mName;
|
||
|
|
||
|
public MyMockFolder(FolderRole role, String name) {
|
||
|
mRole = role;
|
||
|
mName = name;
|
||
|
}
|
||
|
|
||
|
public String getName() {
|
||
|
return mName;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public FolderRole getRole() {
|
||
|
return mRole;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a dummy account with minimal fields
|
||
|
*/
|
||
|
private void createTestAccount() {
|
||
|
mAccount = new Account(getContext());
|
||
|
mAccount.save(mPreferences);
|
||
|
|
||
|
mUuid = mAccount.getUuid();
|
||
|
}
|
||
|
|
||
|
}
|