fix: NativeImage.getScaleFactors returns correct scales (#25832)

* fix: NativeImage.getScaleFactors returns correct scales

* fix tests
This commit is contained in:
Jeremy Rose 2020-10-12 18:59:18 -07:00 committed by GitHub
parent 9cd882f1fa
commit cbe751d349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 12 deletions

View file

@ -270,7 +270,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) {
@ -389,18 +393,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

View file

@ -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', () => {

View file

@ -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 });