Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2013 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.ide.eclipse.adt.internal.editors.draw9patch.graphics;
     18 
     19 import java.util.Arrays;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import org.eclipse.swt.graphics.Image;
     24 import org.eclipse.swt.graphics.ImageData;
     25 import org.eclipse.swt.widgets.Display;
     26 
     27 public class GraphicsUtilitiesTest extends TestCase {
     28     private static final int MASK_ALPHA = 0xFF000000;
     29 
     30     private static final String DIR = "/com/android/ide/eclipse/testdata/draw9patch/";
     31 
     32     public void testConvertToNinePatchNull() throws Exception {
     33         ImageData result = GraphicsUtilities.convertToNinePatch(null);
     34         assertNull(result);
     35     }
     36 
     37     public void testConvertToNinePatch() throws Exception {
     38         String fileName = DIR + "no-patched.png";
     39         Image image = new Image(Display.getDefault(),
     40                 getClass().getResourceAsStream(fileName));
     41         ImageData baseData = image.getImageData();
     42 
     43         ImageData result = GraphicsUtilities.convertToNinePatch(baseData);
     44 
     45         assertEquals(baseData.width + 2, result.width);
     46         assertEquals(baseData.height + 2, result.height);
     47 
     48         // horizontal
     49         for (int x = 0; x < result.width; x++) {
     50 
     51             // top row
     52             assertEquals(0x0, result.getPixel(x, 0) & MASK_ALPHA);
     53 
     54             // bottom row
     55             assertEquals(0x0, result.getPixel(x, result.height - 1) & MASK_ALPHA);
     56         }
     57 
     58         // vertical
     59         for (int y = 0; y < result.height; y++) {
     60 
     61             // left column
     62             assertEquals(0x0, result.getPixel(0, y) & MASK_ALPHA);
     63 
     64             // right column
     65             assertEquals(0x0, result.getPixel(result.width - 1, y) & MASK_ALPHA);
     66         }
     67     }
     68 
     69     public void testClearImageDataNull() throws Exception {
     70         try {
     71             GraphicsUtilities.clearImageData(null);
     72             fail();
     73         } catch (IllegalArgumentException e) {
     74         }
     75     }
     76 
     77     public void testClearImageData() throws Exception {
     78         String fileName = DIR + "no-patched.png";
     79         Image image = new Image(Display.getDefault(),
     80                 getClass().getResourceAsStream(fileName));
     81 
     82         ImageData baseData = image.getImageData();
     83         GraphicsUtilities.clearImageData(baseData);
     84         for (int y = 0; y < baseData.height; y++) {
     85             for (int x = 0; x < baseData.width; x++) {
     86                 assertEquals(0x000000, baseData.getPixel(x, y));
     87                 assertEquals(0x00, baseData.getAlpha(x, y));
     88             }
     89         }
     90 
     91     }
     92 
     93     public void testCopyNull() throws Exception {
     94         ImageData result = GraphicsUtilities.copy(null);
     95         assertNull(result);
     96     }
     97 
     98     public void testCopy() throws Exception {
     99         String fileName = DIR + "no-patched.png";
    100         Image image = new Image(Display.getDefault(),
    101                 getClass().getResourceAsStream(fileName));
    102 
    103         ImageData baseData = image.getImageData();
    104         ImageData copiedData = GraphicsUtilities.copy(baseData);
    105 
    106         assertEquals(baseData.width, copiedData.width);
    107         assertEquals(baseData.height, copiedData.height);
    108         assertEquals(baseData.depth, copiedData.depth);
    109         assertEquals(baseData.transparentPixel, copiedData.transparentPixel);
    110         assertEquals(baseData.alpha, copiedData.alpha);
    111         assertTrue(baseData.palette.equals(copiedData.palette));
    112 
    113         final int[] baseColors = new int[baseData.width];
    114         final byte[] baseAlpha = new byte[baseData.width];
    115 
    116         final int[] copiedColors = new int[copiedData.width];
    117         final byte[] copiedAlpha = new byte[copiedData.width];
    118 
    119         for (int y = 0; y < baseData.height; y++) {
    120 
    121             baseData.getPixels(0, y, baseData.width, baseColors, 0);
    122             baseData.getPixels(0, y, baseData.width, copiedColors, 0);
    123             assertTrue(Arrays.equals(baseColors, copiedColors));
    124 
    125             baseData.getAlphas(0, y, baseData.width, baseAlpha, 0);
    126             baseData.getAlphas(0, y, baseData.width, copiedAlpha, 0);
    127             assertTrue(Arrays.equals(baseAlpha, copiedAlpha));
    128 
    129         }
    130     }
    131 
    132     public void testGetVerticalPixelsIllegalArgument() throws Exception {
    133         String fileName = DIR + "no-patched.png";
    134         Image image = new Image(Display.getDefault(),
    135                 getClass().getResourceAsStream(fileName));
    136 
    137         ImageData baseData = image.getImageData();
    138         int[] temp = new int[baseData.width];
    139 
    140         // data must not be null
    141         try {
    142             GraphicsUtilities.getVerticalPixels(null, 0, 0, 1, temp);
    143             fail();
    144         } catch (IllegalArgumentException e) {
    145         }
    146 
    147         // out must not be null
    148         try {
    149             GraphicsUtilities.getVerticalPixels(baseData, 0, 0, 1, null);
    150             fail();
    151         } catch (IllegalArgumentException e) {
    152         }
    153 
    154         // out length must be > height
    155         try {
    156             GraphicsUtilities.getVerticalPixels(baseData, 0, 0, 1, new int[0]);
    157             fail();
    158         } catch (IllegalArgumentException e) {
    159         }
    160 
    161         // x must be > 0
    162         try {
    163             GraphicsUtilities.getVerticalPixels(baseData, -1, 0, 1, temp);
    164             fail();
    165         } catch (IllegalArgumentException e) {
    166         }
    167 
    168         // y must be > 0
    169         try {
    170             GraphicsUtilities.getVerticalPixels(baseData, 0, -1, 1, temp);
    171             fail();
    172         } catch (IllegalArgumentException e) {
    173         }
    174 
    175         // height must be >= 0
    176         try {
    177             GraphicsUtilities.getVerticalPixels(baseData, 0, 0, 0, temp);
    178             fail();
    179         } catch (IllegalArgumentException e) {
    180         }
    181 
    182         // argument x must be < data.width
    183         try {
    184             GraphicsUtilities.getVerticalPixels(baseData, baseData.width, 0, baseData.height, temp);
    185             fail();
    186         } catch (IllegalArgumentException e) {
    187         }
    188 
    189         // argument y must be < data.height
    190         try {
    191             GraphicsUtilities
    192                     .getVerticalPixels(baseData, 0, baseData.height, baseData.height, temp);
    193             fail();
    194         } catch (IllegalArgumentException e) {
    195         }
    196 
    197         // argument height must be > (y + data.height)
    198         try {
    199             GraphicsUtilities.getVerticalPixels(baseData, 0, 1, baseData.height, temp);
    200             fail();
    201         } catch (IllegalArgumentException e) {
    202         }
    203 
    204     }
    205 
    206     public void testGetVerticalPixels() throws Exception {
    207         String fileName = DIR + "no-patched.png";
    208         Image image = new Image(Display.getDefault(),
    209                 getClass().getResourceAsStream(fileName));
    210 
    211         ImageData baseData = image.getImageData();
    212         int[] temp = new int[baseData.width];
    213 
    214         GraphicsUtilities.getVerticalPixels(baseData, 0, 0, baseData.height, temp);
    215 
    216         int height = baseData.height;
    217         for (int y = 0; y < height; y++) {
    218             assertEquals(baseData.getPixel(0, y), temp[y]);
    219         }
    220     }
    221 
    222     public void testGetHorizontalPixelsIllegalArgument() throws Exception {
    223         String fileName = DIR + "no-patched.png";
    224         Image image = new Image(Display.getDefault(),
    225                 getClass().getResourceAsStream(fileName));
    226 
    227         ImageData baseData = image.getImageData();
    228         int[] temp = new int[baseData.width];
    229 
    230         // data must not be null
    231         try {
    232             GraphicsUtilities.getHorizontalPixels(null, 0, 0, 1, temp);
    233             fail();
    234         } catch (IllegalArgumentException e) {
    235         }
    236 
    237         // out must not be null
    238         try {
    239             GraphicsUtilities.getHorizontalPixels(baseData, 0, 0, 1, null);
    240             fail();
    241         } catch (IllegalArgumentException e) {
    242         }
    243 
    244         // out length must be > width
    245         try {
    246             GraphicsUtilities.getHorizontalPixels(baseData, 0, 0, 1, new int[0]);
    247             fail();
    248         } catch (IllegalArgumentException e) {
    249         }
    250 
    251         // x must be > 0
    252         try {
    253             GraphicsUtilities.getHorizontalPixels(baseData, -1, 0, 1, temp);
    254             fail();
    255         } catch (IllegalArgumentException e) {
    256         }
    257 
    258         // y must be > 0
    259         try {
    260             GraphicsUtilities.getHorizontalPixels(baseData, 0, -1, 1, temp);
    261             fail();
    262         } catch (IllegalArgumentException e) {
    263         }
    264 
    265         // width must be >= 0
    266         try {
    267             GraphicsUtilities.getHorizontalPixels(baseData, 0, 0, 0, temp);
    268             fail();
    269         } catch (IllegalArgumentException e) {
    270         }
    271 
    272         // argument x must be < data.width
    273         try {
    274             GraphicsUtilities
    275                     .getHorizontalPixels(baseData, baseData.width, 0, baseData.width, temp);
    276             fail();
    277         } catch (IllegalArgumentException e) {
    278         }
    279 
    280         // argument y must be < data.height
    281         try {
    282             GraphicsUtilities
    283                     .getHorizontalPixels(baseData, 0, baseData.height, baseData.width, temp);
    284             fail();
    285         } catch (IllegalArgumentException e) {
    286         }
    287 
    288         // argument width must be > (x + data.width)
    289         try {
    290             GraphicsUtilities.getHorizontalPixels(baseData, 1, 0, baseData.width, temp);
    291             fail();
    292         } catch (IllegalArgumentException e) {
    293         }
    294 
    295     }
    296 
    297     public void testGetHorizontalPixels() throws Exception {
    298         String fileName = DIR + "no-patched.png";
    299         Image image = new Image(Display.getDefault(),
    300                 getClass().getResourceAsStream(fileName));
    301 
    302         ImageData baseData = image.getImageData();
    303         int[] temp = new int[baseData.width];
    304 
    305         GraphicsUtilities.getHorizontalPixels(baseData, 0, 0, baseData.width, temp);
    306 
    307         int width = baseData.width;
    308         for (int x = 0; x < width; x++) {
    309             assertEquals(baseData.getPixel(x, 0), temp[x]);
    310         }
    311     }
    312 
    313 }
    314