Browse Source

Merge pull request #11255 from electron/fix-11245

Enable nativeImage module addRepresentation() tests
Zeke Sikelianos 7 years ago
parent
commit
aab35073ee
2 changed files with 21 additions and 7 deletions
  1. 14 1
      atom/common/api/atom_api_native_image.cc
  2. 7 6
      spec/api-native-image-spec.js

+ 14 - 1
atom/common/api/atom_api_native_image.cc

@@ -17,6 +17,8 @@
 #include "base/strings/string_util.h"
 #include "native_mate/object_template_builder.h"
 #include "net/base/data_url.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkImageInfo.h"
 #include "third_party/skia/include/core/SkPixelRef.h"
 #include "ui/base/layout.h"
 #include "ui/base/webui/web_ui_util.h"
@@ -93,9 +95,20 @@ bool AddImageSkiaRep(gfx::ImageSkia* image,
   std::unique_ptr<SkBitmap> decoded(new SkBitmap());
 
   // Try PNG first.
-  if (!gfx::PNGCodec::Decode(data, size, decoded.get()))
+  if (!gfx::PNGCodec::Decode(data, size, decoded.get())) {
     // Try JPEG.
     decoded = gfx::JPEGCodec::Decode(data, size);
+    if (decoded) {
+      // `JPEGCodec::Decode()` doesn't tell `SkBitmap` instance it creates
+      // that all of its pixels are opaque, that's why the bitmap gets
+      // an alpha type `kPremul_SkAlphaType` instead of `kOpaque_SkAlphaType`.
+      // Let's fix it here.
+      // TODO(alexeykuzmin): This workaround should be removed
+      // when the `JPEGCodec::Decode()` code is fixed.
+      // See https://github.com/electron/electron/issues/11294.
+      decoded->setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
+    }
+  }
 
   if (!decoded) {
     // Try Bitmap

+ 7 - 6
spec/api-native-image-spec.js

@@ -31,7 +31,7 @@ describe('nativeImage module', () => {
       width: 1
     },
     {
-      dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFUlEQVQYlWP8////fwYGBgYmBigAAD34BABBrq9BAAAAAElFTkSuQmCC',
+      dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==',
       filename: '2x2.jpg',
       format: ImageFormat.JPEG,
       hasAlphaChannel: false,
@@ -183,6 +183,7 @@ describe('nativeImage module', () => {
         expect(imageFromDataUrl.getSize()).to.deep.equal(imageFromPath.getSize())
         expect(imageFromDataUrl.toBitmap()).to.satisfy(
             bitmap => imageFromPath.toBitmap().equals(bitmap))
+        expect(imageFromDataUrl.toDataURL()).to.equal(imageFromPath.toDataURL())
       }
     })
   })
@@ -193,8 +194,10 @@ describe('nativeImage module', () => {
       for (const imageData of imagesData) {
         const imageFromPath = nativeImage.createFromPath(imageData.path)
 
-        expect(imageFromPath.toDataURL()).to.equal(imageData.dataUrl)
-        expect(imageFromPath.toDataURL({scaleFactor: 2.0})).to.equal(imageData.dataUrl)
+        const scaleFactors = [1.0, 2.0]
+        for (const scaleFactor of scaleFactors) {
+          expect(imageFromPath.toDataURL({scaleFactor})).to.equal(imageData.dataUrl)
+        }
       }
     })
 
@@ -444,9 +447,7 @@ describe('nativeImage module', () => {
     })
   })
 
-  // TODO(alexeykuzmin): Disabled during Chromium 61 upgrade.
-  // Fix them and enable.
-  xdescribe('addRepresentation()', () => {
+  describe('addRepresentation()', () => {
     it('supports adding a buffer representation for a scale factor', () => {
       const image = nativeImage.createEmpty()