1 # Copyright (c) 2011 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.
5 """Python representation for Chromium Omnibox.
7 Obtain one of these from PyUITestSuite::GetOmniboxInfo() call.
10 class MyTest(pyauto.PyUITest):
12 info = self.OmniboxInfo() # fetch omnibox snapshot
15 See more tests in chrome/test/functional/omnibox.py.
18 import simplejson
as json
20 from pyauto_errors
import JSONInterfaceError
23 class OmniboxInfo(object):
24 """Represent info for Chromium Omnibox.
27 - a list of matches in the same order as you'd see in the omnibox,
28 - a dictionary of properties related to the omnibox.
34 u'contents': u'google',
35 u'description': u'Google Search',
36 u'destination_url': u'http://www.google.com/search?aq=f&'
37 'sourceid=chrome&ie=UTF-8&q=google',
39 u'type': u'search-what-you-typed'},
41 u'contents': u'maps.google.com/',
42 u'description': u'Google Maps',
43 u'destination_url': u'http://maps.google.com/',
45 u'type': u'navsuggest'},
46 { u'contents': u'google maps',
48 u'destination_url': u'http://www.google.com/search?aq=0&oq=google&'
49 'sourceid=chrome&ie=UTF-8&q=google+maps',
51 u'type': u'search-suggest'},
52 { u'contents': u'google earth',
54 u'destination_url': u'http://www.google.com/search?aq=1&oq=google&'
55 'sourceid=chrome&ie=UTF-8&q=google+earth',
57 u'type': u'search-suggest'},
58 { u'contents': u'Search Google for <enter query>',
59 u'description': u'(Keyword: google.com)',
60 u'destination_url': u'',
62 u'type': u'search-other-engine'}],
64 u'properties': { u'has_focus': True,
66 u'query_in_progress': False,
69 def __init__(self
, omnibox_dict
):
70 """Initialize a OmniboxInfo from a json string.
73 omnibox_dict: returned by an IPC call for the command 'GetOmniboxInfo'.
76 pyauto_errors.JSONInterfaceError if the automation call returns an error.
78 # JSON string prepared in GetOmniboxInfo() in automation_provider.cc
79 self
.omniboxdict
= omnibox_dict
80 if self
.omniboxdict
.has_key('error'):
81 raise JSONInterfaceError(self
.omniboxdict
['error'])
84 """Get omnibox matches.
87 a list of omnibox match items.
89 return self
.omniboxdict
.get('matches', [])
91 def MatchesWithAttributes(self
, attr_dict
):
92 """Find all omnibox matches which match the attributes in |attr_dict|.
95 attr_dict: a dictionary of attributes to be satisfied.
96 All attributes in the given dictionary should be satisfied.
98 { 'destiantion_url': 'http://www.google.com/',
99 'description': 'Google' }
102 a list of omnibox match items.
105 for item
in self
.Matches():
107 for key
, val
in attr_dict
.iteritems():
108 if not item
.has_key(key
) or item
[key
] != val
:
114 def Properties(self
, key
=None):
115 """Get the properties
118 key: if specified, value for the given property is returned.
121 a dictionary of properties if no key is given, OR
122 value corresponding to a particular property if key is given
124 all
= self
.omniboxdict
.get('properties')
130 """Get the text in the omnibox.
132 This need not be the same as the user-inputted text, since omnibox may
133 autocomplete some URLs, or the user may move omnibox popup selection
136 return self
.Properties('text')
138 def IsQueryInProgress(self
):
139 """Determine if a query is in progress."""
140 return self
.Properties('query_in_progress')