#!/usr/bin/python
"""
$Id: page_title.py,v 1.6 2006/11/14 17:32:58 dom Exp $

The PageTitleTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
* Provide a short but descriptive page title.
  
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 PAGE_TITLE BP
# Assumes valid XHTML content (i.e. that a title exists)
class PageTitleTestCase(testcase.XMLBasedTestCase):
    BpId = ["PAGE_TITLE"]

    def startElement(self, name,attrs):
        if name=="title":
            self._title = ""

        
    def characters(self,content):
        if hasattr(self,"_title"):
            self._title = self._title + content

    
    def endElement(self, name):
        if name=="title":
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            if self._title!="":
                self.addObservation(testcase.Observation("PT1",location,{"title":self._title}))
            else:
                self.addObservation(testcase.Observation("PT2",location))

    def endDocument(self):
        if not "PT1" in self._observations and not "PT2" in self._observations:
            self.addObservation(testcase.Observation("PT3",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 = PageTitleTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithTitle(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation("PT1",LineColumnLocation(inp,'utf-8',5,11),{"title":"Test"})]
            )
        self._test(inp,res)

    def testWithEmptyTitle(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/emptytitle.xhtml"
        res = TestResults(
            [Observation("PT2",LineColumnLocation(inp,'utf-8',5,7))]
            )
        self._test(inp,res)

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

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

if __name__ == '__main__':
    _test()



