|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Parse, LLC. All rights reserved. |
| 3 | + * |
| 4 | + * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, |
| 5 | + * copy, modify, and distribute this software in source code or binary form for use |
| 6 | + * in connection with the web services and APIs provided by Parse. |
| 7 | + * |
| 8 | + * As with any software that integrates with the Parse platform, your use of |
| 9 | + * this software is subject to the Parse Terms of Service |
| 10 | + * [https://www.parse.com/about/terms]. This copyright notice shall be |
| 11 | + * included in all copies or substantial portions of the software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package com.parse; |
| 23 | + |
| 24 | +import android.content.Context; |
| 25 | +import android.graphics.Bitmap; |
| 26 | +import android.graphics.drawable.BitmapDrawable; |
| 27 | +import android.graphics.drawable.ColorDrawable; |
| 28 | +import android.graphics.drawable.Drawable; |
| 29 | +import android.test.InstrumentationTestCase; |
| 30 | + |
| 31 | +import com.parse.ui.test.R; |
| 32 | + |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.util.Arrays; |
| 35 | + |
| 36 | +public class ParseImageViewTest extends InstrumentationTestCase { |
| 37 | + |
| 38 | + public void testParseImageViewWithNullParseFile() throws Exception { |
| 39 | + final Drawable drawable = new ColorDrawable(); |
| 40 | + final ParseImageView imageView = new ParseImageView(getInstrumentation().getTargetContext()); |
| 41 | + imageView.setPlaceholder(drawable); |
| 42 | + imageView.setParseFile(null); |
| 43 | + |
| 44 | + byte[] data = ParseTaskUtils.wait(imageView.loadInBackground()); |
| 45 | + |
| 46 | + assertNull(data); |
| 47 | + assertEquals(drawable, imageView.getDrawable()); |
| 48 | + } |
| 49 | + |
| 50 | + public void testParseImageViewWithNotImageParseFile() throws Exception { |
| 51 | + byte[] data = "hello".getBytes(); |
| 52 | + ParseFile file = new ParseFile(data); |
| 53 | + |
| 54 | + final Drawable drawable = new ColorDrawable(); |
| 55 | + final ParseImageView imageView = new ParseImageView(getInstrumentation().getTargetContext()); |
| 56 | + imageView.setPlaceholder(drawable); |
| 57 | + imageView.setParseFile(file); |
| 58 | + |
| 59 | + byte[] dataAgain = ParseTaskUtils.wait(imageView.loadInBackground()); |
| 60 | + |
| 61 | + assertTrue(Arrays.equals(data, dataAgain)); |
| 62 | + // Since the parseFile can not be decode as an image, the getDrawable should not be changed |
| 63 | + assertEquals(drawable, imageView.getDrawable()); |
| 64 | + } |
| 65 | + |
| 66 | + public void testParseImageViewWithImageParseFile() throws Exception { |
| 67 | + Context context = getInstrumentation().getTargetContext(); |
| 68 | + final Drawable iconImage = context.getResources().getDrawable(R.drawable.icon); |
| 69 | + Bitmap iconBitmap = ((BitmapDrawable) iconImage).getBitmap(); |
| 70 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 71 | + iconBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); |
| 72 | + final byte[] imageData = stream.toByteArray(); |
| 73 | + ParseFile file = new ParseFile(imageData); |
| 74 | + |
| 75 | + final Drawable drawable = new ColorDrawable(); |
| 76 | + final ParseImageView imageView = new ParseImageView(context); |
| 77 | + imageView.setPlaceholder(drawable); |
| 78 | + imageView.setParseFile(file); |
| 79 | + |
| 80 | + byte[] dataAgain = ParseTaskUtils.wait(imageView.loadInBackground()); |
| 81 | + |
| 82 | + assertEquals(imageData, dataAgain); |
| 83 | + assertNotNull(imageView.getDrawable()); |
| 84 | + // It is hard to assert whether the two images are equal or not, so we just verify the image has |
| 85 | + // been changed |
| 86 | + assertNotSame(drawable, imageView.getDrawable()); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
0 commit comments