#!/opt/lsb/appbat/bin/python

from pytet import *

import sys,os,re

install_dir="/opt/lsb/test/desktop/fontconfig/"

def startup():
        print "tc1: Calling startup"

def cleanup():
        print "tc1: Calling cleanup"

def test1():
        try:
                stdout,stderr = os.popen3('export FONTCONFIG_FILE=' + os.path.join(install_dir, 'fonts.conf') +
                        ' ; fc-list - family pixelsize ' + os.path.join(install_dir, 'fonts/'))[1:]
                data = stdout.read()
                stdout.close()

                l = filter(None, data.split("\n"))
                l.sort(lambda x,y: cmp(y.split("=",1)[-1], x.split("=",1)[-1]))
                if l[0].split()[-1] != "Fixed:pixelsize=6":
                        tet_result(TET_FAIL)
                elif l[1].split()[-1] != "Fixed:pixelsize=16":
                        tet_result(TET_FAIL)
                else:
                        tet_result(TET_PASS)

        except os.error:
                tet_result(TET_FAIL)


def test2():
        try:
                stdin,stdout,stderr = os.popen3('export FONTCONFIG_FILE=' + install_dir + 'fonts.conf ; fc-match $FONTCONFIG_FILE')
        except os.error:
                tet_result(TET_FAIL)
        else:
                tet_result(TET_PASS)

def test3():
        # intentionally force error output on stderr for invalid FONTCONFIG_FILE location.
        stderr = os.popen3('export FONTCONFIG_FILE=/dev/null/INVALID/LOCATION/fonts.conf; fc-match $FONTCONFIG_FILE')[-1]
        if not stderr.read():
                # invalid location should trigger error message.
                tet_result(TET_FAIL)
        else:
                tet_result(TET_PASS)

testlist = { 1:test1, 2:test2, 3:test3 }
pytet_init(testlist, startup, cleanup)
