1 /**
2  * Copyright (c) 2012, Google Inc.
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 package com.android.mail.providers;
17 
18 import android.content.Intent;
19 import android.os.Parcel;
20 import android.test.AndroidTestCase;
21 import android.test.suitebuilder.annotation.SmallTest;
22 
23 import com.android.mail.utils.Utils;
24 
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 
28 @SmallTest
29 public class AccountTests extends AndroidTestCase {
30 
testSerializeDeserialize()31     public void testSerializeDeserialize() {
32         final Parcel dest = Parcel.obtain();
33         dest.writeInt(0);
34         dest.writeString("accountUri");
35         dest.writeInt(12345);
36         dest.writeString("foldersList");
37         dest.writeString("searchUri");
38         dest.writeString("fromAddresses");
39         dest.writeString("expungeMessageUri");
40         dest.writeString("undoUri");
41         dest.writeString("settingIntentUri");
42         dest.writeInt(0);
43 
44         final Account before = Account.builder().buildFrom(dest, null);
45         final Intent intent = new Intent();
46         intent.putExtra(Utils.EXTRA_ACCOUNT, before);
47 
48         final Account after = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT);
49         assertNotNull(after);
50 
51         assertEquals(before.getEmailAddress(), after.getEmailAddress());
52         assertEquals(before.getDisplayName(), after.getDisplayName());
53         assertEquals(before.accountFromAddresses, after.accountFromAddresses);
54         assertEquals(before.capabilities, after.capabilities);
55         assertEquals(before.providerVersion, after.providerVersion);
56         assertEquals(before.uri, after.uri);
57         assertEquals(before.folderListUri, after.folderListUri);
58         assertEquals(before.searchUri, after.searchUri);
59         assertEquals(before.expungeMessageUri, after.expungeMessageUri);
60     }
61 
testDeserializeNullSenderNameUsingJSON()62     public void testDeserializeNullSenderNameUsingJSON() throws JSONException {
63         final JSONObject json = new JSONObject();
64         // fields required by deserialization
65         json.put(UIProvider.AccountColumns.NAME, "name");
66         json.put(UIProvider.AccountColumns.TYPE, "type");
67         json.put(UIProvider.AccountColumns.PROVIDER_VERSION, 1);
68         json.put(UIProvider.AccountColumns.CAPABILITIES, 2);
69 
70         // null sender name (same thing as not putting a sender name at all)
71         json.put(UIProvider.AccountColumns.SENDER_NAME, null);
72 
73         final Account account = Account.newInstance(json.toString());
74         assertNotNull(account);
75         assertNull(account.getSenderName());
76     }
77 }
78