Commit 5106918e authored by Csoregi Natalia's avatar Csoregi Natalia
Browse files

Backed out changeset d0ff30e1d830 (bug 1650692) for toolchains failures on...

Backed out changeset d0ff30e1d830 (bug 1650692) for toolchains failures on ImageDecoderTest.kt. CLOSED TREE
parent 98305641
Loading
Loading
Loading
Loading
−1.69 KiB
Loading image diff...
+0 −123
Original line number Diff line number Diff line
package org.mozilla.geckoview.test

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.filters.MediumTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.geckoview.GeckoResult

@MediumTest
@RunWith(AndroidJUnit4::class)
class ImageDecoderTest {

    private fun compareBitmap(expectedLocation: String, actual: Bitmap) {
        val stream = InstrumentationRegistry.getInstrumentation().targetContext.assets
                .open(expectedLocation)

        val expected = BitmapFactory.decodeStream(stream)
        for (x in 0 until actual.height) {
            for (y in 0 until actual.width) {
                assertEquals(expected.getPixel(x, y), actual.getPixel(x, y))
            }
        }
    }

    @Test(expected = IllegalArgumentException::class)
    fun decodeNullUri() {
        ImageDecoder.instance().decode(null)
    }

    @Test
    fun decodeIconSvg() {
        val svgNatural = GeckoResult<Void>()
        val svg100 = GeckoResult<Void>()

        ImageDecoder.instance().decode("web_extensions/actions/button/icon.svg").accept { actual ->
            assertEquals(500, actual!!.width)
            assertEquals(500, actual.height)
            svgNatural.complete(null)
        }

        ImageDecoder.instance().decode("web_extensions/actions/button/icon.svg", 100).accept { actual ->
            assertEquals(100, actual!!.width)
            assertEquals(100, actual.height)
            compareBitmap("web_extensions/actions/button/expected.png", actual)
            svg100.complete(null)
        }

        UiThreadUtils.waitForResult(svgNatural)
        UiThreadUtils.waitForResult(svg100)
    }

    @Test
    fun decodeIconPng() {
        val pngNatural = GeckoResult<Void>()
        val png100 = GeckoResult<Void>()
        val png38 = GeckoResult<Void>()
        val png19 = GeckoResult<Void>()

        ImageDecoder.instance().decode("web_extensions/actions/button/geo-19.png").accept { actual ->
            compareBitmap("web_extensions/actions/button/geo-19.png", actual!!)
            pngNatural.complete(null)
        }

        // Raster images shouldn't be upscaled
        ImageDecoder.instance().decode("web_extensions/actions/button/geo-38.png", 100).accept { actual ->
            compareBitmap("web_extensions/actions/button/geo-38.png", actual!!)
            png100.complete(null)
        }

        ImageDecoder.instance().decode("web_extensions/actions/button/geo-38.png", 38).accept { actual ->
            compareBitmap("web_extensions/actions/button/geo-38.png", actual!!)
            png38.complete(null)
        }

        ImageDecoder.instance().decode("web_extensions/actions/button/geo-19.png", 19).accept { actual ->
            compareBitmap("web_extensions/actions/button/geo-19.png", actual!!)
            png19.complete(null)
        }

        UiThreadUtils.waitForResult(pngNatural)
        UiThreadUtils.waitForResult(png100)
        UiThreadUtils.waitForResult(png38)
        UiThreadUtils.waitForResult(png19)
    }

