Home | History | Annotate | Download | only in adapter
      1 /*
      2  * Copyright (C) 2011 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.exchange.adapter;
     18 
     19 import android.content.ContentValues;
     20 import android.test.AndroidTestCase;
     21 import android.test.MoreAsserts;
     22 
     23 import java.io.ByteArrayInputStream;
     24 import java.io.IOException;
     25 
     26 /** You can run this entire test case with:
     27  *   runtest -c com.android.exchange.adapter.SerializerTests exchange
     28  */
     29 public class SerializerTests extends AndroidTestCase {
     30 
     31     private static final byte[] BYTE_ARRAY = new byte[] {1, 2, 3, 4, 5};
     32     private static final int BYTE_ARRAY_LENGTH = 5;
     33     private static final String ID = "ID";
     34     private static final String KEY = "Key";
     35 
     36     // Basic test for use of start, end, tag, data, opaque, and done
     37     public void testSerializer() throws IOException {
     38         ContentValues values = new ContentValues();
     39         // Create a test stream
     40         Serializer s = new Serializer();
     41         s.start(Tags.COMPOSE_SEND_MAIL);
     42 
     43         // Test writeStringValue without and with data
     44         s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID);
     45         values.put(KEY, ID);
     46         s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID);
     47 
     48         s.data(Tags.COMPOSE_CLIENT_ID, ID);
     49         s.tag(Tags.COMPOSE_SAVE_IN_SENT_ITEMS);
     50         s.start(Tags.COMPOSE_MIME);
     51         s.opaque(new ByteArrayInputStream(BYTE_ARRAY), BYTE_ARRAY_LENGTH);
     52         s.end();  // COMPOSE_MIME
     53         s.end();  // COMPOSE_SEND_MAIL
     54         s.done(); // DOCUMENT
     55         // Get the bytes for the stream
     56         byte[] bytes = s.toByteArray();
     57         // These are the expected bytes (self-explanatory)
     58         byte[] expectedBytes = new byte[] {
     59                 3,      // Version 1.3
     60                 1,      // unknown or missing public identifier
     61                 106,    // UTF-8
     62                 0,      // String array length
     63                 Wbxml.SWITCH_PAGE,
     64                 Tags.COMPOSE,
     65                 Tags.COMPOSE_SEND_MAIL - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
     66                 Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE,
     67                 Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
     68                 Wbxml.STR_I,    // 0-terminated string
     69                 (byte)ID.charAt(0),
     70                 (byte)ID.charAt(1),
     71                 0,
     72                 Wbxml.END,  // COMPOSE_ACCOUNT_ID
     73                 Tags.COMPOSE_CLIENT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
     74                 Wbxml.STR_I,    // 0-terminated string
     75                 (byte)ID.charAt(0),
     76                 (byte)ID.charAt(1),
     77                 0,
     78                 Wbxml.END,  // COMPOSE_CLIENT_ID
     79                 Tags.COMPOSE_SAVE_IN_SENT_ITEMS - Tags.COMPOSE_PAGE,
     80                 Tags.COMPOSE_MIME - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
     81                 (byte)Wbxml.OPAQUE,
     82                 BYTE_ARRAY_LENGTH,
     83                 BYTE_ARRAY[0],
     84                 BYTE_ARRAY[1],
     85                 BYTE_ARRAY[2],
     86                 BYTE_ARRAY[3],
     87                 BYTE_ARRAY[4],
     88                 Wbxml.END,  // COMPOSE_MIME
     89                 Wbxml.END   // COMPOSE_SEND_MAIL
     90          };
     91         // Make sure we get what's expected
     92         MoreAsserts.assertEquals("Serializer mismatch", bytes, expectedBytes);
     93     }
     94 }
     95