#!/usr/bin/python
"""
$Id: pop_up.py,v 1.7 2006/07/31 13:24:34 dom Exp $

The PopUpTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
Do not cause pop-ups or other windows to appear and do not
change the current window without informing the user.

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

    def __init__(self):
        testcase.XMLBasedTestCase.__init__(self)
        self._candidates = ["a", "area", "base", "link", "form"]
            
    def startElement(self, name, attrs):
        if name in self._candidates and attrs.has_key("target") and attrs["target"] not in ["_self","_parent","_top"]:
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            self.addObservation(testcase.Observation("PU1",location))


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

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


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

if __name__ == '__main__':
    _test()



