1/* 2 * Copyright (C) 2009 Marc Blank 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.exchange; 19 20import com.android.email.provider.EmailContent.Account; 21 22import org.apache.http.Header; 23import org.apache.http.client.methods.HttpPost; 24import org.apache.http.client.methods.HttpRequestBase; 25 26import android.content.Context; 27import android.test.AndroidTestCase; 28 29import java.io.File; 30import java.io.IOException; 31 32/** 33 * You can run this entire test case with: 34 * runtest -c com.android.exchange.EasSyncServiceTests email 35 */ 36 public class EasSyncServiceTests extends AndroidTestCase { 37 Context mMockContext; 38 39 @Override 40 public void setUp() throws Exception { 41 super.setUp(); 42 mMockContext = getContext(); 43 } 44 45 /** 46 * Test that our unique file name algorithm works as expected. 47 * @throws IOException 48 */ 49 public void testCreateUniqueFile() throws IOException { 50 // Delete existing files, if they exist 51 EasSyncService svc = new EasSyncService(); 52 svc.mContext = mMockContext; 53 try { 54 String fileName = "A11achm3n1.doc"; 55 File uniqueFile = svc.createUniqueFileInternal(null, fileName); 56 assertEquals(fileName, uniqueFile.getName()); 57 if (uniqueFile.createNewFile()) { 58 uniqueFile = svc.createUniqueFileInternal(null, fileName); 59 assertEquals("A11achm3n1-2.doc", uniqueFile.getName()); 60 if (uniqueFile.createNewFile()) { 61 uniqueFile = svc.createUniqueFileInternal(null, fileName); 62 assertEquals("A11achm3n1-3.doc", uniqueFile.getName()); 63 } 64 } 65 fileName = "A11achm3n1"; 66 uniqueFile = svc.createUniqueFileInternal(null, fileName); 67 assertEquals(fileName, uniqueFile.getName()); 68 if (uniqueFile.createNewFile()) { 69 uniqueFile = svc.createUniqueFileInternal(null, fileName); 70 assertEquals("A11achm3n1-2", uniqueFile.getName()); 71 } 72 } finally { 73 // These are the files that should be created earlier in the test. Make sure 74 // they are deleted for the next go-around 75 File directory = getContext().getFilesDir(); 76 String[] fileNames = new String[] {"A11achm3n1.doc", "A11achm3n1-2.doc", "A11achm3n1"}; 77 int length = fileNames.length; 78 for (int i = 0; i < length; i++) { 79 File file = new File(directory, fileNames[i]); 80 if (file.exists()) { 81 file.delete(); 82 } 83 } 84 } 85 } 86 87 public void testResetHeartbeats() { 88 EasSyncService svc = new EasSyncService(); 89 // Test case in which the minimum and force heartbeats need to come up 90 svc.mPingMaxHeartbeat = 1000; 91 svc.mPingMinHeartbeat = 200; 92 svc.mPingHeartbeat = 300; 93 svc.mPingForceHeartbeat = 100; 94 svc.mPingHeartbeatDropped = true; 95 svc.resetHeartbeats(400); 96 assertEquals(400, svc.mPingMinHeartbeat); 97 assertEquals(1000, svc.mPingMaxHeartbeat); 98 assertEquals(400, svc.mPingHeartbeat); 99 assertEquals(400, svc.mPingForceHeartbeat); 100 assertFalse(svc.mPingHeartbeatDropped); 101 102 // Test case in which the force heartbeat needs to come up 103 svc.mPingMaxHeartbeat = 1000; 104 svc.mPingMinHeartbeat = 200; 105 svc.mPingHeartbeat = 100; 106 svc.mPingForceHeartbeat = 100; 107 svc.mPingHeartbeatDropped = true; 108 svc.resetHeartbeats(150); 109 assertEquals(200, svc.mPingMinHeartbeat); 110 assertEquals(1000, svc.mPingMaxHeartbeat); 111 assertEquals(150, svc.mPingHeartbeat); 112 assertEquals(150, svc.mPingForceHeartbeat); 113 assertFalse(svc.mPingHeartbeatDropped); 114 115 // Test case in which the maximum needs to come down 116 svc.mPingMaxHeartbeat = 1000; 117 svc.mPingMinHeartbeat = 200; 118 svc.mPingHeartbeat = 800; 119 svc.mPingForceHeartbeat = 100; 120 svc.mPingHeartbeatDropped = true; 121 svc.resetHeartbeats(600); 122 assertEquals(200, svc.mPingMinHeartbeat); 123 assertEquals(600, svc.mPingMaxHeartbeat); 124 assertEquals(600, svc.mPingHeartbeat); 125 assertEquals(100, svc.mPingForceHeartbeat); 126 assertFalse(svc.mPingHeartbeatDropped); 127 } 128 129 public void testAddHeaders() { 130 HttpRequestBase method = new HttpPost(); 131 EasSyncService svc = new EasSyncService(); 132 svc.mAuthString = "auth"; 133 svc.mProtocolVersion = "12.1"; 134 svc.mDeviceType = "android"; 135 svc.mAccount = null; 136 // With second argument false, there should be no header 137 svc.setHeaders(method, false); 138 Header[] headers = method.getHeaders("X-MS-PolicyKey"); 139 assertEquals(0, headers.length); 140 // With second argument true, there should always be a header 141 // The value will be "0" without an account 142 method.removeHeaders("X-MS-PolicyKey"); 143 svc.setHeaders(method, true); 144 headers = method.getHeaders("X-MS-PolicyKey"); 145 assertEquals(1, headers.length); 146 assertEquals("0", headers[0].getValue()); 147 // With an account, but null security key, the header's value should be "0" 148 Account account = new Account(); 149 account.mSecuritySyncKey = null; 150 svc.mAccount = account; 151 method.removeHeaders("X-MS-PolicyKey"); 152 svc.setHeaders(method, true); 153 headers = method.getHeaders("X-MS-PolicyKey"); 154 assertEquals(1, headers.length); 155 assertEquals("0", headers[0].getValue()); 156 // With an account and security key, the header's value should be the security key 157 account.mSecuritySyncKey = "key"; 158 svc.mAccount = account; 159 method.removeHeaders("X-MS-PolicyKey"); 160 svc.setHeaders(method, true); 161 headers = method.getHeaders("X-MS-PolicyKey"); 162 assertEquals(1, headers.length); 163 assertEquals("key", headers[0].getValue()); 164 } 165} 166