Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2008 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.mms.util;
     18 
     19 import java.nio.IntBuffer;
     20 
     21 import android.graphics.Bitmap;
     22 import android.graphics.drawable.BitmapDrawable;
     23 import android.test.AndroidTestCase;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 import android.text.SpannableStringBuilder;
     26 import android.text.style.ImageSpan;
     27 
     28 import com.android.internal.widget.Smileys;
     29 
     30 /**
     31  * This is a series of unit tests for the SmileyParser class.
     32  *
     33  * This is just unit tests of the SmileyParser - the activity is not instantiated
     34  */
     35 @SmallTest
     36 public class SmileyParserUnitTests extends AndroidTestCase {
     37 
     38     @Override
     39     protected void setUp() throws Exception {
     40         super.setUp();
     41 
     42         SmileyParser.init(getContext());
     43     }
     44 
     45     /**
     46      * Test of smiley strings.
     47      */
     48     public void testSmileyParser() {
     49         SmileyParser parser = SmileyParser.getInstance();
     50         SpannableStringBuilder buf = new SpannableStringBuilder();
     51 
     52         // Put a string that looks kind of like a smiley in between two valid smileys.
     53         buf.append(parser.addSmileySpans(":-):-:-("));
     54 
     55         ImageSpan[] spans = buf.getSpans(0, buf.length(), ImageSpan.class);
     56 
     57         assertTrue("Smiley (happy) bitmaps aren't equal",
     58                 compareImageSpans(new ImageSpan(mContext,
     59                         Smileys.getSmileyResource(Smileys.HAPPY)), spans[0]));
     60 
     61         assertTrue("Smiley (sad) bitmaps aren't equal",
     62                 compareImageSpans(new ImageSpan(mContext,
     63                         Smileys.getSmileyResource(Smileys.SAD)), spans[1]));
     64     }
     65 
     66     private boolean compareImageSpans(ImageSpan span1, ImageSpan span2) {
     67         BitmapDrawable bitmapDrawable1 = (BitmapDrawable)span1.getDrawable();
     68         BitmapDrawable bitmapDrawable2 = (BitmapDrawable)span2.getDrawable();
     69         Bitmap bitmap1 = bitmapDrawable1.getBitmap();
     70         Bitmap bitmap2 = bitmapDrawable2.getBitmap();
     71 
     72         int rowBytes1 = bitmap1.getRowBytes();
     73         int rowBytes2 = bitmap2.getRowBytes();
     74         if (rowBytes1 != rowBytes2) {
     75             return false;
     76         }
     77         int height1 = bitmap1.getHeight();
     78         int height2 = bitmap2.getHeight();
     79         if (height1 != height2) {
     80             return false;
     81         }
     82         int size = height1 * rowBytes1;
     83         int[] intArray1 = new int[size];
     84         int[] intArray2 = new int[size];
     85         IntBuffer intBuf1 = IntBuffer.wrap(intArray1);
     86         IntBuffer intBuf2 = IntBuffer.wrap(intArray2);
     87 
     88         bitmap1.copyPixelsToBuffer(intBuf1);
     89         bitmap2.copyPixelsToBuffer(intBuf2);
     90 
     91         for (int i = 0; i < size; i++) {
     92             if (intArray1[i] != intArray2[i]) {
     93                 return false;
     94             }
     95         }
     96         return true;
     97     }
     98 
     99 }
    100