require "test/unit"
require "phi"

class TCMenu < Test::Unit::TestCase
  def test_ancestors
    assert_equal Phi::Component, Phi::Menu.superclass
  end

  def test_1
    form = Phi::Form.new :form1
    menu = Phi.new_menu(form, :menu1, [
      item1 = Phi.new_item('&File', '', :mi_file1).add(
        item11 = Phi.new_item('&Close', '', :mi_close1)
      )
    ])

    assert_instance_of Phi::MainMenu, menu
    assert_instance_of Phi::MenuItem, item1

    assert_respond_to form, :menu1

    assert_respond_to form, :menu
    # form.menuはobjectを生成しない
    assert_equal form.menu.object_id, menu.object_id

    assert_respond_to menu, :items
    items = menu.items
    assert_instance_of Phi::MenuItem, items
    assert_respond_to items, :mi_file1
    assert_equal item1, items.mi_file1

    # menu.itemsはobjectを生成しない
    assert_equal menu.items.object_id, items.object_id
    assert_respond_to item1, :mi_close1
    assert_equal item11, item1.mi_close1
  end

  def test_new_sub_menu
    form = Phi::Form.new :form1
    menu = Phi.new_menu(form, :menu1, [
      item1 = Phi.new_item('&File', '', :mi_file1).add(
        item10 = Phi.new_sub_menu('&Option', :mi_option1, [
          item11 = Phi.new_item('&Close', '', :mi_close1)
        ])
      )
    ])

    assert_instance_of Phi::MainMenu, menu
    assert_instance_of Phi::MenuItem, item1

    assert_respond_to form, :menu1
    assert_equal form, menu.parent
    assert_equal form, item1.parent
    assert_equal form, item10.parent
    assert_equal form, item11.parent
  end

  def test_new_popup_menu
    form = Phi::Form.new :form1
    menu = Phi.new_popup_menu(form, :menu1, [
      item1 = Phi.new_item('&File', '', :mi_file1).add(
        item11 = Phi.new_item('&Close', '', :mi_close1)
      )
    ])

    assert_instance_of Phi::PopupMenu, menu
    assert_instance_of Phi::MenuItem, item1

    assert_respond_to form, :menu1
    assert_equal form, menu.parent
  end
end
