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

The ImageMapTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
Do not use image maps unless you know the target client supports
them effectively.

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 IMAGE_MAPS BP
class ImageMapTestCase(testcase.XMLBasedTestCase):
    BpId = ["IMAGE_MAPS"]

    def __init__(self):
        testcase.XMLBasedTestCase.__init__(self)
        # the list of  elements that can take an access key in XHTML Basic
        self._candidates = ["a", "area", "button", "input", "label", "legend", "textarea"]
            
    def startElement(self, name, attrs):
        if name=='img' and attrs.has_key("usemap"):
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            self.addObservation(testcase.Observation("IM2",location))


    def endDocument(self):
        if not "IM2" in self._observations:
            self.addObservation(testcase.Observation("IM1",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 = ImageMapTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithNoImageMap(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation("IM1",Location(inp))]
            )
        self._test(inp,res)
        

    def testWithImageMap(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/imagemap-1.xhtml"
        res = TestResults(
            [Observation("IM2",LineColumnLocation(inp,'utf-8',9,3))])
        self._test(inp,res)


def _test():
    import doctest, access_key
    doctest.testmod(access_key)
    unittest.main()

if __name__ == '__main__':
    _test()



