From 83a542382357665cd05a66a39736c4de701c8827 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Thu, 8 Oct 2020 14:10:58 -0700 Subject: [PATCH 1/2] fix: NativeImage.getScaleFactors returns correct scales --- shell/common/api/electron_api_native_image.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index 8159d0a062616..7163d42436ab2 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -264,7 +264,11 @@ gfx::Size NativeImage::GetSize(const base::Optional scale_factor) { std::vector NativeImage::GetScaleFactors() { gfx::ImageSkia image_skia = image_.AsImageSkia(); - return image_skia.GetSupportedScales(); + std::vector scale_factors; + for (const auto& rep : image_skia.image_reps()) { + scale_factors.push_back(rep.scale()); + } + return scale_factors; } float NativeImage::GetAspectRatio(const base::Optional scale_factor) { From f51402e29a9648a7b3c0b364f18ae1f2727e9168 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Thu, 8 Oct 2020 14:31:47 -0700 Subject: [PATCH 2/2] fix tests --- shell/common/api/electron_api_native_image.cc | 14 ++++++++------ spec-main/api-remote-spec.ts | 8 +++----- spec/api-native-image-spec.js | 9 +++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index 7163d42436ab2..ebe5414a12a93 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -387,18 +387,20 @@ gin::Handle NativeImage::Create(v8::Isolate* isolate, gin::Handle NativeImage::CreateFromPNG(v8::Isolate* isolate, const char* buffer, size_t length) { - gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( - reinterpret_cast(buffer), length); - return Create(isolate, image); + gfx::ImageSkia image_skia; + electron::util::AddImageSkiaRepFromPNG( + &image_skia, reinterpret_cast(buffer), length, 1.0); + return Create(isolate, gfx::Image(image_skia)); } // static gin::Handle NativeImage::CreateFromJPEG(v8::Isolate* isolate, const char* buffer, size_t length) { - gfx::Image image = gfx::ImageFrom1xJPEGEncodedData( - reinterpret_cast(buffer), length); - return Create(isolate, image); + gfx::ImageSkia image_skia; + electron::util::AddImageSkiaRepFromJPEG( + &image_skia, reinterpret_cast(buffer), length, 1.0); + return Create(isolate, gfx::Image(image_skia)); } // static diff --git a/spec-main/api-remote-spec.ts b/spec-main/api-remote-spec.ts index c9201d58c58ad..0b5d4e8603cd5 100644 --- a/spec-main/api-remote-spec.ts +++ b/spec-main/api-remote-spec.ts @@ -107,7 +107,7 @@ describe('typeUtils serialization/deserialization', () => { expect(nonEmpty.isEmpty()).to.be.false(); expect(nonEmpty.getAspectRatio()).to.equal(1); expect(nonEmpty.toDataURL()).to.not.be.empty(); - expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL); + expect(nonEmpty.toBitmap({ scaleFactor: 1.0 })).to.deep.equal(image.toBitmap({ scaleFactor: 1.0 })); expect(nonEmpty.getSize()).to.deep.equal({ width: 2, height: 2 }); expect(nonEmpty.getBitmap()).to.not.be.empty(); expect(nonEmpty.toPNG()).to.not.be.empty(); @@ -132,14 +132,12 @@ describe('typeUtils serialization/deserialization', () => { expect(nonEmpty.getBitmap({ scaleFactor: 1.0 })).to.not.be.empty(); expect(nonEmpty.getBitmap({ scaleFactor: 2.0 })).to.not.be.empty(); expect(nonEmpty.toBitmap()).to.not.be.empty(); - expect(nonEmpty.toBitmap({ scaleFactor: 1.0 })).to.not.be.empty(); - expect(nonEmpty.toBitmap({ scaleFactor: 2.0 })).to.not.be.empty(); + expect(nonEmpty.toBitmap({ scaleFactor: 1.0 })).to.deep.equal(image.toBitmap({ scaleFactor: 1.0 })); + expect(nonEmpty.toBitmap({ scaleFactor: 2.0 })).to.deep.equal(image.toBitmap({ scaleFactor: 2.0 })); expect(nonEmpty.toPNG()).to.not.be.empty(); expect(nonEmpty.toPNG({ scaleFactor: 1.0 })).to.not.be.empty(); expect(nonEmpty.toPNG({ scaleFactor: 2.0 })).to.not.be.empty(); expect(nonEmpty.toDataURL()).to.not.be.empty(); - expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL1); - expect(nonEmpty.toDataURL({ scaleFactor: 2.0 })).to.equal(dataURL2); }); it('serializes and deserializes an Array', () => { diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index 8ef1964a0289c..71adbfc334308 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -569,23 +569,32 @@ describe('nativeImage module', () => { buffer: nativeImage.createFromPath(imageDataOne.path).toPNG() }); + expect(image.getScaleFactors()).to.deep.equal([1]); + const imageDataTwo = getImage({ width: 2, height: 2 }); image.addRepresentation({ scaleFactor: 2.0, buffer: nativeImage.createFromPath(imageDataTwo.path).toPNG() }); + expect(image.getScaleFactors()).to.deep.equal([1, 2]); + const imageDataThree = getImage({ width: 3, height: 3 }); image.addRepresentation({ scaleFactor: 3.0, buffer: nativeImage.createFromPath(imageDataThree.path).toPNG() }); + expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]); + image.addRepresentation({ scaleFactor: 4.0, buffer: 'invalid' }); + // this one failed, so it shouldn't show up in the scale factors + expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]); + expect(image.isEmpty()).to.be.false(); expect(image.getSize()).to.deep.equal({ width: 1, height: 1 });