#!/usr/bin/env python
#
# The horrors of the outliner!
# This test tries to make sure we don't do a shitload
# of extra work in the crummy outliner
#
#
fname = "../../../../qa/models2import/Softsale.xmi"

import coral
from coral.modeler.outline import outlineeditor
import time
DI = coral.lang.XMIDI20

# Create window and display it
from coral.modeler import modeler, action
mw=modeler.ModelerWindow()
mw.processEvents()

prj = action.openProject(fname)
assert prj

mw.processEvents()

ERRORS = 0

def check(n, n_shouldbe, s):
    print s, ": ", n, "(should be", n_shouldbe, ")"
    if n == n_shouldbe:
        return
    global ERRORS
    ERRORS += 1

n_init = outlineeditor.DEBUG_N_LIST_ITEMS
check(n_init, 1, "Amount of listitems at startup")

# Activate a root abstract element
for e in prj.contents():
    if e.oclIsKindOf(DI.Diagram):
        continue
    mw.getDiagramEditorManager().editorSet.notifyFocusOn(e)
    mw.processEvents()
    break

n_start = outlineeditor.DEBUG_N_LIST_ITEMS
check(n_start, n_init + len(prj.contents()), "Amount of listitems in expanded view of Softsale.xmi")

# Activate all diagrams
try:
    for e in prj.contents():
        if not e.oclIsKindOf(DI.Diagram):
            continue
        mw.getDiagramEditorManager().editorSet.notifyFocusOn(e)
        mw.processEvents()
        n_now = outlineeditor.DEBUG_N_LIST_ITEMS
        # The value should not change!
        check(n_now, n_start, "After activating a diagram")
        ###time.sleep(1)
except KeyboardInterrupt:
    pass

if ERRORS:
    print "ARGH, we failed on ", ERRORS, "places!"
    assert 0
