VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_RankGrid"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private page_ As Visio.Page
Private grid_ As RankGrid

Public Function Setup()
  ' Mandatory setup function
  Set page_ = ThisDocument.Pages.Add
  Set grid_ = New RankGrid: Call grid_.Init(page_)
End Function

Public Function Teardown()
  ' Mandatory teardown function3
  Call page_.Delete(0)
End Function

Public Function t_Access()
  On Error GoTo PROPAGATE_ERROR
  
  Dim theGrid As New RankGrid
  Dim iElement As Visio.Shape: Set iElement = page_.Drop(FindMaster(ThisDocument, MASTER_BASIC), 0, 0)
  
  Call Dev_NewCase("No grid")
  Call theGrid.Init(page_)
  Call Dev_ExpectEQ(0, theGrid.Size)
  Call Dev_ExpectEQ(RANK_UNREACHABLE, theGrid.GetRankFor(iElement))
  
  Call Dev_NewCase("Custom grid")
  Dim iLine1 As Visio.Shape: Set iLine1 = page_.Drop(FindMaster(ThisDocument, MASTER_LEVEL), 0, 5)
  iLine1.Name = PREFIX_GRID & "1"
  Dim iLine2 As Visio.Shape: Set iLine2 = page_.Drop(FindMaster(ThisDocument, MASTER_LEVEL), 0, -5)
  iLine2.Name = PREFIX_GRID & "2"
  Call theGrid.Init(page_)
  Call Dev_ExpectEQ(2, theGrid.Size)
  
  Call Dev_NewCase("Ranks")
  Call Dev_ExpectEQ(1, theGrid.GetRankFor(iElement), "Mid")
  iElement.CellsU("PinY") = 15
  Call Dev_ExpectEQ(0, theGrid.GetRankFor(iElement), "Top")
  iElement.CellsU("PinY") = -15
  Call Dev_ExpectEQ(RANK_UNREACHABLE, theGrid.GetRankFor(iElement), "Bottom")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_CreateGrid()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Invalid rank")
  On Error Resume Next
  Call grid_.CreateGrid(0, 10, 30, 1, 1)
  Call Dev_ExpectNoError
  On Error GoTo PROPAGATE_ERROR
  Call Dev_ExpectEQ(0, page_.Shapes.Count, "Do not create anything")
  
  Call Dev_NewCase("Valid grid")
  Call grid_.CreateGrid(3, dHeight:=10, dWidth:=30, dTopMargin:=1, dLeftMargin:=2)
  Call Dev_ExpectEQ(3, page_.Shapes.Count, "Shapes count")
  Call Dev_ExpectAEQ(10, page_.Shapes("SLevel1").CellsU("PinY") - page_.Shapes.Item("SLevel2").CellsU("PinY"), 2, "Height")
  Call Dev_ExpectAEQ(30, page_.Shapes("SLevel1").CellsU("Width"), 2, "Width")
  Call Dev_ExpectAEQ(grid_.Width, page_.Shapes("SLevel1").CellsU("Width"), 2, "Width")
  Call Dev_ExpectAEQ(-1, page_.Shapes("SLevel1").CellsU("PinY") - page_.PageSheet.CellsU("YGridOrigin"), 2, "Top Margin")
  Call Dev_ExpectAEQ(2, page_.Shapes("SLevel1").CellsU("BeginX") - page_.PageSheet.CellsU("XGridOrigin"), 2, "Left Margin")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_DeleteGrid()
  On Error GoTo PROPAGATE_ERROR
  
  Dim iElement As Visio.Shape: Set iElement = page_.Drop(FindMaster(ThisDocument, MASTER_BASIC), 0, 0)
  Dim nID&: nID = iElement.ID
  
  Call Dev_NewCase("No grid")
  On Error Resume Next
  Call grid_.DeleteGrid
  Call Dev_ExpectNoError
  On Error GoTo PROPAGATE_ERROR
  Call Dev_ExpectEQ(1, page_.Shapes.Count, "Do not remove existing shapes")
  
  Call Dev_NewCase("Valid grid")
  Call grid_.CreateGrid(3, dHeight:=10, dWidth:=30, dTopMargin:=1, dLeftMargin:=2)
  Call grid_.DeleteGrid
  Call Dev_ExpectEQ(1, page_.Shapes.Count, "Shapes count")
  Call Dev_ExpectEQ(nID, page_.Shapes(1).ID)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_MoveShape()
  On Error GoTo PROPAGATE_ERROR
  
  Dim iElement As Visio.Shape: Set iElement = page_.Drop(FindMaster(ThisDocument, MASTER_BASIC), 0, 0)
  
  Call Dev_NewCase("No grid")
  On Error Resume Next
  Call Dev_ExpectFalse(grid_.MoveToRank(iElement, 0))
  Call Dev_ExpectNoError
  Call Dev_ExpectFalse(grid_.MoveUp(iElement))
  Call Dev_ExpectNoError
  Call Dev_ExpectFalse(grid_.MoveDown(iElement))
  Call Dev_ExpectNoError
  On Error GoTo PROPAGATE_ERROR
  
  Call grid_.CreateGrid(3, dHeight:=10, dWidth:=30, dTopMargin:=1, dLeftMargin:=2)
  Dim iLevel1 As Visio.Shape: Set iLevel1 = page_.Shapes("SLevel1")
  Dim iLevel2 As Visio.Shape: Set iLevel2 = page_.Shapes("SLevel2")
  Dim iLevel3 As Visio.Shape: Set iLevel3 = page_.Shapes("SLevel3")
  
  Call Dev_NewCase("Move valid")
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 0))
  Call Dev_ExpectEQ(0, grid_.GetRankFor(iElement))
  Call Dev_ExpectGR(iElement.CellsU("PinY"), iLevel1.CellsU("PinY"))

  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 1))
  Call Dev_ExpectEQ(1, grid_.GetRankFor(iElement))
  Call Dev_ExpectLS(iElement.CellsU("PinY"), iLevel1.CellsU("PinY"))
  Call Dev_ExpectGR(iElement.CellsU("PinY"), iLevel2.CellsU("PinY"))
  
  Call Dev_ExpectTrue(grid_.MoveUp(iElement))
  Call Dev_ExpectEQ(0, grid_.GetRankFor(iElement))
  Call Dev_ExpectFalse(grid_.MoveUp(iElement))
  Call Dev_ExpectTrue(grid_.MoveDown(iElement))
  Call Dev_ExpectEQ(1, grid_.GetRankFor(iElement))
  
  
  Call Dev_NewCase("Move from top")
  iElement.Cells("PinY") = 100
  Call Dev_ExpectEQ(0, grid_.GetRankFor(iElement))
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 2), "MoveTo")
  Call Dev_ExpectEQ(2, grid_.GetRankFor(iElement), "MoveTo")
  
  iElement.Cells("PinY") = 100
  Call Dev_ExpectFalse(grid_.MoveUp(iElement), "Move up")
  Call Dev_ExpectEQ(0, grid_.GetRankFor(iElement), "Move up")
  
  iElement.Cells("PinY") = 100
  Call Dev_ExpectTrue(grid_.MoveDown(iElement), "Move down")
  Call Dev_ExpectEQ(1, grid_.GetRankFor(iElement), "Move down")
  
  Call Dev_NewCase("Move from bottom")
  iElement.Cells("PinY") = -100
  Call Dev_ExpectEQ(RANK_UNREACHABLE, grid_.GetRankFor(iElement))
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 2), "MoveTo")
  Call Dev_ExpectEQ(2, grid_.GetRankFor(iElement), "MoveTo")
  
  iElement.Cells("PinY") = -100
  Call Dev_ExpectTrue(grid_.MoveUp(iElement), "Move up")
  Call Dev_ExpectEQ(2, grid_.GetRankFor(iElement), "Move up")
  
  iElement.Cells("PinY") = -100
  Call Dev_ExpectFalse(grid_.MoveDown(iElement), "Move down")
  Call Dev_ExpectEQ(RANK_UNREACHABLE, grid_.GetRankFor(iElement), "Move down")
  
  Call Dev_NewCase("Create additional levels")
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 2))
  Call Dev_ExpectTrue(grid_.MoveDown(iElement), "Move down")
  Call Dev_ExpectEQ(3, grid_.GetRankFor(iElement), "Move down")
  Call Dev_ExpectEQ(4, grid_.Size, "Move down")
  
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 6), "Move to")
  Call Dev_ExpectEQ(6, grid_.GetRankFor(iElement), "Move to")
  Call Dev_ExpectEQ(7, grid_.Size, "Move to")
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, RANK_MAX), "Max rank")
  Call Dev_ExpectFalse(grid_.MoveDown(iElement), "Max rank")
  Call Dev_ExpectFalse(grid_.MoveToRank(iElement, RANK_MAX + 1), "Max rank")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_CustomGrid()
  On Error GoTo PROPAGATE_ERROR
  
  Dim iElement As Visio.Shape: Set iElement = page_.Drop(FindMaster(ThisDocument, MASTER_BASIC), 0, 0)
  Call grid_.CreateGrid(3, dHeight:=10, dWidth:=30, dTopMargin:=1, dLeftMargin:=2)
  Dim iLevel1 As Visio.Shape: Set iLevel1 = page_.Shapes("SLevel1")
  Dim iLevel2 As Visio.Shape: Set iLevel2 = page_.Shapes("SLevel2")
  Dim iLevel3 As Visio.Shape: Set iLevel3 = page_.Shapes("SLevel3")
  
  iLevel1.CellsU("BeginY") = -10
  iLevel1.CellsU("EndY") = -10
  iLevel3.CellsU("BeginY") = 10
  iLevel3.CellsU("EndY") = 10
  Call iLevel2.Delete
  
  Call grid_.Init(page_)
  
  Call Dev_ExpectEQ(2, grid_.Size, "Grid size")
  Call Dev_ExpectTrue(grid_.MoveToRank(iElement, 1), "Valid move")
  Call Dev_ExpectEQ(1, grid_.GetRankFor(iElement), "Valid move")
  Call Dev_ExpectLS(iElement.CellsU("PinY"), iLevel3.CellsU("PinY"), "Level ordering")
  Call Dev_ExpectGR(iElement.CellsU("PinY"), iLevel1.CellsU("PinY"), "Level ordering")
  
  Call Dev_NewCase("Add level")
  Call Dev_ExpectTrue(grid_.MoveDown(iElement), "Move down")
  Call Dev_ExpectEQ(2, grid_.GetRankFor(iElement), "Move down")
  Call Dev_ExpectEQ(3, grid_.Size, "Move down")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_ChangeWidth()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("No grid")
  On Error Resume Next
  Call Dev_ExpectFalse(grid_.IncrementWidth(10))
  Call Dev_ExpectNoError
  Call Dev_ExpectFalse(grid_.IncrementWidth(-10))
  Call Dev_ExpectNoError
  On Error GoTo PROPAGATE_ERROR
  
  Call grid_.CreateGrid(3, dHeight:=10, dWidth:=30, dTopMargin:=1, dLeftMargin:=2)
  Dim iLevel1 As Visio.Shape: Set iLevel1 = page_.Shapes("SLevel1")
  Dim iLevel2 As Visio.Shape: Set iLevel2 = page_.Shapes("SLevel2")
  Dim iLevel3 As Visio.Shape: Set iLevel3 = page_.Shapes("SLevel3")
  iLevel2.CellsU("BeginX") = 10
  
  Call Dev_NewCase("Valid grid")
  Call Dev_ExpectTrue(grid_.IncrementWidth(10))
  Call Dev_ExpectAEQ(iLevel1.CellsU("BeginX"), iLevel2.CellsU("BeginX"), 2, "Alignment")
  Call Dev_ExpectAEQ(40, grid_.Width, 2, "Expand Width")
  Call Dev_ExpectTrue(grid_.IncrementWidth(-10))
  Call Dev_ExpectAEQ(30, grid_.Width, 2, "Shrink Width")
  
  Call Dev_NewCase("Invalid increment")
  Call Dev_ExpectFalse(grid_.IncrementWidth(-1000))
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
