Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / test / pyautolib / omnibox_info.py
blob5f112558bc1b257382de2de09c3270410e885a54
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.
9 Example:
10 class MyTest(pyauto.PyUITest):
11 def testBasic(self):
12 info = self.OmniboxInfo() # fetch omnibox snapshot
13 print info.Matches()
15 See more tests in chrome/test/functional/omnibox.py.
16 """
18 import simplejson as json
20 from pyauto_errors import JSONInterfaceError
23 class OmniboxInfo(object):
24 """Represent info for Chromium Omnibox.
26 Info contains:
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.
30 Sample info text:
32 { u'matches': [
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',
38 u'starred': False,
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/',
44 u'starred': False,
45 u'type': u'navsuggest'},
46 { u'contents': u'google maps',
47 u'description': u'',
48 u'destination_url': u'http://www.google.com/search?aq=0&oq=google&'
49 'sourceid=chrome&ie=UTF-8&q=google+maps',
50 u'starred': False,
51 u'type': u'search-suggest'},
52 { u'contents': u'google earth',
53 u'description': u'',
54 u'destination_url': u'http://www.google.com/search?aq=1&oq=google&'
55 'sourceid=chrome&ie=UTF-8&q=google+earth',
56 u'starred': False,
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'',
61 u'starred': False,
62 u'type': u'search-other-engine'}],
64 u'properties': { u'has_focus': True,
65 u'keyword': u'',
66 u'query_in_progress': False,
67 u'text': u'google'}}
68 """
69 def __init__(self, omnibox_dict):
70 """Initialize a OmniboxInfo from a json string.
72 Args:
73 omnibox_dict: returned by an IPC call for the command 'GetOmniboxInfo'.
75 Raises:
76 pyauto_errors.JSONInterfaceError if the automation call returns an error.
77 """
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'])
83 def Matches(self):
84 """Get omnibox matches.
86 Returns:
87 a list of omnibox match items.
88 """
89 return self.omniboxdict.get('matches', [])
91 def MatchesWithAttributes(self, attr_dict):
92 """Find all omnibox matches which match the attributes in |attr_dict|.
94 Args:
95 attr_dict: a dictionary of attributes to be satisfied.
96 All attributes in the given dictionary should be satisfied.
97 example:
98 { 'destiantion_url': 'http://www.google.com/',
99 'description': 'Google' }
101 Returns:
102 a list of omnibox match items.
104 out = []
105 for item in self.Matches():
106 matched = True
107 for key, val in attr_dict.iteritems():
108 if not item.has_key(key) or item[key] != val:
109 matched = False
110 if matched:
111 out.append(item)
112 return out
114 def Properties(self, key=None):
115 """Get the properties
117 Args:
118 key: if specified, value for the given property is returned.
120 Returns:
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')
125 if not key:
126 return all
127 return all.get(key)
129 def Text(self):
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
134 up/down.
136 return self.Properties('text')
138 def IsQueryInProgress(self):
139 """Determine if a query is in progress."""
140 return self.Properties('query_in_progress')