Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2014 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.emailcommon.provider;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import org.json.JSONException;
     23 import org.json.JSONObject;
     24 
     25 @SmallTest
     26 public class AccountTest extends AndroidTestCase {
     27 
     28     public void testDeserializeFromJSON() throws JSONException {
     29         final JSONObject json = new JSONObject();
     30         json.put(EmailContent.AccountColumns.DISPLAY_NAME, "David Hasselhoff");
     31         json.put(EmailContent.AccountColumns.EMAIL_ADDRESS, "dhoff (at) example.com");
     32         json.put(EmailContent.AccountColumns.SYNC_LOOKBACK, 42);
     33         json.put(EmailContent.AccountColumns.SYNC_INTERVAL, 99);
     34         json.put(Account.JSON_TAG_HOST_AUTH_RECV, getHostAuthJSON("receiver", "recpass").toJson());
     35         json.put(Account.JSON_TAG_HOST_AUTH_SEND, getHostAuthJSON("send", "sendpass").toJson());
     36         json.put(EmailContent.AccountColumns.FLAGS, 22);
     37         json.put(EmailContent.AccountColumns.SENDER_NAME, "Friend of Kitt");
     38         json.put(EmailContent.AccountColumns.PROTOCOL_VERSION, "protocol version 3.14");
     39         json.put(EmailContent.AccountColumns.SIGNATURE, "David with a heart over the i");
     40         json.put(EmailContent.AccountColumns.PING_DURATION, 77);
     41 
     42         // deserialize the json
     43         final Account a = Account.fromJson(json);
     44 
     45         // verify that all fields deserialized as expected
     46         assertEquals("David Hasselhoff", a.getDisplayName());
     47         assertEquals("dhoff (at) example.com", a.getEmailAddress());
     48         assertEquals(42, a.getSyncLookback());
     49         assertEquals(99, a.getSyncInterval());
     50         assertEquals("receiver", a.mHostAuthRecv.mLogin);
     51         assertEquals("recpass", a.mHostAuthRecv.mPassword);
     52         assertEquals("send", a.mHostAuthSend.mLogin);
     53         assertEquals("sendpass", a.mHostAuthSend.mPassword);
     54         assertEquals(22, a.getFlags());
     55         assertEquals("Friend of Kitt", a.getSenderName());
     56         assertEquals("protocol version 3.14", a.mProtocolVersion);
     57         assertEquals("David with a heart over the i", a.getSignature());
     58         assertEquals(77, a.mPingDuration);
     59     }
     60 
     61     /**
     62      * A factory method to generate HostAuth values that allow us to serialize the Account object.
     63      * See {@link HostAuthTests} for tests that exercise serialization of HostAuth.
     64      *
     65      * @param username a given username
     66      * @param password a given password
     67      * @return a HostAuth that includes the given username and password
     68      */
     69     private static HostAuth getHostAuthJSON(String username, String password) {
     70         final HostAuth ha = new HostAuth();
     71         ha.setLogin(username, password);
     72         ha.mProtocol = "IMAP";
     73         ha.mAddress = "dhoff (at) example.com";
     74         ha.mPort = 543;
     75         ha.mFlags = 777;
     76         return ha;
     77     }
     78 
     79     public void testSerializeAndDeserializeWithJSON() {
     80         // build an Account object with all fields set
     81         final Account before = new Account();
     82         before.setDisplayName("David Hasselhoff");
     83         before.setEmailAddress("dhoff (at) example.com");
     84         before.mSyncKey = "syncKey";
     85         before.setSyncLookback(42);
     86         before.setSyncInterval(99);
     87         before.setFlags(1 << 5);
     88         before.setSenderName("Friend of Kitt");
     89         before.mProtocolVersion = "protocol version 3.14";
     90         before.mSecuritySyncKey = "securitySyncKey";
     91         before.setSignature("David with a heart over the i");
     92         before.mPolicyKey = 66;
     93         before.mPingDuration = 77;
     94         before.mHostAuthRecv = getHostAuthJSON("receiver", "recpass");
     95 
     96         // this must be called before serialization occurs
     97         before.ensureLoaded(getContext());
     98 
     99         // serialize and deserialize
    100         final Account after = Account.fromJson(before.toJson());
    101 
    102         assertEquals(before.getDisplayName(), after.getDisplayName());
    103         assertEquals(before.getEmailAddress(), after.getEmailAddress());
    104         assertEquals(before.getSyncLookback(), after.getSyncLookback());
    105         assertEquals(before.getSyncInterval(), after.getSyncInterval());
    106         assertEquals(before.mHostAuthSend, after.mHostAuthSend);
    107         assertEquals(before.mHostAuthKeySend, after.mHostAuthKeySend);
    108         assertEquals(before.mHostAuthKeyRecv, after.mHostAuthKeyRecv);
    109         assertEquals(before.getFlags(), after.getFlags());
    110         assertEquals(before.getSenderName(), after.getSenderName());
    111         assertEquals(before.mProtocolVersion, after.mProtocolVersion);
    112         assertEquals(before.getSignature(), after.getSignature());
    113         assertEquals(before.mPingDuration, after.mPingDuration);
    114 
    115         assertNull(after.mSyncKey); // sync key is not serialized; field defaults to null
    116         assertEquals(0, after.mPolicyKey); // policy key is not serialized; field defaults to 0
    117     }
    118 }
    119