|
| 1 | +require "./spec_helper" |
| 2 | + |
| 3 | +private class LifetimeControl < UIng::Control |
| 4 | + @ref_ptr : Pointer(UIng::LibUI::Button) |
| 5 | + |
| 6 | + def initialize |
| 7 | + @ref_ptr = Pointer(UIng::LibUI::Button).null |
| 8 | + end |
| 9 | + |
| 10 | + def adopt(parent : UIng::Control) : Nil |
| 11 | + take_ownership(parent) |
| 12 | + end |
| 13 | + |
| 14 | + def mark_from_parent_destroy : Nil |
| 15 | + mark_released_from_parent_destroy |
| 16 | + end |
| 17 | + |
| 18 | + def mark_from_native_destroy : Nil |
| 19 | + mark_released_from_native_destroy |
| 20 | + end |
| 21 | + |
| 22 | + def to_unsafe |
| 23 | + ref_ptr |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +private class LifetimeContainer < LifetimeControl |
| 28 | + @child : UIng::Control? |
| 29 | + |
| 30 | + def initialize(@child : UIng::Control? = nil) |
| 31 | + super() |
| 32 | + end |
| 33 | + |
| 34 | + def adopt_child(child : LifetimeControl) : Nil |
| 35 | + @child = child |
| 36 | + child.adopt(self) |
| 37 | + end |
| 38 | + |
| 39 | + protected def before_destroy : Nil |
| 40 | + @child.try &.mark_released_from_parent_destroy |
| 41 | + @child = nil |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +describe UIng::Control do |
| 46 | + it "marks descendants released when a parent is destroyed natively" do |
| 47 | + parent = LifetimeContainer.new |
| 48 | + child = LifetimeContainer.new |
| 49 | + grandchild = LifetimeControl.new |
| 50 | + |
| 51 | + parent.adopt_child(child) |
| 52 | + child.adopt_child(grandchild) |
| 53 | + parent.mark_from_parent_destroy |
| 54 | + |
| 55 | + parent.released?.should be_true |
| 56 | + child.released?.should be_true |
| 57 | + grandchild.released?.should be_true |
| 58 | + child.parent.should be_nil |
| 59 | + grandchild.parent.should be_nil |
| 60 | + end |
| 61 | + |
| 62 | + it "rejects direct destroy while the control is owned by a parent" do |
| 63 | + parent = LifetimeContainer.new |
| 64 | + child = LifetimeControl.new |
| 65 | + parent.adopt_child(child) |
| 66 | + |
| 67 | + expect_raises(Exception, /destroy a child control directly/) do |
| 68 | + child.destroy |
| 69 | + end |
| 70 | + |
| 71 | + child.released?.should be_false |
| 72 | + child.parent.should eq(parent) |
| 73 | + end |
| 74 | + |
| 75 | + it "rejects checked pointer access after parent destroy marking" do |
| 76 | + child = LifetimeControl.new |
| 77 | + child.mark_from_parent_destroy |
| 78 | + |
| 79 | + expect_raises(Exception, /already been destroyed/) do |
| 80 | + child.to_unsafe |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + it "marks descendants released when native code destroys a root control" do |
| 85 | + root = LifetimeContainer.new |
| 86 | + child = LifetimeControl.new |
| 87 | + root.adopt_child(child) |
| 88 | + |
| 89 | + root.mark_from_native_destroy |
| 90 | + |
| 91 | + root.released?.should be_true |
| 92 | + child.released?.should be_true |
| 93 | + child.parent.should be_nil |
| 94 | + end |
| 95 | +end |
0 commit comments