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

The ExternalResourcesTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
* Use style sheets to control layout and presentation, unless the device is known not to support them.
*  Organize documents so that they may be read without style sheets.


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,utils

# Tests regarding the  STYLE_SHEETS_SUPPORT and STYLE_SHEETS_USE BP
class StyleSheetsTestCase(testcase.LinksBasedTestCase):
    BpId = ["STYLE_SHEETS_SUPPORT","STYLE_SHEETS_USE"]

    def startDocument(self):
        self._hasCss = False
        testcase.LinksBasedTestCase.startDocument(self)

    def startElement(self,name,attrs):
        if name=="style" or attrs.has_key("style"):
            self._hasCss = True
        testcase.LinksBasedTestCase.startElement(self,name,attrs)
    
    def _observeLinks(self):
        import validator.utils
        links = self.getLinks()
        for link in links:
            if link.nature==utils.LINK_TYPE_EMBED and link.mime=="text/css":
                self._hasCss = True
                break
        if self._hasCss:
            self.addObservation(Observation("ST1",testcase.Location(self.uri)))
        else:
            self.addObservation(Observation("ST2",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 = StyleSheetsTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithStyleSheet(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/stylesheets-1.xhtml"
        res = TestResults(
            [Observation("ST1",Location(inp))]
            )
        self._test(inp,res)


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


def _test():
    unittest.main()

if __name__ == '__main__':
    _test()



