#!/usr/bin/python
"""
$Id: link_target.py,v 1.9 2006/11/14 18:19:46 dom Exp $

The LinkTargetTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BP:
Note the target file's format unless you know the device supports it.

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 LINK_TARGET_FORMAT BP
class LinkTargetTestCase(testcase.LinksBasedTestCase):
    BpId=["LINK_TARGET_FORMAT"]
    
    def _observeLinks(self):
        from  validator import utils
        links = self.getLinks()
        for link in links:
            if link.nature==utils.LINK_TYPE_EXT:
                # Checking what is the uri scheme of the target
                if not link.target.split(":")[0] in ["http"]:
                    self.addObservation(Observation("LT3",link.location,{"uri":link.target,"scheme":link.target.split(":")[0]}))
                # @@@ accepted mime type should come from profile
                elif link.mime not in ["image/jpeg","image/gif","application/xhtml+xml","text/html"]: 
                    self.addObservation(Observation("LT1",link.location,{"uri":link.target,"mime":link.mime}))
        if not "LT1" in self._observations and not "LT3" in self._observations:
            self.addObservation(Observation("LT2",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 = LinkTargetTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithGoodLinks(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/link-target-1.xhtml"
        res = TestResults(
            [Observation("LT2",Location(inp))]
            )
        self._test(inp,res)

    def testWithABadLink(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/link-target-2.xhtml"
        res = TestResults(
            [
            Observation('LT1',LineColumnLocation(inp,'utf-8',9,18),{'mime': 'text/html', 'uri': u'http://dev.w3.org/cvsweb/2006/mwbp-validator/tests/'}),            
            Observation('LT1',LineColumnLocation(inp,'utf-8',10,3),{'mime': 'application/rdf+xml', 'uri': u'http://www.w3.org/2002/01/tr-automation/tr.rdf'}),
             Observation('LT1',LineColumnLocation(inp,'utf-8',12,13) ,{'mime': 'text/html', 'uri': u'http://www.w3.org/People/Dom/'})]
            )
        self._test(inp,res)


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

if __name__ == '__main__':
    _test()



