summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/autocomplete_edit_proxy.h
blob: e33824a921f9513e2a92b859c01eb1c419715a97 (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
// 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.

#ifndef CHROME_TEST_AUTOMATION_AUTOCOMPLETE_EDIT_PROXY_H_
#define CHROME_TEST_AUTOMATION_AUTOCOMPLETE_EDIT_PROXY_H_
#pragma once

#include <string>
#include <vector>

#include "base/string_number_conversions.h"
#include "chrome/browser/autocomplete/autocomplete.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/test/automation/automation_handle_tracker.h"
#include "googleurl/src/gurl.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_utils.h"

// The purpose of this class is to act as a serializable version of
// AutocompleteMatch. The reason for this class is because we don't want to
// serialize all elements of AutocompleteMatch and we want some data from the
// autocomplete provider without the hassle of serializing it.
struct AutocompleteMatchData {
 public:
  AutocompleteMatchData()
      : relevance(0),
        deletable(false),
        inline_autocomplete_offset(0),
        is_history_what_you_typed_match(false),
        starred(false) {
  }

  explicit AutocompleteMatchData(const AutocompleteMatch& match)
      : provider_name(match.provider->name()),
        relevance(match.relevance),
        deletable(match.deletable),
        fill_into_edit(match.fill_into_edit),
        inline_autocomplete_offset(match.inline_autocomplete_offset),
        destination_url(match.destination_url),
        contents(match.contents),
        description(match.description),
        is_history_what_you_typed_match(match.is_history_what_you_typed_match),
        type(AutocompleteMatch::TypeToString(match.type)),
        starred(match.starred) {
  }

  std::string provider_name;
  int relevance;
  bool deletable;
  std::wstring fill_into_edit;
  size_t inline_autocomplete_offset;
  GURL destination_url;
  std::wstring contents;
  std::wstring description;
  bool is_history_what_you_typed_match;
  std::string type;
  bool starred;
};
typedef std::vector<AutocompleteMatchData> Matches;

namespace IPC {

template <>
struct ParamTraits<AutocompleteMatchData> {
  typedef AutocompleteMatchData param_type;
  static void Write(Message* m, const param_type& p) {
    m->WriteString(p.provider_name);
    m->WriteInt(p.relevance);
    m->WriteBool(p.deletable);
    m->WriteWString(p.fill_into_edit);
    m->WriteSize(p.inline_autocomplete_offset);
    m->WriteString(p.destination_url.possibly_invalid_spec());
    m->WriteWString(p.contents);
    m->WriteWString(p.description);
    m->WriteBool(p.is_history_what_you_typed_match);
    m->WriteString(p.type);
    m->WriteBool(p.starred);
  }

  static bool Read(const Message* m, void** iter, param_type* r) {
    std::string destination_url;
    if (!m->ReadString(iter, &r->provider_name) ||
        !m->ReadInt(iter, &r->relevance) ||
        !m->ReadBool(iter, &r->deletable) ||
        !m->ReadWString(iter, &r->fill_into_edit) ||
        !m->ReadSize(iter, &r->inline_autocomplete_offset) ||
        !m->ReadString(iter, &destination_url) ||
        !m->ReadWString(iter, &r->contents) ||
        !m->ReadWString(iter, &r->description) ||
        !m->ReadBool(iter, &r->is_history_what_you_typed_match) ||
        !m->ReadString(iter, &r->type) ||
        !m->ReadBool(iter, &r->starred))
      return false;
    r->destination_url = GURL(destination_url);
    return true;
  }

  static void Log(const param_type& p, std::string* l) {
    l->append("[");
    l->append(p.provider_name);
    l->append(" ");
    l->append(base::IntToString(p.relevance));
    l->append(" ");
    l->append(p.deletable ? "true" : "false");
    l->append(" ");
    LogParam(p.fill_into_edit, l);
    l->append(" ");
    l->append(base::IntToString(p.inline_autocomplete_offset));
    l->append(" ");
    l->append(p.destination_url.spec());
    l->append(" ");
    LogParam(p.contents, l);
    l->append(" ");
    LogParam(p.description, l);
    l->append(" ");
    l->append(p.is_history_what_you_typed_match ? "true" : "false");
    l->append(" ");
    l->append(p.type);
    l->append(" ");
    l->append(p.starred ? "true" : "false");
    l->append("]");
  }
};
}  // namespace IPC

class AutocompleteEditProxy : public AutomationResourceProxy {
 public:
  AutocompleteEditProxy(AutomationMessageSender* sender,
                        AutomationHandleTracker* tracker,
                        int handle)
      : AutomationResourceProxy(tracker, sender, handle) {}
  virtual ~AutocompleteEditProxy() {}

  // All these functions return true if the autocomplete edit is valid and
  // there are no IPC errors.

  // Gets the text visible in the omnibox.
  bool GetText(std::wstring* text) const;

  // Sets the text visible in the omnibox.
  bool SetText(const std::wstring& text);

  // Determines if a query to an autocomplete provider is still in progress.
  // NOTE: No autocomplete queries will be made if the omnibox doesn't have
  // focus.  This can be achieved by sending a IDC_FOCUS_LOCATION accelerator
  // to the browser.
  bool IsQueryInProgress(bool* query_in_progress) const;

  // Gets a list of autocomplete matches that have been gathered so far.
  bool GetAutocompleteMatches(Matches* matches) const;

  // Waits for the autocomplete edit to receive focus.
  bool WaitForFocus() const;

  // Waits for all queries to autocomplete providers to complete.
  // |wait_timeout_ms| gives the number of milliseconds to wait for the query
  // to finish. Returns false if IPC call failed or if the function times out.
  bool WaitForQuery(int wait_timeout_ms) const;

 private:
  DISALLOW_COPY_AND_ASSIGN(AutocompleteEditProxy);
};

#endif  // CHROME_TEST_AUTOMATION_AUTOCOMPLETE_EDIT_PROXY_H_