local map = ...

-- #1490: if menu:on_mouse_released() starts a new menu, the original
-- menu:on_mouse_released() event used to get called a second time,
-- because starting a menu could reentrantly corrupt the reverse iteration
-- over the menus list.

local click_count = 0
local child_started_count = 0

local menu = {}

function menu:on_mouse_released(button, x, y)
  click_count = click_count + 1
  sol.menu.start(sol.main, {
    on_started = function(self)
      child_started_count = child_started_count + 1
    end
  })
end

function map:on_started()

  sol.menu.start(sol.main, menu)

  sol.input.simulate_mouse_pressed("left", 160, 120)
  sol.input.simulate_mouse_released("left", 160, 120)

  later(function()
    assert_equal(click_count, 1, "menu:on_mouse_released() was called an unexpected number of times")
    assert_equal(child_started_count, 1, "the menu started from on_mouse_released() was started an unexpected number of times")
    sol.main.exit()
  end)
end
