#!/usr/bin/env python
import qt
import sys
import os.path
import tempfile

from coral.modeler import iconcache
from coral import configuration

qapp=qt.QApplication(sys.argv)

# there are real icons
for i in ["exit","redo","undo"]:
	q1=iconcache.getIcon(i)
	q2=iconcache.getIcon(i)
	assert q1==q2

# fake icons
for i in ["blahblah","/ah/blah","aha/blaha"]:
	q = iconcache.getIcon(i)
	assert q.isNull()
	
# check pathname resolution
config=configuration.getCoralConfiguration()
assert iconcache.__fixPath("/tmp/test")==["/tmp/test"]
assert iconcache.__fixPath("tmp/test")==[config.dGet(["Coral","install_path"])+"/coral/addins/tmp/test",
					 
					 config.dGet(["Coral","user_home"])+"/.coral/coral/addins/tmp/test"]

# check reload on change and removal
p = qt.QPixmap(32,32)
name = tempfile.mktemp()
fname = name+".png"
p.save(fname,"PNG")
p1 = iconcache.getIcon(name)
assert not p1.isNull()
p.save(fname,"PNG")
p2 = iconcache.getIcon(name)
assert not p2.isNull()
assert p1!=p2
os.unlink(fname)
p3 = iconcache.getIcon(name)
assert p3.isNull()
