#!/usr/bin/python
"""
$Id: measures.py,v 1.4 2006/06/12 10:11:45 dom Exp $

The MeasuresTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
*  Do not use pixel measures and do not use absolute units in markup
   language attribute values and style sheet property values.

License
-------
Copyright (c) 2006 World Wide Web Consortium, (Massachusetts
Institute of Technology, European Research Consortium for Informatics
and Mathematics, Keio University). All Rights Reserved. This work is
distributed under the W3C Software License [1] in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[1] http://www.w3.org/Consortium/Legal/copyright-software

"""



from  validator import testcase

# Tests regarding the MEASURES BP
# @@@ Needs to check the css properties as well
class MeasuresTestCase(testcase.XMLBasedTestCase):
    BpId= ["MEASURES"]

    def startElement(self,name,attrs):
        # we exclude img as per the BP
        if name in ["object","hr","th","td","table","cols","colgroup","tbody","td","tfoot","thead","tr","iframe","applet"]:
            candidates = ["width","height","cellpadding","cellspacing","charoff"]
            for c in candidates:
                if attrs.has_key(c):
                    if "%" not in attrs[c]:
                        location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
                        self.addObservation(Observation("ME1",location,{"attribute":c}))

        
    def endDocument(self):
        if "ME1" not in self._observations:
            self.addObservation(Observation("ME2",testcase.Location(self.uri)))

                
# -------------------------
# Unit tests for this module
import unittest

from validator.testcase import TestResults, Observation, Location, LineColumnLocation
class Tests(unittest.TestCase):
    def _test(self,inp,res):
        a = MeasuresTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithoutAbsoluteMeasure(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation("ME2",Location(inp))]
            )
        self._test(inp,res)

    def testWithoutRelativeMeasures(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/measures-1.xhtml"
        res = TestResults(
            [Observation("ME2",Location(inp))]
            )
        self._test(inp,res)


    def testWithAbsoluteMeasure(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/measures-2.xhtml"
        res = TestResults(
            [Observation("ME1",LineColumnLocation(inp,'utf-8',12,0),{'attribute': 'width'}),
             Observation('ME1',LineColumnLocation(inp,'utf-8',14,0),{'attribute': 'width'})
             ]
            )
        self._test(inp,res)
        

def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



