slint/tests/cases/elements/image.slint
Olivier Goffart a6a142f9c8 C++: fix a bug in which a substraction could overflow
There was an image like this:
```slint
height: 40px
Image {
     width: parent.width;
     height: self.source.height * 1px;
     source: @image-url("....");
}
```

The `y` propery ended inlined like so:
`(40 - the_image.source.get().size().height)/float(2)`
but since height was `unsigned` the C++ rules means that the operation
happens as unsigned and we have an overflow. and `y` turned out to be
very big instead of slightly negative (for image whose height was larger
than 40)
2025-03-05 08:25:45 +01:00

67 lines
2 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//include_path: ../../../demos/printerdemo/ui/images/
TestCase := Rectangle {
img := Image {
source: @image-url("cat.jpg");
}
img2 := Image {
source: @image-url("cat.jpg");
source-clip-x: 20;
}
img3 := Image {
source: @image-url("image.slint");
}
out property <image> with-border: @image-url("dog.jpg", nine-slice(12 13 14 15));
property <length> img_width: img.width;
property <length> img_height: img.height;
in-out property <float> test_no_overflow: (21 - img.source.width) / 2; // (21 - 320)/2 = -149.5
property <bool> test: img2.source-clip-height * 1px == img2.height && img2.source-clip-width * 1px == img2.width &&
img2.width/1px == img2.source.width - 20 && img3.source.width == 0 && img3.source.height == 0 && test_no_overflow == -149.5;
}
/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert_eq(instance.get_img_width(), 320.);
assert_eq(instance.get_img_height(), 480.);
assert(instance.get_test());
auto img_search = slint::testing::ElementHandle::find_by_element_id(handle, "TestCase::img");
assert_eq(img_search.size(), 1);
auto img = img_search[0];
assert(*img.accessible_role() == slint::testing::AccessibleRole::Image);
```
```rust
use i_slint_backend_testing::AccessibleRole;
let instance = TestCase::new().unwrap();
assert_eq!(instance.get_img_width(), 320.);
assert_eq!(instance.get_img_height(), 480.);
assert!(instance.get_test());
let mut img_search = slint_testing::ElementHandle::find_by_element_id(&instance, "TestCase::img");
let img = img_search.next().unwrap();
assert_eq!(img.accessible_role(), Some(AccessibleRole::Image));
```
```js
var instance = new slint.TestCase();
assert.equal(instance.img_width, 320);
assert.equal(instance.img_height, 480);
assert(instance.test);
```
*/