|
| 1 | +package scala.swing |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit |
| 4 | + |
| 5 | +import org.scalatest.{FlatSpec, Matchers} |
| 6 | + |
| 7 | +import scala.concurrent.duration.Duration |
| 8 | +import scala.concurrent.{Await, Future, Promise} |
| 9 | +import scala.swing.event.{UIElementHidden, UIElementMoved, UIElementResized, UIElementShown} |
| 10 | +import scala.util.control.NonFatal |
| 11 | + |
| 12 | +// Note: `AsyncFlatSpec` has issues with swallowing errors and returning early. |
| 13 | +class Issue97 extends FlatSpec with Matchers { |
| 14 | + case class Count(shown: Int = 0, hidden: Int = 0, moved: Int = 0, resized: Int = 0) |
| 15 | + |
| 16 | + def countEvents(): Future[Count] = { |
| 17 | + val p = Promise[Count]() |
| 18 | + |
| 19 | + def safely(thunk: => Unit): Unit = |
| 20 | + try { |
| 21 | + thunk |
| 22 | + } catch { |
| 23 | + case NonFatal(ex) => |
| 24 | + p.tryFailure(ex) |
| 25 | + } |
| 26 | + |
| 27 | + Swing.onEDT { |
| 28 | + safely { |
| 29 | + var c = Count() |
| 30 | + val lb = new Label("Foo") |
| 31 | + lb.listenTo(lb) |
| 32 | + lb.reactions += { |
| 33 | + case UIElementShown (`lb`) => c = c.copy(shown = c.shown + 1) |
| 34 | + case UIElementHidden (`lb`) => c = c.copy(hidden = c.hidden + 1) |
| 35 | + case UIElementMoved (`lb`) => c = c.copy(moved = c.moved + 1) |
| 36 | + case UIElementResized (`lb`) => c = c.copy(resized = c.resized + 1) |
| 37 | + } |
| 38 | + val b = new BoxPanel(Orientation.Horizontal) |
| 39 | + b.contents += lb |
| 40 | + lb.visible = false |
| 41 | + lb.visible = true |
| 42 | + b.contents.insert(0, new Label("Bar")) // about to move `lb` to the right |
| 43 | + b.peer.doLayout() |
| 44 | + // note: `Frame#pack()` creates a native window peer, |
| 45 | + // and thus is not possible to run on Travis without X11 |
| 46 | + |
| 47 | + // wait till next EDT cycle |
| 48 | + Swing.onEDT { |
| 49 | + p.trySuccess(c) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + p.future |
| 54 | + } |
| 55 | + |
| 56 | + "Components" should "fire exactly one event when moved, removed or made visible or invisible" in { |
| 57 | + val futCount = countEvents() |
| 58 | + val c = Await.result(futCount, Duration(20, TimeUnit.SECONDS)) |
| 59 | + assert(c.shown === 1) |
| 60 | + assert(c.hidden === 1) |
| 61 | + assert(c.moved === 1) |
| 62 | + assert(c.resized === 1) |
| 63 | + } |
| 64 | +} |
0 commit comments