Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / path_canonicalizer_test.py
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import posixpath
7 import unittest
8
9 from extensions_paths import PUBLIC_TEMPLATES, SERVER2
10 from local_file_system import LocalFileSystem
11 from test_file_system import TestFileSystem
12 from object_store_creator import ObjectStoreCreator
13 from path_canonicalizer import PathCanonicalizer
14 from special_paths import SITE_VERIFICATION_FILE
15
16
17 class PathCanonicalizerTest(unittest.TestCase):
18   def setUp(self):
19     self._path_canonicalizer = PathCanonicalizer(
20         LocalFileSystem.Create(PUBLIC_TEMPLATES),
21         ObjectStoreCreator.ForTest(),
22         ('.html', '.md'))
23
24   def testSpecifyCorrectly(self):
25     self._AssertIdentity('extensions/browserAction')
26     self._AssertIdentity('extensions/storage')
27     self._AssertIdentity('extensions/blah')
28     self._AssertIdentity('extensions/index')
29     self._AssertIdentity('extensions/whats_new')
30     self._AssertIdentity('apps/storage')
31     self._AssertIdentity('apps/bluetooth')
32     self._AssertIdentity('apps/blah')
33     self._AssertIdentity('apps/tags/webview')
34
35   def testSpecifyIncorrectly(self):
36     self._AssertRedirectWithDefaultExtensions(
37         'extensions/browserAction', 'apps/browserAction')
38     self._AssertRedirectWithDefaultExtensions(
39         'extensions/browserAction', 'apps/extensions/browserAction')
40     self._AssertRedirectWithDefaultExtensions(
41         'apps/bluetooth', 'extensions/bluetooth')
42     self._AssertRedirectWithDefaultExtensions(
43         'apps/bluetooth', 'extensions/apps/bluetooth')
44     self._AssertRedirectWithDefaultExtensions(
45         'extensions/index', 'apps/index')
46     self._AssertRedirectWithDefaultExtensions(
47         'extensions/browserAction', 'static/browserAction')
48     self._AssertRedirectWithDefaultExtensions(
49         'apps/tags/webview', 'apps/webview')
50     self._AssertRedirectWithDefaultExtensions(
51         'apps/tags/webview', 'extensions/webview')
52     self._AssertRedirectWithDefaultExtensions(
53         'apps/tags/webview', 'extensions/tags/webview')
54
55     # These are a little trickier because storage.html is in both directories.
56     # They must canonicalize to the closest match.
57     self._AssertRedirectWithDefaultExtensions(
58         'extensions/storage', 'extensions/apps/storage')
59     self._AssertRedirectWithDefaultExtensions(
60         'apps/storage', 'apps/extensions/storage')
61
62   def testUnspecified(self):
63     self._AssertRedirectWithDefaultExtensions(
64         'extensions/browserAction', 'browserAction')
65     self._AssertRedirectWithDefaultExtensions(
66         'apps/bluetooth', 'bluetooth')
67     # Default happens to be apps because it's first alphabetically.
68     self._AssertRedirectWithDefaultExtensions(
69         'apps/storage', 'storage')
70     # Nonexistent APIs should be left alone.
71     self._AssertIdentity('blah.html')
72
73   def testDirectories(self):
74     # Directories can be canonicalized too!
75     self._AssertIdentity('apps/')
76     self._AssertIdentity('apps/tags/')
77     self._AssertIdentity('extensions/')
78     # No trailing slash should be treated as files not directories, at least
79     # at least according to PathCanonicalizer.
80     self._AssertRedirect('extensions/apps', 'apps')
81     self._AssertRedirect('extensions', 'extensions')
82     # Just as tolerant of spelling mistakes.
83     self._AssertRedirect('apps/', 'Apps/')
84     self._AssertRedirect('apps/tags/', 'Apps/TAGS/')
85     self._AssertRedirect('extensions/', 'Extensions/')
86     # Find directories in the correct place.
87     self._AssertRedirect('apps/tags/', 'tags/')
88     self._AssertRedirect('apps/tags/', 'extensions/tags/')
89
90   def testSpellingErrors(self):
91     for spelme in ('browseraction', 'browseraction.htm', 'BrowserAction',
92                    'BrowserAction.html', 'browseraction.html', 'Browseraction',
93                    'browser-action', 'Browser.action.html', 'browser_action',
94                    'browser-action.html', 'Browser_Action.html'):
95       self._AssertRedirect('extensions/browserAction', spelme)
96       self._AssertRedirect('extensions/browserAction', 'extensions/%s' % spelme)
97       self._AssertRedirect('extensions/browserAction', 'apps/%s' % spelme)
98
99   def testNonDefaultExtensions(self):
100     # The only example currently of a file with a non-default extension is
101     # the redirects.json file. That shouldn't have its extension stripped since
102     # it's not in the default extensions.
103     self._AssertIdentity('redirects.json')
104     self._AssertRedirect('redirects.json', 'redirects')
105     self._AssertRedirect('redirects.json', 'redirects.html')
106     self._AssertRedirect('redirects.json', 'redirects.js')
107     self._AssertRedirect('redirects.json', 'redirects.md')
108
109   def testSiteVerificationFile(self):
110     # The site verification file should not redirect.
111     self._AssertIdentity(SITE_VERIFICATION_FILE)
112     self._AssertRedirect(SITE_VERIFICATION_FILE,
113                          posixpath.splitext(SITE_VERIFICATION_FILE)[0])
114
115   def testDotSeparated(self):
116     self._AssertIdentity('extensions/devtools_inspectedWindow')
117     self._AssertRedirect('extensions/devtools_inspectedWindow',
118                          'extensions/devtools.inspectedWindow')
119
120   def testUnderscoreSeparated(self):
121     file_system = TestFileSystem({
122       'pepper_dev': {
123         'c': {
124           'index.html': ''
125         }
126       },
127       'pepper_stable': {
128         'c': {
129           'index.html': ''
130         }
131       }
132     })
133     self._path_canonicalizer = PathCanonicalizer(
134         file_system,
135         ObjectStoreCreator.ForTest(),
136         ('.html', '.md'))
137     self._AssertIdentity('pepper_stable/c/index')
138     self._AssertRedirect('pepper_stable/c/index',
139                          'pepper_stable/c/index.html')
140
141   def _AssertIdentity(self, path):
142     self._AssertRedirect(path, path)
143
144   def _AssertRedirect(self, to, from_):
145     self.assertEqual(to, self._path_canonicalizer.Canonicalize(from_))
146
147   def _AssertRedirectWithDefaultExtensions(self, to, from_):
148     for ext in ('', '.html', '.md'):
149       self._AssertRedirect(
150           to, self._path_canonicalizer.Canonicalize(from_ + ext))
151
152
153 if __name__ == '__main__':
154   unittest.main()