- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / pyautolib / omnibox_info.py
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.
4
5 """Python representation for Chromium Omnibox.
6
7 Obtain one of these from PyUITestSuite::GetOmniboxInfo() call.
8
9 Example:
10 class MyTest(pyauto.PyUITest):
11   def testBasic(self):
12     info = self.OmniboxInfo()  # fetch omnibox snapshot
13     print info.Matches()
14
15 See more tests in chrome/test/functional/omnibox.py.
16 """
17
18 import simplejson as json
19
20 from pyauto_errors import JSONInterfaceError
21
22
23 class OmniboxInfo(object):
24   """Represent info for Chromium Omnibox.
25
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.
29
30   Sample info text:
31
32   { u'matches': [
33     {
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'},
40     {
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'}],
63
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.
71
72     Args:
73       omnibox_dict: returned by an IPC call for the command 'GetOmniboxInfo'.
74
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'])
82
83   def Matches(self):
84     """Get omnibox matches.
85
86     Returns:
87       a list of omnibox match items.
88     """
89     return self.omniboxdict.get('matches', [])
90
91   def MatchesWithAttributes(self, attr_dict):
92     """Find all omnibox matches which match the attributes in |attr_dict|.
93
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' }
100
101     Returns:
102       a list of omnibox match items.
103     """
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
113
114   def Properties(self, key=None):
115     """Get the properties
116
117     Args:
118       key: if specified, value for the given property is returned.
119
120     Returns:
121       a dictionary of properties if no key is given, OR
122       value corresponding to a particular property if key is given
123     """
124     all = self.omniboxdict.get('properties')
125     if not key:
126       return all
127     return all.get(key)
128
129   def Text(self):
130     """Get the text in the omnibox.
131
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.
135     """
136     return self.Properties('text')
137
138   def IsQueryInProgress(self):
139     """Determine if a query is in progress."""
140     return self.Properties('query_in_progress')