#!/usr/bin/python
"""
$Id: access_key.py,v 1.9 2007/04/26 07:21:09 dom Exp $

The AccessKeysTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
Assign access keys to links in navigational menus and
frequently accessed functionality.

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

    def startDocument(self):
        self._candidates = ["a", "area", "button", "input", "label", "legend", "textarea"]
        self._hasCandidates = False
            
    def startElement(self, name, attrs):
        if name in self._candidates:
            self._hasCandidates = True
            if attrs.has_key("accesskey"):
                # the document does use accesskeys
                # we log a "pass" observation (AK2)
                # and an info observation for each accesskey (AK1)
                location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
                # @@@ Should this depend on whether the access key is numeric?
                if not "AK2" in self._observations:
                    self.addObservation(testcase.Observation("AK2",location))
                self.addObservation(testcase.Observation("AK1",location,{"accesskey":attrs["accesskey"]}))
                # testing whether the accesskey is numerical
                try:
                    int(attrs["accesskey"])
                except ValueError:
                    self.addObservation(testcase.Observation("AK6",location,{"accesskey":attrs["accesskey"]}))


    def endDocument(self):
        if not self._hasCandidates:
            self.addObservation(testcase.Observation("AK4",testcase.Location(self.uri)))
        else:
            if not "AK2" in self._observations:
                self.addObservation(testcase.Observation("AK3",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 = AccessKeysTestCase()
        self.assertEqual(a.run(inp),res)
        
    def testWithNumericAccessKeys(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/accesskeys-1.xhtml"
        res = TestResults(
            [Observation("AK2",LineColumnLocation(inp,"utf-8",9,18)),
             Observation("AK1",LineColumnLocation(inp,"utf-8",9,18),{"accesskey":'0'})]
            )
        self._test(inp,res)

    def testWithoutAccessKeys(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/accesskeys-2.xhtml"
        res = TestResults(
            [Observation("AK3",Location(inp))])
        self._test(inp,res)

    def testWithNonNumericAccessKeys(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/accesskeys-3.xhtml"
        res = TestResults(
            [Observation('AK2',LineColumnLocation(inp,'utf-8',9,18)),
             Observation('AK1',LineColumnLocation(inp,'utf-8',9,18),{"accesskey":'A'}),
             Observation('AK6',LineColumnLocation(inp,'utf-8',9,18),{"accesskey":'A'})])
        self._test(inp,res)

def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



