
$:.push(File::dirname($0))

load("test_prologue.rb")

class EDT_TestClass < TestBase

  # ObjectInstPath
  def test_1

    ly = RBA::Layout::new
    li = ly.insert_layer(RBA::LayerInfo::new(1, 0))
    tc = ly.create_cell("TOP")
    c2 = ly.create_cell("C2")
    shape = tc.shapes(li).insert(RBA::Box::new(-100, -200, 300, 400))
    inst = tc.insert(RBA::CellInstArray::new(c2.cell_index, RBA::Trans::new(10, 20)))

    p = RBA::ObjectInstPath::new
    p.cv_index = 17
    assert_equal(p.cv_index, 17)

    p.seq = 2
    assert_equal(p.seq, 2)

    assert_equal(p.is_cell_inst?, true)
    p.layer = -1
    assert_equal(p.is_cell_inst?, true)

    p.layer = 42
    assert_equal(p.layer, 42)
    assert_equal(p.is_cell_inst?, false)

    pp = p.dup
    assert_equal(pp == p, true)
    assert_equal(pp != p, false)
    assert_equal(pp < p, false)
    assert_equal(p < pp, false)

    p.shape = shape
    assert_equal(p.shape.to_s, "box (-100,-200;300,400)")
    assert_equal(pp == p, false)
    assert_equal(pp != p, true)
    assert_equal(pp < p, true)
    assert_equal(p < pp, false)

    p.top = tc.cell_index
    assert_equal(p.top, tc.cell_index)
    assert_equal(p.cell_index, tc.cell_index)
    assert_equal(p.source, tc.cell_index)
    assert_equal(p.trans.to_s, "r0 *1 0,0")
    assert_equal(p.source_trans.to_s, "r0 *1 0,0")
    
    assert_equal(p.path_length, 0)

    p.append_path(RBA::InstElement::new(inst))

    assert_equal(p.path_length, 1)
    assert_equal(p.path_nth(0).inst.to_s, "cell_index=1 r0 10,20")
    assert_equal(p.path_nth(0).cell_inst.to_s, "#1 r0 10,20")
    assert_equal(p.path_nth(0).specific_trans.to_s, "r0 10,20")

    assert_equal(p.trans.to_s, "r0 *1 10,20")
    assert_equal(p.source_trans.to_s, "r0 *1 10,20")
    assert_equal(p.top, tc.cell_index)
    assert_equal(p.cell_index, c2.cell_index)
    assert_equal(p.source, c2.cell_index)

    # turn it into a instance pointer
    p.layer = -1
    assert_equal(p.is_cell_inst?, true)
    assert_equal(p.trans.to_s, "r0 *1 10,20")   # deprecated -> includes instance too
    assert_equal(p.source_trans.to_s, "r0 *1 0,0")
    assert_equal(p.top, tc.cell_index)
    assert_equal(p.cell_index, c2.cell_index)   # deprecated -> includes instance too
    assert_equal(p.source, tc.cell_index)
    assert_equal(p.inst.to_s, "cell_index=1 r0 10,20")

    p.layer = 42
    assert_equal(p.path_length, 1)
    p.clear_path
    assert_equal(p.path_length, 0)
    assert_equal(p.top, tc.cell_index)
    assert_equal(p.cell_index, tc.cell_index)
    assert_equal(p.source, tc.cell_index)
    assert_equal(p.trans.to_s, "r0 *1 0,0")
    assert_equal(p.source_trans.to_s, "r0 *1 0,0")

  end

  # Selection
  def test_2

    mw = RBA::Application::instance.main_window
    mw.create_layout( 0 )
    lv = mw.current_view

    cv = lv.active_cellview 
    ly = cv.layout
    li = ly.insert_layer(RBA::LayerInfo::new(1, 0))
    tc = ly.create_cell("TOP")
    c2 = ly.create_cell("C2")
    tc_shape = tc.shapes(li).insert(RBA::Box::new(-100, -200, 300, 400))
    c2_shape = c2.shapes(li).insert(RBA::Box::new(-1, -2, 3, 4))
    inst = tc.insert(RBA::CellInstArray::new(c2.cell_index, RBA::Trans::new(10, 20)))

    p = RBA::ObjectInstPath::new
    p.cv_index = 0
    p.layer = li
    p.shape = tc_shape

    p2 = RBA::ObjectInstPath::new
    p2.cv_index = 0
    p2.layer = li
    p2.append_path(RBA::InstElement::new(inst))
    p2.shape = c2_shape

    assert_equal(lv.has_object_selection?, false)
    assert_equal(lv.object_selection.size, 0)
    lv.object_selection = [ p ]
    assert_equal(lv.object_selection.size, 1)
    assert_equal(lv.has_object_selection?, true)
    assert_equal(lv.object_selection[0] == p, true)
    lv.clear_object_selection
    assert_equal(lv.object_selection.size, 0)

    lv.select_object(p)
    assert_equal(lv.object_selection.size, 1)
    assert_equal(lv.object_selection[0] == p, true)

    lv.select_object(p2)
    assert_equal(lv.object_selection.size, 2)
    lv.unselect_object(p)
    assert_equal(lv.object_selection.size, 1)
    assert_equal(lv.object_selection[0] == p2, true)

    sel = []
    lv.each_object_selected { |o| sel << o }
    assert_equal(sel.size, 1)
    assert_equal(sel[0] == p2, true)

    # without a mouse pointer we can't test more than this:
    assert_equal(lv.has_transient_object_selection?, false)
    sel = []
    lv.each_object_selected_transient { |o| sel << o }
    assert_equal(sel.size, 0)

  end

end

load("test_epilogue.rb")
