Home | History | Annotate | Download | only in managedprovisioning
      1 /*
      2  * Copyright 2016, 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.managedprovisioning;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.Paint;
     25 import android.graphics.drawable.Drawable;
     26 import android.net.Uri;
     27 import android.test.AndroidTestCase;
     28 import android.test.suitebuilder.annotation.SmallTest;
     29 
     30 import java.io.ByteArrayInputStream;
     31 import java.io.ByteArrayOutputStream;
     32 import java.io.File;
     33 import java.io.FileOutputStream;
     34 import java.io.InputStream;
     35 import java.lang.Exception;
     36 
     37 @SmallTest
     38 public class LogoUtilsTest extends AndroidTestCase {
     39 
     40     private static final int SAMPLE_COLOR = Color.RED;
     41 
     42     public void testPartiallyResizedBitmap() throws Exception {
     43         Bitmap bitmap = createSampleBitmap(20, 30);
     44         File tempFile = writeBitmapToTempFile(bitmap);
     45         try {
     46             Bitmap newBitmap = LogoUtils.getBitmapPartiallyResized(tempFile.getPath(), 10, 15);
     47             // It should have been able to get only the half bitmap.
     48             assertEquals(10, newBitmap.getWidth());
     49             assertEquals(15, newBitmap.getHeight());
     50             assertEquals(SAMPLE_COLOR, newBitmap.getPixel(5, 5));
     51         } finally {
     52             tempFile.delete();
     53         }
     54     }
     55 
     56     public void testPartiallyResizedElongatedBitmap() throws Exception {
     57         Bitmap bitmap = createSampleBitmap(8, 32);
     58         File tempFile = writeBitmapToTempFile(bitmap);
     59         try {
     60             Bitmap newBitmap = LogoUtils.getBitmapPartiallyResized(tempFile.getPath(), 8, 8);
     61             // It should have been able to get only the quarter bitmap.
     62             assertEquals(2, newBitmap.getWidth());
     63             assertEquals(8, newBitmap.getHeight());
     64             assertEquals(SAMPLE_COLOR, newBitmap.getPixel(1, 1));
     65         } finally {
     66             tempFile.delete();
     67         }
     68     }
     69 
     70     public void testResizeBitmapKeepRatio() throws Exception {
     71         Bitmap bitmap = createSampleBitmap(16, 32);
     72         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 8, 8);
     73         // Should have kept the ratio.
     74         assertEquals(4, newBitmap.getWidth());
     75         assertEquals(8, newBitmap.getHeight());
     76         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
     77     }
     78 
     79     public void testResizeBitmapNoScalingNeeded() throws Exception {
     80         Bitmap bitmap = createSampleBitmap(16, 32);
     81         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 20, 40);
     82         // The maximum dimensions are larger than the actual ones: should
     83         // not have changed the dimensions.
     84         assertEquals(16, newBitmap.getWidth());
     85         assertEquals(32, newBitmap.getHeight());
     86         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
     87     }
     88 
     89     public void testResizeBitmapNoIntegerRatio() throws Exception {
     90         Bitmap bitmap = createSampleBitmap(15, 15);
     91         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 10, 10);
     92         assertEquals(10, newBitmap.getWidth());
     93         assertEquals(10, newBitmap.getHeight());
     94         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
     95     }
     96 
     97     public void testSaveGetOrganisationLogo() throws Exception {
     98         Bitmap bitmap = createSampleBitmap(7, 5);
     99         File tempFile = writeBitmapToTempFile(bitmap);
    100         try {
    101             LogoUtils.saveOrganisationLogo(getContext(), Uri.fromFile(tempFile));
    102             Drawable drawable = LogoUtils.getOrganisationLogo(getContext());
    103             // We should have the original drawable.
    104             assertEquals(7, drawable.getIntrinsicWidth());
    105             assertEquals(5, drawable.getIntrinsicHeight());
    106         } finally {
    107             LogoUtils.cleanUp(getContext());
    108         }
    109     }
    110 
    111     public void testDefaultOrganisationLogo() throws Exception {
    112         int maxWidth = (int) getContext().getResources().getDimension(R.dimen.max_logo_width);
    113         int maxHeight = (int) getContext().getResources().getDimension(R.dimen.max_logo_height);
    114         // In this test, we don't save the logo.
    115 
    116         // First let's compute the expected logo. It is the default one, resized if too big.
    117         Bitmap expected = BitmapFactory.decodeResource(getContext().getResources(),
    118                 R.drawable.briefcase_icon);
    119         expected = LogoUtils.resizeBitmap(expected, maxWidth, maxHeight);
    120 
    121         // Now, get the actual logo
    122         Drawable logo = LogoUtils.getOrganisationLogo(getContext());
    123 
    124         // They should be equal.
    125         assertBitmapEquals(expected, bitmapFromDrawable(logo));
    126     }
    127 
    128     private Bitmap createSampleBitmap(int width, int height) {
    129         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    130         Canvas canvas = new Canvas(bitmap);
    131         Paint paint = new Paint();
    132         paint.setColor(SAMPLE_COLOR);
    133         canvas.drawRect(0, 0, width, height, paint);
    134         return bitmap;
    135     }
    136 
    137     private void assertBitmapEquals(Bitmap b1, Bitmap b2) {
    138         assertEquals(b1.getWidth(), b2.getWidth());
    139         assertEquals(b1.getHeight(), b2.getHeight());
    140         for (int x = 0; x < b1.getWidth(); x++) {
    141             for (int y = 0; y < b1.getHeight(); y++) {
    142                 assertEquals(b1.getPixel(x, y), b2.getPixel(x, y));
    143             }
    144         }
    145     }
    146 
    147     private Bitmap bitmapFromDrawable(Drawable drawable) {
    148         int width = drawable.getIntrinsicWidth();
    149         int height = drawable.getIntrinsicHeight();
    150         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    151         Canvas canvas = new Canvas(bitmap);
    152         drawable.setBounds(0, 0, width, height);
    153         drawable.draw(canvas);
    154         return bitmap;
    155     }
    156 
    157     // Returns the temporary file where the bitmap was written.
    158     private File writeBitmapToTempFile(Bitmap bitmap) throws Exception {
    159         File tempFile = File.createTempFile("temp_bitmap", "", getContext().getCacheDir());
    160         FileOutputStream fos = new FileOutputStream(tempFile);
    161         bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
    162         fos.close();
    163         return tempFile;
    164     }
    165 }
    166