12337918599721780b39b7f286eaf6893a2902b8eMarc Blank/*
22337918599721780b39b7f286eaf6893a2902b8eMarc Blank * Copyright (C) 2011 The Android Open Source Project
32337918599721780b39b7f286eaf6893a2902b8eMarc Blank *
42337918599721780b39b7f286eaf6893a2902b8eMarc Blank * Licensed under the Apache License, Version 2.0 (the "License");
52337918599721780b39b7f286eaf6893a2902b8eMarc Blank * you may not use this file except in compliance with the License.
62337918599721780b39b7f286eaf6893a2902b8eMarc Blank * You may obtain a copy of the License at
72337918599721780b39b7f286eaf6893a2902b8eMarc Blank *
82337918599721780b39b7f286eaf6893a2902b8eMarc Blank *      http://www.apache.org/licenses/LICENSE-2.0
92337918599721780b39b7f286eaf6893a2902b8eMarc Blank *
102337918599721780b39b7f286eaf6893a2902b8eMarc Blank * Unless required by applicable law or agreed to in writing, software
112337918599721780b39b7f286eaf6893a2902b8eMarc Blank * distributed under the License is distributed on an "AS IS" BASIS,
122337918599721780b39b7f286eaf6893a2902b8eMarc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132337918599721780b39b7f286eaf6893a2902b8eMarc Blank * See the License for the specific language governing permissions and
142337918599721780b39b7f286eaf6893a2902b8eMarc Blank * limitations under the License.
152337918599721780b39b7f286eaf6893a2902b8eMarc Blank */
162337918599721780b39b7f286eaf6893a2902b8eMarc Blank
172337918599721780b39b7f286eaf6893a2902b8eMarc Blankpackage com.android.exchange.adapter;
182337918599721780b39b7f286eaf6893a2902b8eMarc Blank
19cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blankimport android.content.ContentValues;
202337918599721780b39b7f286eaf6893a2902b8eMarc Blankimport android.test.AndroidTestCase;
212337918599721780b39b7f286eaf6893a2902b8eMarc Blankimport android.test.MoreAsserts;
222337918599721780b39b7f286eaf6893a2902b8eMarc Blank
232337918599721780b39b7f286eaf6893a2902b8eMarc Blankimport java.io.ByteArrayInputStream;
242337918599721780b39b7f286eaf6893a2902b8eMarc Blankimport java.io.IOException;
252337918599721780b39b7f286eaf6893a2902b8eMarc Blank
262337918599721780b39b7f286eaf6893a2902b8eMarc Blank/** You can run this entire test case with:
272337918599721780b39b7f286eaf6893a2902b8eMarc Blank *   runtest -c com.android.exchange.adapter.SerializerTests exchange
282337918599721780b39b7f286eaf6893a2902b8eMarc Blank */
292337918599721780b39b7f286eaf6893a2902b8eMarc Blankpublic class SerializerTests extends AndroidTestCase {
302337918599721780b39b7f286eaf6893a2902b8eMarc Blank
312337918599721780b39b7f286eaf6893a2902b8eMarc Blank    private static final byte[] BYTE_ARRAY = new byte[] {1, 2, 3, 4, 5};
322337918599721780b39b7f286eaf6893a2902b8eMarc Blank    private static final int BYTE_ARRAY_LENGTH = 5;
33cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank    private static final String ID = "ID";
34cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank    private static final String KEY = "Key";
352337918599721780b39b7f286eaf6893a2902b8eMarc Blank
362337918599721780b39b7f286eaf6893a2902b8eMarc Blank    // Basic test for use of start, end, tag, data, opaque, and done
372337918599721780b39b7f286eaf6893a2902b8eMarc Blank    public void testSerializer() throws IOException {
38cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        ContentValues values = new ContentValues();
392337918599721780b39b7f286eaf6893a2902b8eMarc Blank        // Create a test stream
402337918599721780b39b7f286eaf6893a2902b8eMarc Blank        Serializer s = new Serializer();
412337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.start(Tags.COMPOSE_SEND_MAIL);
42cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank
43cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        // Test writeStringValue without and with data
44cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID);
45cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        values.put(KEY, ID);
46cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        s.writeStringValue(values, KEY, Tags.COMPOSE_ACCOUNT_ID);
47cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank
48cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank        s.data(Tags.COMPOSE_CLIENT_ID, ID);
492337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.tag(Tags.COMPOSE_SAVE_IN_SENT_ITEMS);
502337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.start(Tags.COMPOSE_MIME);
512337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.opaque(new ByteArrayInputStream(BYTE_ARRAY), BYTE_ARRAY_LENGTH);
522337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.end();  // COMPOSE_MIME
532337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.end();  // COMPOSE_SEND_MAIL
542337918599721780b39b7f286eaf6893a2902b8eMarc Blank        s.done(); // DOCUMENT
552337918599721780b39b7f286eaf6893a2902b8eMarc Blank        // Get the bytes for the stream
562337918599721780b39b7f286eaf6893a2902b8eMarc Blank        byte[] bytes = s.toByteArray();
572337918599721780b39b7f286eaf6893a2902b8eMarc Blank        // These are the expected bytes (self-explanatory)
582337918599721780b39b7f286eaf6893a2902b8eMarc Blank        byte[] expectedBytes = new byte[] {
592337918599721780b39b7f286eaf6893a2902b8eMarc Blank                3,      // Version 1.3
602337918599721780b39b7f286eaf6893a2902b8eMarc Blank                1,      // unknown or missing public identifier
612337918599721780b39b7f286eaf6893a2902b8eMarc Blank                106,    // UTF-8
622337918599721780b39b7f286eaf6893a2902b8eMarc Blank                0,      // String array length
632337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Wbxml.SWITCH_PAGE,
642337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Tags.COMPOSE,
652337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Tags.COMPOSE_SEND_MAIL - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
66cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE,
67cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                Tags.COMPOSE_ACCOUNT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
68cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                Wbxml.STR_I,    // 0-terminated string
69cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                (byte)ID.charAt(0),
70cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                (byte)ID.charAt(1),
71cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                0,
72cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                Wbxml.END,  // COMPOSE_ACCOUNT_ID
732337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Tags.COMPOSE_CLIENT_ID - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
742337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Wbxml.STR_I,    // 0-terminated string
75cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                (byte)ID.charAt(0),
76cb4d1b9a2b55a986fb5eef7397b13c4cdb812f88Marc Blank                (byte)ID.charAt(1),
772337918599721780b39b7f286eaf6893a2902b8eMarc Blank                0,
782337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Wbxml.END,  // COMPOSE_CLIENT_ID
792337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Tags.COMPOSE_SAVE_IN_SENT_ITEMS - Tags.COMPOSE_PAGE,
802337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Tags.COMPOSE_MIME - Tags.COMPOSE_PAGE + Wbxml.WITH_CONTENT,
812337918599721780b39b7f286eaf6893a2902b8eMarc Blank                (byte)Wbxml.OPAQUE,
822337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY_LENGTH,
832337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY[0],
842337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY[1],
852337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY[2],
862337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY[3],
872337918599721780b39b7f286eaf6893a2902b8eMarc Blank                BYTE_ARRAY[4],
882337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Wbxml.END,  // COMPOSE_MIME
892337918599721780b39b7f286eaf6893a2902b8eMarc Blank                Wbxml.END   // COMPOSE_SEND_MAIL
902337918599721780b39b7f286eaf6893a2902b8eMarc Blank         };
912337918599721780b39b7f286eaf6893a2902b8eMarc Blank        // Make sure we get what's expected
922337918599721780b39b7f286eaf6893a2902b8eMarc Blank        MoreAsserts.assertEquals("Serializer mismatch", bytes, expectedBytes);
932337918599721780b39b7f286eaf6893a2902b8eMarc Blank    }
942337918599721780b39b7f286eaf6893a2902b8eMarc Blank}
95