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
|
#!/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.
"""Python representation for Chromium Omnibox.
Obtain one of these from PyUITestSuite::GetOmniboxInfo() call.
Example:
class MyTest(pyauto.PyUITest):
def testBasic(self):
info = self.OmniboxInfo() # fetch omnibox snapshot
print info.Matches()
See more tests in chrome/test/functional/omnibox.py.
"""
import simplejson as json
from pyauto_errors import JSONInterfaceError
class OmniboxInfo(object):
"""Represent info for Chromium Omnibox.
Info contains:
- a list of matches in the same order as you'd see in the omnibox,
- a dictionary of properties related to the omnibox.
Sample info text:
{ u'matches': [
{
u'contents': u'google',
u'description': u'Google Search',
u'destination_url': u'http://www.google.com/search?aq=f&'
'sourceid=chrome&ie=UTF-8&q=google',
u'starred': False,
u'type': u'search-what-you-typed'},
{
u'contents': u'maps.google.com/',
u'description': u'Google Maps',
u'destination_url': u'http://maps.google.com/',
u'starred': False,
u'type': u'navsuggest'},
{ u'contents': u'google maps',
u'description': u'',
u'destination_url': u'http://www.google.com/search?aq=0&oq=google&'
'sourceid=chrome&ie=UTF-8&q=google+maps',
u'starred': False,
u'type': u'search-suggest'},
{ u'contents': u'google earth',
u'description': u'',
u'destination_url': u'http://www.google.com/search?aq=1&oq=google&'
'sourceid=chrome&ie=UTF-8&q=google+earth',
u'starred': False,
u'type': u'search-suggest'},
{ u'contents': u'Search Google for <enter query>',
u'description': u'(Keyword: google.com)',
u'destination_url': u'',
u'starred': False,
u'type': u'search-other-engine'}],
u'properties': { u'has_focus': True,
u'keyword': u'',
u'query_in_progress': False,
u'text': u'google'}}
"""
def __init__(self, json_string):
"""Initialize a OmniboxInfo from a json string.
Args:
json_string: a json string, as returned by a json ipc call for the
command 'GetOmniboxInfo'
Raises:
pyauto_errors.JSONInterfaceError if the automation call returns an error.
"""
# JSON string prepared in GetOmniboxInfo() in automation_provider.cc
self.omniboxdict = json.loads(json_string)
if self.omniboxdict.has_key('error'):
raise JSONInterfaceError(self.omniboxdict['error'])
def Matches(self):
"""Get omnibox matches.
Returns:
a list of omnibox match items.
"""
return self.omniboxdict.get('matches', [])
def MatchesWithAttributes(self, attr_dict):
"""Find all omnibox matches which match the attributes in |attr_dict|.
Args:
attr_dict: a dictionary of attributes to be satisfied.
All attributes in the given dictionary should be satisfied.
example:
{ 'destiantion_url': 'http://www.google.com/',
'description': 'Google' }
Returns:
a list of omnibox match items.
"""
out = []
for item in self.Matches():
matched = True
for key, val in attr_dict.iteritems():
if not item.has_key(key) or item[key] != val:
matched = False
if matched:
out.append(item)
return out
def Properties(self, key=None):
"""Get the properties
Args:
key: if specified, value for the given property is returned.
Returns:
a dictionary of properties if no key is given, OR
value corresponding to a particular property if key is given
"""
all = self.omniboxdict.get('properties')
if not key:
return all
return all.get(key)
def Text(self):
"""Get the text in the omnibox.
This need not be the same as the user-inputted text, since omnibox may
autocomplete some URLs, or the user may move omnibox popup selection
up/down.
"""
return self.Properties('text')
def IsQueryInProgress(self):
"""Determine if a query is in progress."""
return self.Properties('query_in_progress')
|