1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email.mail.store.imap; 18 19 import static com.android.email.mail.store.imap.ImapTestUtils.*; 20 21 import com.android.email.mail.store.imap.ImapConstants; 22 import com.android.email.mail.store.imap.ImapResponse; 23 import com.android.email.mail.store.imap.ImapSimpleString; 24 25 import android.test.suitebuilder.annotation.SmallTest; 26 27 import junit.framework.TestCase; 28 29 @SmallTest 30 public class ImapResponseTest extends TestCase { 31 32 public void testIsTagged() { 33 assertTrue(buildResponse("a", false).isTagged()); 34 assertFalse(buildResponse(null, false).isTagged()); 35 } 36 37 public void testIsOk() { 38 assertTrue(buildResponse(null, false, new ImapSimpleString("OK")).isOk()); 39 assertFalse(buildResponse(null, false, new ImapSimpleString("NO")).isOk()); 40 } 41 42 public void testIsDataResponse() { 43 final ImapResponse OK = buildResponse("tag", false, new ImapSimpleString("OK")); 44 final ImapResponse SEARCH = buildResponse(null, false, new ImapSimpleString("SEARCH"), 45 new ImapSimpleString("1")); 46 final ImapResponse EXISTS = buildResponse(null, false, new ImapSimpleString("3"), 47 new ImapSimpleString("EXISTS")); 48 49 final ImapResponse TAGGED_EXISTS = buildResponse("tag", false, new ImapSimpleString("1"), 50 new ImapSimpleString("EXISTS")); 51 52 assertTrue(SEARCH.isDataResponse(0, ImapConstants.SEARCH)); 53 assertTrue(EXISTS.isDataResponse(1, ImapConstants.EXISTS)); 54 55 // Falses... 56 assertFalse(SEARCH.isDataResponse(1, ImapConstants.SEARCH)); 57 assertFalse(EXISTS.isDataResponse(0, ImapConstants.EXISTS)); 58 59 assertFalse(EXISTS.isDataResponse(1, ImapConstants.FETCH)); 60 61 // It's tagged, so can't be a data response 62 assertFalse(TAGGED_EXISTS.isDataResponse(1, ImapConstants.EXISTS)); 63 } 64 65 public void testGetResponseCodeOrEmpty() { 66 assertEquals( 67 "rescode", 68 buildResponse("tag", false, 69 new ImapSimpleString("OK"), 70 buildList(new ImapSimpleString("rescode")) 71 ).getResponseCodeOrEmpty().getString() 72 ); 73 74 assertEquals( 75 "", 76 buildResponse("tag", false, 77 new ImapSimpleString("STATUS"), // Not a status response 78 buildList(new ImapSimpleString("rescode")) 79 ).getResponseCodeOrEmpty().getString() 80 ); 81 82 assertEquals( 83 "", 84 buildResponse("tag", false, 85 new ImapSimpleString("OK"), 86 new ImapSimpleString("XXX"), // Second element not a list. 87 buildList(new ImapSimpleString("rescode")) 88 ).getResponseCodeOrEmpty().getString() 89 ); 90 } 91 92 public void testGetAlertTextOrEmpty() { 93 assertEquals( 94 "alert text", 95 buildResponse("tag", false, 96 new ImapSimpleString("OK"), 97 buildList(new ImapSimpleString("ALERT")), 98 new ImapSimpleString("alert text") 99 ).getAlertTextOrEmpty().getString() 100 ); 101 102 // Not alert 103 assertEquals( 104 "", 105 buildResponse("tag", false, 106 new ImapSimpleString("OK"), 107 buildList(new ImapSimpleString("X")), 108 new ImapSimpleString("alert text") 109 ).getAlertTextOrEmpty().getString() 110 ); 111 } 112 113 public void testGetStatusResponseTextOrEmpty() { 114 // Not a status response 115 assertEquals( 116 "", 117 buildResponse("tag", false, 118 new ImapSimpleString("XXX"), 119 new ImapSimpleString("!text!") 120 ).getStatusResponseTextOrEmpty().getString() 121 ); 122 123 // Second element isn't a list. 124 assertEquals( 125 "!text!", 126 buildResponse("tag", false, 127 new ImapSimpleString("OK"), 128 new ImapSimpleString("!text!") 129 ).getStatusResponseTextOrEmpty().getString() 130 ); 131 132 // Second element is a list. 133 assertEquals( 134 "!text!", 135 buildResponse("tag", false, 136 new ImapSimpleString("OK"), 137 buildList(new ImapSimpleString("XXX")), 138 new ImapSimpleString("!text!") 139 ).getStatusResponseTextOrEmpty().getString() 140 ); 141 } 142 } 143