#!/usr/bin/python
"""
$Id: non_text.py,v 1.5 2006/11/15 13:25:34 dom Exp $

The NonTextTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
* Provide a text equivalent for every non-text element.

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

    def startDocument(self):
        self._inObject = False
    
    def startElement(self, name, attrs):
        if name=="img":
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            if attrs.has_key("alt") and attrs.has_key("src"):
                if self._inObject:
                    self._objectContent = self._objectContent + attrs["alt"]
                self.addObservation(testcase.Observation("NT2",location,{"src":attrs["src"],"alt":attrs["alt"]}))
            # @@@ is this really needed?
            # This is already ensured by the validity check
            else:
                self.addObservation(testcase.Observation("NT1",location))
        elif name=="object" and attrs.has_key("data"):
            self._inObject = attrs["data"]
            self._objectContent = ""
            if attrs.has_key("title"):
                self._objectContent = attrs["title"]


    def characters(self,content):
        if self._inObject:
            self._objectContent = self._objectContent + content

        
    def endElement(self,name):
        if name=="object" and self._inObject:
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            self.addObservation(testcase.Observation("NT2",location,{"src":self._inObject,"alt":self._objectContent}))
            self._inObject = False

        
    def endDocument(self):
        if not "NT1" in self._observations:
            if not "NT2" in self._observations:
                self.addObservation(testcase.Observation("NT3",testcase.Location(self.uri)))
            else:
                self.addObservation(testcase.Observation("NT4",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 = NonTextTestCase()
        self.assertEqual(a.run(inp),res)

    def testWithMissingAlt(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/no-alt.xhtml"
        res = TestResults(
            [Observation("NT1",LineColumnLocation(inp,"utf-8",14,3))]
            )
        self._test(inp,res)


    def testWithNoNonTextItem(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation("NT3",Location(inp))]
            )
        self._test(inp,res)
    
    def testWithImage(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/nontext-1.xhtml"
        res = TestResults(
            [Observation("NT2",LineColumnLocation(inp,"utf-8",11,14),{'src': u'http://www.w3.org/Icons/w3c_home', 'alt': u'W3C'}),
             Observation('NT4',Location(inp))
             ]
            )
        self._test(inp,res)

    def testWithObjectAndTitle(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/nontext-2.xhtml"
        res = TestResults(
            [Observation('NT2',LineColumnLocation(inp,'utf-8',12,93),{'src': u' http://www.w3.org/Talks/Tools/Slidy/w3c-logo-blue.svg', 'alt': u'W3C logo'}),
             Observation('NT4',Location(inp))
             ])
        self._test(inp,res)

    def testWithObjectAndContent(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/nontext-3.xhtml"
        res = TestResults(
            [Observation('NT2',LineColumnLocation(inp,'utf-8',12,84),{'src': u' http://www.w3.org/Talks/Tools/Slidy/w3c-logo-blue.svg', 'alt': u'W3C Logo'}),
             Observation('NT4',Location(inp))
             ])
        self._test(inp,res)

    def testWithObjectAndImage(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/nontext-4.xhtml"
        res = TestResults(
            [Observation('NT2',LineColumnLocation(inp,'utf-8',12,76),{'src': u'http://www.w3.org/Icons/w3c_home', 'alt': u'W3C logo'}),
             Observation('NT2',LineColumnLocation(inp,'utf-8',12,137),{'src': u' http://www.w3.org/Talks/Tools/Slidy/w3c-logo-blue.svg', 'alt': u'W3C logo'}),
             Observation('NT4',Location(inp))
             ])
        self._test(inp,res)


def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



