1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import os
5import unittest
6
7from telemetry.page import page
8from telemetry.page import page_set
9
10class TestPage(unittest.TestCase):
11  def testUrlPathJoin(self):
12    # pylint: disable=W0212
13    self.assertEqual('a/b', page._UrlPathJoin('a', 'b'))
14    self.assertEqual('a/b', page._UrlPathJoin('a/', 'b'))
15    self.assertEqual('a/b', page._UrlPathJoin('a', '/b'))
16    self.assertEqual('a/b', page._UrlPathJoin('a/', '/b'))
17    self.assertEqual('a/b/c', page._UrlPathJoin('a', 'b', 'c'))
18    self.assertEqual('a/b/c', page._UrlPathJoin('a', 'b/', 'c'))
19    self.assertEqual('a/b/c', page._UrlPathJoin('a', 'b', '/c'))
20    self.assertEqual('a/b/c', page._UrlPathJoin('a', 'b/', '/c'))
21    self.assertEqual('a/b', page._UrlPathJoin('a', 'b', ''))
22
23  def testGetUrlBaseDirAndFileForAbsolutePath(self):
24    apage = page.Page('file:///somedir/otherdir/file.html',
25                      None, # In this test, we don't need a page set.
26                      base_dir='basedir')
27    serving_dirs, filename = apage.serving_dirs_and_file
28    self.assertEqual(serving_dirs, 'basedir/somedir/otherdir')
29    self.assertEqual(filename, 'file.html')
30
31  def testGetUrlBaseDirAndFileForRelativePath(self):
32    apage = page.Page('file:///../../otherdir/file.html',
33                      None, # In this test, we don't need a page set.
34                      base_dir='basedir')
35    serving_dirs, filename = apage.serving_dirs_and_file
36    self.assertEqual(serving_dirs, 'basedir/../../otherdir')
37    self.assertEqual(filename, 'file.html')
38
39  def testGetUrlBaseDirAndFileForUrlBaseDir(self):
40    ps = page_set.PageSet.FromDict({
41        'description': 'hello',
42        'archive_path': 'foo.wpr',
43        'serving_dirs': ['../../somedir/'],
44        'pages': [
45          {'url': 'file:///../../somedir/otherdir/file.html'}
46        ]}, 'basedir/')
47    serving_dirs, filename = ps[0].serving_dirs_and_file
48    self.assertEqual(serving_dirs, ['basedir/../../somedir/'])
49    self.assertEqual(filename, 'otherdir/file.html')
50
51  def testDisplayUrlForHttp(self):
52    ps = page_set.PageSet.FromDict({
53      "description": "hello",
54      "archive_path": "foo.wpr",
55      "pages": [
56        {"url": "http://www.foo.com/"},
57        {"url": "http://www.bar.com/"}
58        ]
59      }, os.path.dirname(__file__))
60    self.assertEquals(ps[0].display_url, 'http://www.foo.com/')
61    self.assertEquals(ps[1].display_url, 'http://www.bar.com/')
62
63  def testDisplayUrlForHttps(self):
64    ps = page_set.PageSet.FromDict({
65      "description": "hello",
66      "archive_path": "foo.wpr",
67      "pages": [
68        {"url": "http://www.foo.com/"},
69        {"url": "https://www.bar.com/"}
70        ]
71      }, os.path.dirname(__file__))
72    self.assertEquals(ps[0].display_url, 'http://www.foo.com/')
73    self.assertEquals(ps[1].display_url, 'https://www.bar.com/')
74
75  def testDisplayUrlForFile(self):
76    ps = page_set.PageSet.FromDict({
77      "description": "hello",
78      "archive_path": "foo.wpr",
79      "pages": [
80        {"url": "file:///../../otherdir/foo.html"},
81        {"url": "file:///../../otherdir/bar.html"},
82        ]
83      }, os.path.dirname(__file__))
84    self.assertEquals(ps[0].display_url, 'foo.html')
85    self.assertEquals(ps[1].display_url, 'bar.html')
86
87  def testDisplayUrlForFilesDifferingBySuffix(self):
88    ps = page_set.PageSet.FromDict({
89      "description": "hello",
90      "archive_path": "foo.wpr",
91      "pages": [
92        {"url": "file:///../../otherdir/foo.html"},
93        {"url": "file:///../../otherdir/foo1.html"},
94        ]
95      }, os.path.dirname(__file__))
96    self.assertEquals(ps[0].display_url, 'foo.html')
97    self.assertEquals(ps[1].display_url, 'foo1.html')
98
99  def testDisplayUrlForFileOfDifferentPaths(self):
100    ps = page_set.PageSet.FromDict({
101      "description": "hello",
102      "archive_path": "foo.wpr",
103      "pages": [
104        {"url": "file:///../../somedir/foo.html"},
105        {"url": "file:///../../otherdir/bar.html"},
106        ]
107      }, os.path.dirname(__file__))
108    self.assertEquals(ps[0].display_url, 'somedir/foo.html')
109    self.assertEquals(ps[1].display_url, 'otherdir/bar.html')
110
111  def testDisplayUrlForFileDirectories(self):
112    ps = page_set.PageSet.FromDict({
113      "description": "hello",
114      "archive_path": "foo.wpr",
115      "pages": [
116        {"url": "file:///../../otherdir/foo/"},
117        {"url": "file:///../../otherdir/bar/"},
118        ]
119      }, os.path.dirname(__file__))
120    self.assertEquals(ps[0].display_url, 'foo')
121    self.assertEquals(ps[1].display_url, 'bar')
122
123  def testDisplayUrlForSingleFile(self):
124    ps = page_set.PageSet.FromDict({
125      "description": "hello",
126      "archive_path": "foo.wpr",
127      "pages": [
128        {"url": "file:///../../otherdir/foo.html"},
129        ]
130      }, os.path.dirname(__file__))
131    self.assertEquals(ps[0].display_url, 'foo.html')
132
133  def testDisplayUrlForSingleDirectory(self):
134    ps = page_set.PageSet.FromDict({
135      "description": "hello",
136      "archive_path": "foo.wpr",
137      "pages": [
138        {"url": "file:///../../otherdir/foo/"},
139        ]
140      }, os.path.dirname(__file__))
141    self.assertEquals(ps[0].display_url, 'foo')
142