    @Test
    fun decodeIconPngDownscale() {
        val pngNatural = GeckoResult<Void>()
        val png16 = GeckoResult<Void>()

        ImageDecoder.instance().decode("web_extensions/actions/button/red.png").accept { actual ->
            assertEquals(32, actual!!.width)
            assertEquals(32, actual.height)

            for (x in 0 until actual.height) {
                for (y in 0 until actual.width) {
                    assertEquals(Color.RED, actual.getPixel(x, y))
                }
            }
            pngNatural.complete(null)
        }

        ImageDecoder.instance().decode("web_extensions/actions/button/red.png", 16).accept { actual ->
            assertEquals(16, actual!!.width)
            assertEquals(16, actual.height)

            for (x in 0 until actual.height) {
                for (y in 0 until actual.width) {
                    assertEquals(Color.RED, actual.getPixel(x, y))
                }
            }
            png16.complete(null)
        }

        UiThreadUtils.waitForResult(pngNatural)
        UiThreadUtils.waitForResult(png16)
    }
}
+8 −12
Original line number Diff line number Diff line
@@ -16,11 +16,6 @@ import org.mozilla.gecko.annotation.WrapForJNI;

    private ImageDecoder() {}

    /**
     * Gets the singleton instance of this class.
     *
     * @return the singleton {@link ImageDecoder} instance
     */
    public static ImageDecoder instance() {
        if (instance == null) {
            instance = new ImageDecoder();
@@ -30,7 +25,7 @@ import org.mozilla.gecko.annotation.WrapForJNI;
    }

    @WrapForJNI(dispatchTo = "gecko", stubName = "Decode")
    private static native void nativeDecode(final String uri, final int maxSize,
    private static native void nativeDecode(final String uri, final int desiredLength,
                                            GeckoResult<Bitmap> result);

    /**
@@ -60,13 +55,14 @@ import org.mozilla.gecko.annotation.WrapForJNI;
     *
     *            e.g. if the image file is locate at /assets/test.png inside the apk, set the uri
     *            to resource://android/assets/test.png.
     * @param maxSize Longest size for the image in device pixel units. The resulting image
     * @param desiredLength Longest size for the image in device pixel units. The resulting image
     *                      might be slightly different if the image cannot be resized efficiently.
     *                If maxSize is 0 then the image will be decoded to its natural size.
     *                      If desiredLength is 0 then the image will be decoded to its natural
     *                      size.
     * @return A {@link GeckoResult} to the decoded image.
     */
    @NonNull
    public GeckoResult<Bitmap> decode(final @NonNull String uri, final int maxSize) {
    public GeckoResult<Bitmap> decode(final @NonNull String uri, final int desiredLength) {
        if (uri == null) {
            throw new IllegalArgumentException("Uri cannot be null");
        }
@@ -74,10 +70,10 @@ import org.mozilla.gecko.annotation.WrapForJNI;
        final GeckoResult<Bitmap> result = new GeckoResult<>();

        if (GeckoThread.isStateAtLeast(GeckoThread.State.PROFILE_READY)) {
            nativeDecode(uri, maxSize, result);
            nativeDecode(uri, desiredLength, result);
        } else {
            GeckoThread.queueNativeCallUntil(GeckoThread.State.PROFILE_READY, this,
                    "nativeDecode", String.class, uri, int.class, maxSize,
                    "nativeDecode", String.class, uri, int.class, desiredLength,
                    GeckoResult.class, result);
        }

+12 −13
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@ class ImageCallbackHelper : public imgIContainerCallback,
    gDecodeRequests.remove(this);
  }

  ImageCallbackHelper(java::GeckoResult::Param aResult, int32_t aMaxSize)
      : mResult(aResult), mMaxSize(aMaxSize), mImage(nullptr) {
  ImageCallbackHelper(java::GeckoResult::Param aResult, int32_t aDesiredLength)
      : mResult(aResult), mDesiredLength(aDesiredLength), mImage(nullptr) {
    MOZ_ASSERT(mResult);
  }

@@ -63,9 +63,8 @@ class ImageCallbackHelper : public imgIContainerCallback,
    }

    mImage = aImage;
    return mImage->StartDecoding(imgIContainer::FLAG_SYNC_DECODE |
                                     imgIContainer::FLAG_ASYNC_NOTIFY |
                                     imgIContainer::FLAG_HIGH_QUALITY_SCALING,
    return mImage->StartDecoding(
        imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY,
        imgIContainer::FRAME_FIRST);
  }

@@ -73,11 +72,11 @@ class ImageCallbackHelper : public imgIContainerCallback,
  nsresult SendBitmap() {
    RefPtr<gfx::SourceSurface> surface;

    if (mMaxSize > 0) {
    if (mDesiredLength > 0) {
      surface = mImage->GetFrameAtSize(
          gfx::IntSize(mMaxSize, mMaxSize), imgIContainer::FRAME_FIRST,
          imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY |
              imgIContainer::FLAG_HIGH_QUALITY_SCALING);
          gfx::IntSize(mDesiredLength, mDesiredLength),
          imgIContainer::FRAME_FIRST,
          imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
    } else {
      surface = mImage->GetFrame(
          imgIContainer::FRAME_FIRST,
@@ -126,7 +125,7 @@ class ImageCallbackHelper : public imgIContainerCallback,

 private:
  const java::GeckoResult::GlobalRef mResult;
  int32_t mMaxSize;
  int32_t mDesiredLength;
  nsCOMPtr<imgIContainer> mImage;
  virtual ~ImageCallbackHelper() {}
};
@@ -137,11 +136,11 @@ NS_IMPL_ISUPPORTS(ImageCallbackHelper, imgIContainerCallback,
}  // namespace

/* static */ void ImageDecoderSupport::Decode(jni::String::Param aUri,
                                              int32_t aMaxSize,
                                              int32_t aDesiredLength,
                                              jni::Object::Param aResult) {
  auto result = java::GeckoResult::LocalRef(aResult);
  RefPtr<ImageCallbackHelper> helper =
      new ImageCallbackHelper(result, aMaxSize);
      new ImageCallbackHelper(result, aDesiredLength);

  nsresult rv = DecodeInternal(aUri->ToString(), helper, helper);
  if (NS_FAILED(rv)) {
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ namespace widget {
class ImageDecoderSupport final
    : public java::ImageDecoder::Natives<ImageDecoderSupport> {
 public:
  static void Decode(jni::String::Param aUri, int32_t aMaxSize,
  static void Decode(jni::String::Param aUri, int32_t aDesiredLength,
                     jni::Object::Param aResult);

 private:
+1 −1

File changed.

Contains only whitespace changes.

Loading