Skip to content

Commit

Permalink
fix: NativeImage.getScaleFactors returns correct scales (#25903)
Browse files Browse the repository at this point in the history
  • Loading branch information
trop[bot] committed Oct 13, 2020
1 parent 1c83779 commit 3793cfc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
20 changes: 13 additions & 7 deletions shell/common/api/electron_api_native_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ gfx::Size NativeImage::GetSize(const base::Optional<float> scale_factor) {

std::vector<float> NativeImage::GetScaleFactors() {
gfx::ImageSkia image_skia = image_.AsImageSkia();
return image_skia.GetSupportedScales();
std::vector<float> 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<float> scale_factor) {
Expand Down Expand Up @@ -386,18 +390,20 @@ gin::Handle<NativeImage> NativeImage::Create(v8::Isolate* isolate,
gin::Handle<NativeImage> NativeImage::CreateFromPNG(v8::Isolate* isolate,
const char* buffer,
size_t length) {
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
reinterpret_cast<const unsigned char*>(buffer), length);
return Create(isolate, image);
gfx::ImageSkia image_skia;
electron::util::AddImageSkiaRepFromPNG(
&image_skia, reinterpret_cast<const unsigned char*>(buffer), length, 1.0);
return Create(isolate, gfx::Image(image_skia));
}

// static
gin::Handle<NativeImage> NativeImage::CreateFromJPEG(v8::Isolate* isolate,
const char* buffer,
size_t length) {
gfx::Image image = gfx::ImageFrom1xJPEGEncodedData(
reinterpret_cast<const unsigned char*>(buffer), length);
return Create(isolate, image);
gfx::ImageSkia image_skia;
electron::util::AddImageSkiaRepFromJPEG(
&image_skia, reinterpret_cast<const unsigned char*>(buffer), length, 1.0);
return Create(isolate, gfx::Image(image_skia));
}

// static
Expand Down
8 changes: 3 additions & 5 deletions spec-main/api-remote-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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', () => {
Expand Down
9 changes: 9 additions & 0 deletions spec/api-native-image-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,23 +539,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 });

Expand Down

0 comments on commit 3793cfc

Please sign in to comment.