summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/find_in_page.py
blob: fac39f104c0a7146fbdb377cf06ceb3842dedf2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import codecs
import os
import unittest

import pyauto_functional
import pyauto
import test_utils


class FindMatchTests(pyauto.PyUITest):

  # Data dir where all find test data files are kept
  find_test_data_dir = 'find_in_page'

  def testCanFindMatchCount(self):
    """Verify Find match count for valid search"""
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title1.html'))
    self.NavigateToURL(url)
    self.assertEqual(1, self.FindInPage('title')['match_count'])

  def testCanFindMatchCountFail(self):
    """Verify Find match count for invalid search"""
    url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title1.html'))
    self.NavigateToURL(url)
    self.assertEqual(0, self.FindInPage('blah')['match_count'])

  def testFindIsNotCaseSensitive(self):
    """Verify that find is not case sensitive.

    Manually Find is case insensitive. But since FindInPage is
    case-sensitive by default we are confirming that we get a
    different result when we turn off case matching.
    """
    url = self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'find_in_page', 'largepage.html'))
    self.NavigateToURL(url)
    case_sensitive_result = self.FindInPage('The')['match_count']
    case_insenstive_result = (self.FindInPage('The', match_case=False)
                              ['match_count'])
    self.assertTrue(case_insenstive_result >= case_sensitive_result)

  def testLocalizationAndCaseOrder(self):
    """Verify that we check for localization.

    Here we check the Turkish-i scenario where we verify that we
    find both dotted and dotless I's.
    """
    url = self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'find_in_page', 'turkish.html'))
    self.NavigateToURL(url)
    dotless = self.FindInPage(u'\u0131')['match_count']
    dotted = self.FindInPage('i')['match_count']
    capital_i_with_dot = self.FindInPage(u'\u0130')['match_count']
    capital_i = self.FindInPage('I')['match_count']
    self.assertNotEqual(dotless, dotted)
    self.assertNotEqual(capital_i_with_dot, capital_i)
    self.assertNotEqual(dotted, capital_i_with_dot)

  def testSearchInTextAreas(self):
    """Verify search for text within various forms and text areas."""
    urls = []
    urls.append(self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'find_in_page', 'textintextarea.html')))
    urls.append(self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'find_in_page', 'smalltextarea.html')))
    urls.append(self.GetFileURLForPath(os.path.join(
        self.DataDir(), 'find_in_page', 'populatedform.html')))
    for url in urls:
      self.NavigateToURL(url)
      self.assertEqual(1, self.FindInPage('cat')['match_count'])
      self.assertEqual(0, self.FindInPage('bat')['match_count'])

  def testSearchWithinSpecialURL(self):
    """Verify search for text within special URLs such as chrome:history.
       chrome://history, chrome://downloads, pyAuto Data directory
    """
    zip_file = 'a_zip_file.zip'
    self.NavigateToURL(self.GetFileURLForPath(self.DataDir()))
    # search in Data directory
    self.assertEqual(1,
        self.FindInPage('downloads', tab_index=0)['match_count'])
    # search in History page
    self.AppendTab(pyauto.GURL('chrome://history'))
    # the contents in the history page load asynchronously after tab loads
    search_query = os.path.join('chrome', 'test', 'data')
    self.WaitUntil(
        lambda: self.FindInPage(search_query, tab_index=1)['match_count'],
        expect_retval=1)
    self.assertEqual(
        1, self.FindInPage(search_query, tab_index=1)['match_count'])
    # search in Downloads page
    test_utils.DownloadFileFromDownloadsDataDir(self, zip_file)
    self.AppendTab(pyauto.GURL('chrome://downloads'))
    # the contents in the downloads page load asynchronously after tab loads
    self.WaitUntil(
        lambda: self.FindInPage(zip_file, tab_index=2)['match_count'],
        expect_retval=2)
    self.assertEqual(2,
        self.FindInPage(zip_file, tab_index=2)['match_count'])
    test_utils.RemoveDownloadedTestFile(self, zip_file)

  def testFindNextAndPrevious(self):
    """Verify search selection coordinates.

    The data file used is set-up such that the text occurs on the same line,
    and we verify their positions by verifying their relative positions.
    """
    search_string = u'\u5728\u897f\u660c\u536b\u661f\u53d1'
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'specialchar.html'))
    self.NavigateToURL(url)
    first_find = self.FindInPage(search_string)
    second_find = self.FindInPage(search_string, find_next=True)
    # We have search occurrence in the same row, so top-bottom
    # coordinates should be the same even for second search.
    self.assertEqual(first_find['match_top'], second_find['match_top'],
                     'Words\' top coordinates should be same')
    self.assertEqual(first_find['match_bottom'], second_find['match_bottom'],
                     'Words\' bottom coordinates should be same')
    # And left-right coordinates should be in order.
    self.assertTrue(first_find['match_left'] < second_find['match_left'],
                    'Second find left coordinate should be greater than '
                    'the first find left coordinate')
    self.assertTrue(first_find['match_right'] < second_find['match_right'],
                    'Second find right coordinate should be greater than '
                    'the first find right coordinate')
    first_find_reverse = self.FindInPage(
        search_string, find_next=True, forward=False)
    # We find next and we go back so find coordinates should be the same
    # as previous ones.
    self.assertEqual(first_find, first_find_reverse,
                     'First occurrence must be selected, since we went back')

  def testSpecialChars(self):
    """Test find in page with unicode and special characters.

    Finds from page content, comments and meta data and verifies that comments
    and meta data are not searchable.
    """
    search_string = u'\u5728\u897f\u660c\u536b\u661f\u53d1'
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'specialchar.html'))
    self.NavigateToURL(url)
    self.assertEqual(4, self.FindInPage(search_string)['match_count'])
    search_string = u'240^*&%!#~!*&\u518d\u5c31\u8077\u624b\u5f53'
    self.assertEqual(2, self.FindInPage(search_string)['match_count'])
    # Find for the special chars in the comment and in the meta tag
    search_string = u'\u4e2d\u65b0\u793e\u8bb0\u8005\u5b8b\u5409'\
                  u'\u6cb3\u6444\u4e2d\u65b0\u7f51'
    self.assertEqual(0, self.FindInPage(search_string)['match_count'],
                     'Chrome should not find chars from comment or meta tags')

  def testFindInLargePage(self):
    """Find in a very large page"""
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'largepage.html'))
    self.NavigateToURL(url)
    self.assertEqual(373, self.FindInPage('daughter of Prince')['match_count'])

  def testFindLongString(self):
    """Find a very long string in a large page"""
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'largepage.html'))
    self.NavigateToURL(url)
    file = codecs.open(os.path.join(self.DataDir(), self.find_test_data_dir,
                                    'LongFind.txt'), 'r', 'utf-8')
    search = file.read()
    self.assertEqual(1, self.FindInPage(search)['match_count'])

  def testFindBigString(self):
    """Find a big font string in a page"""
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'BigText.html'))
    self.NavigateToURL(url)
    self.assertEqual(1, self.FindInPage('SomeLargeString')['match_count'])

  def testVariousFindTests(self):
    """Test find in page for <span> style text, lists, html comments, etc."""
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'FindRandomTests.html'))
    self.NavigateToURL(url)
    search = 'has light blue eyes and my father has dark'
    self.assertEqual(1, self.FindInPage(search)['match_count'],
                     'Failed to find text with <span> tag')
    # Find for list items
    search = 'Google\nApple\nandroid'
    self.assertEqual(1, self.FindInPage(search)['match_count'],
                     'Failed to find the list items')
    # Find HTML comments
    self.assertEqual(0, self.FindInPage('example comment')['match_count'],
                     'We should not find HTML comments')

  def testFindWholeFileContent(self):
    """Find the whole text file page and find count should be 1"""
    find_test_file = os.path.join(self.DataDir(), self.find_test_data_dir,
                                  'find_test.txt')
    url = self.GetFileURLForPath(find_test_file)
    self.NavigateToURL(url)
    file = open(find_test_file)
    search = file.read()
    self.assertEqual(1, self.FindInPage(search)['match_count'],
                     'Failed to find the whole page')

  def testSingleOccurrence(self):
    """Search Back and Forward on a single occurrence"""
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), self.find_test_data_dir, 'FindRandomTests.html'))
    self.NavigateToURL(url)
    self.assertEqual(1, self.FindInPage('2010 Pro Bowl')['match_count'])
    # First occurrence find
    first_occurence_dict = self.FindInPage('2010 Pro Bowl')
    # Finding next occurrence
    next_occurence_dict = self.FindInPage('2010 Pro Bowl', find_next = True)
    self.assertEqual(first_occurence_dict, next_occurence_dict,
                     'We have only one occurrence in this page so'
                     'first and next coordinates must be same')
    # Doing a fake find so we have no previous search
    self.FindInPage('ghgfjgfh201232rere')

    first_occurence_dict = self.FindInPage('2010 Pro Bowl')
    # Finding previous occurrence
    back_occurence_dict = self.FindInPage('2010 Pro Bowl',
                                          find_next = True, forward = False)
    self.assertEqual(first_occurence_dict, back_occurence_dict,
                     'We have only one occurrence in this page so '
                     'first and back search coordinates must be same')

  def testSearchInPDF(self):
    """Verify that we can find in a pdf file.

    Only for Google Chrome builds (Chromium builds do not have internal pdf).
    """
    # bail out if not a branded build
    properties = self.GetBrowserInfo()['properties']
    if properties['branding'] != 'Google Chrome':
      return
    url = self.GetFileURLForPath(os.path.join(
        self.DataDir(), 'plugin', 'Embed.pdf'))
    self.NavigateToURL(url)
    search_count = self.FindInPage('adobe')['match_count']
    self.assertEqual(8, search_count, 'Failed to find in the pdf file')


if __name__ == '__main__':
  pyauto_functional.Main()