OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_H_ | |
6 #define CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/synchronization/waitable_event.h" | |
15 #include "base/threading/platform_thread.h" | |
16 #include "base/threading/thread.h" | |
17 #include "base/time/time.h" | |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/common/automation_constants.h" | |
20 #include "chrome/test/automation/automation_handle_tracker.h" | |
21 #include "chrome/test/automation/browser_proxy.h" | |
22 #include "ipc/ipc_channel_proxy.h" | |
23 #include "ipc/ipc_sender.h" | |
24 #include "ipc/ipc_sync_channel.h" | |
25 #include "ui/base/ui_base_types.h" | |
26 #include "ui/gfx/native_widget_types.h" | |
27 #include "url/gurl.h" | |
28 | |
29 class BrowserProxy; | |
30 class TabProxy; | |
31 | |
32 // This is an interface that AutomationProxy-related objects can use to | |
33 // access the message-sending abilities of the Proxy. | |
34 class AutomationMessageSender : public IPC::Sender { | |
35 public: | |
36 // Sends a message synchronously; it doesn't return until a response has been | |
37 // received or a timeout has expired. | |
38 // | |
39 // The function returns true if a response is received, and returns false if | |
40 // there is a failure or timeout (in milliseconds). | |
41 // | |
42 // NOTE: When timeout occurs, the connection between proxy provider may be | |
43 // in transit state. Specifically, there might be pending IPC messages, | |
44 // and the proxy provider might be still working on the previous | |
45 // request. | |
46 virtual bool Send(IPC::Message* message) = 0; | |
47 virtual bool Send(IPC::Message* message, int timeout_ms) = 0; | |
48 }; | |
49 | |
50 // This is the interface that external processes can use to interact with | |
51 // a running instance of the app. | |
52 class AutomationProxy : public IPC::Listener, public AutomationMessageSender { | |
53 public: | |
54 AutomationProxy(base::TimeDelta action_timeout, bool disconnect_on_failure); | |
55 virtual ~AutomationProxy(); | |
56 | |
57 // Creates a previously unused channel id. | |
58 static std::string GenerateChannelID(); | |
59 | |
60 // Initializes a channel for a connection to an AutomationProvider. | |
61 // If use_named_interface is false, it will act as a client | |
62 // and connect to the named IPC socket with channel_id as its path. | |
63 // If use_named_interface is true, it will act as a server and | |
64 // use an anonymous socketpair instead. | |
65 void InitializeChannel(const std::string& channel_id, | |
66 bool use_named_interface); | |
67 | |
68 // IPC callback | |
69 virtual bool OnMessageReceived(const IPC::Message& msg); | |
70 virtual void OnChannelError(); | |
71 | |
72 // Close the automation IPC channel. | |
73 void Disconnect(); | |
74 | |
75 // Waits for the app to launch and the automation provider to say hello | |
76 // (the app isn't fully done loading by this point). | |
77 // Returns SUCCESS if the launch is successful. | |
78 // Returns TIMEOUT if there was no response by action_timeout_ | |
79 // Returns VERSION_MISMATCH if the automation protocol version of the | |
80 // automation provider does not match and if perform_version_check_ is set | |
81 // to true. Note that perform_version_check_ defaults to false, call | |
82 // set_perform_version_check() to set it. | |
83 AutomationLaunchResult WaitForAppLaunch(); | |
84 | |
85 // See description in AutomationMsg_WaitForProcessLauncherThreadToGoIdle. | |
86 bool WaitForProcessLauncherThreadToGoIdle() WARN_UNUSED_RESULT; | |
87 | |
88 // Waits for any initial page loads to complete. | |
89 // NOTE: this only fires once for a run of the application. | |
90 // Returns true if the load is successful | |
91 bool WaitForInitialLoads() WARN_UNUSED_RESULT; | |
92 | |
93 // Waits for the initial destinations tab to report that it has finished | |
94 // querying. |load_time| is filled in with how long it took, in milliseconds. | |
95 // NOTE: this only fires once for a run of the application. | |
96 // Returns true if the load is successful. | |
97 bool WaitForInitialNewTabUILoad(int* load_time) WARN_UNUSED_RESULT; | |
98 | |
99 // Open a new browser window of type |type|, returning true on success. |show| | |
100 // identifies whether the window should be shown. Returns true on success. | |
101 bool OpenNewBrowserWindow(Browser::Type type, bool show) WARN_UNUSED_RESULT; | |
102 | |
103 // Fills the number of open browser windows into the given variable, returning | |
104 // true on success. False likely indicates an IPC error. | |
105 bool GetBrowserWindowCount(int* num_windows) WARN_UNUSED_RESULT; | |
106 | |
107 // Block the thread until the window count becomes the provided value. | |
108 // Returns true on success. | |
109 bool WaitForWindowCountToBecome(int target_count) WARN_UNUSED_RESULT; | |
110 | |
111 // Fills the number of open normal browser windows (normal type and | |
112 // non-incognito mode) into the given variable, returning true on success. | |
113 // False likely indicates an IPC error. | |
114 bool GetNormalBrowserWindowCount(int* num_windows) WARN_UNUSED_RESULT; | |
115 | |
116 // Returns true if one of the tabs in any window displays given url. | |
117 bool IsURLDisplayed(GURL url) WARN_UNUSED_RESULT; | |
118 | |
119 // Get the duration of the last |event_name| in the browser. Returns | |
120 // false if the IPC failed to send. | |
121 bool GetMetricEventDuration(const std::string& event_name, | |
122 int* duration_ms) WARN_UNUSED_RESULT; | |
123 | |
124 // Returns the BrowserProxy for the browser window at the given index, | |
125 // transferring ownership of the pointer to the caller. | |
126 // On failure, returns NULL. | |
127 // | |
128 // Use GetBrowserWindowCount to see how many browser windows you can ask for. | |
129 // Window numbers are 0-based. | |
130 scoped_refptr<BrowserProxy> GetBrowserWindow(int window_index); | |
131 | |
132 // These methods are intended to be called by the background thread | |
133 // to signal that the given event has occurred, and that any corresponding | |
134 // Wait... function can return. | |
135 void SignalAppLaunch(const std::string& version_string); | |
136 void SignalInitialLoads(); | |
137 // load_time is how long, in ms, the tab contents took to load. | |
138 void SignalNewTabUITab(int load_time); | |
139 | |
140 // Gets the next extension test result in |result|. Returns false if there | |
141 // was a problem sending the result querying RPC. | |
142 bool GetExtensionTestResult(bool* result, std::string* message); | |
143 | |
144 // Generic pattern for sending automation requests. | |
145 bool SendJSONRequest(const std::string& request, | |
146 int timeout_ms, | |
147 std::string* response) WARN_UNUSED_RESULT; | |
148 | |
149 // Begin tracing specified category_patterns on the browser instance. Blocks | |
150 // until browser acknowledges that tracing has begun (or failed if false is | |
151 // returned). |category_patterns| is a comma-delimited list of category | |
152 // wildcards. | |
153 // A category pattern can have an optional '-' prefix to exclude category | |
154 // groups that contain matching category. | |
155 // Either all category_patterns must be included or all must be excluded. | |
156 // | |
157 // Example: BeginTracing("test_MyTest*"); | |
158 // Example: BeginTracing("test_MyTest*,test_OtherStuff"); | |
159 // Example: BeginTracing("-excluded_category1,-excluded_category2"); | |
160 // | |
161 // See base/event_trace.h for documentation of included and excluded | |
162 // category_patterns. | |
163 bool BeginTracing(const std::string& category_patterns) WARN_UNUSED_RESULT; | |
164 | |
165 // End trace and collect the trace output as a json string. | |
166 bool EndTracing(std::string* json_trace_output) WARN_UNUSED_RESULT; | |
167 | |
168 IPC::SyncChannel* channel(); | |
169 | |
170 // AutomationMessageSender implementation. | |
171 virtual bool Send(IPC::Message* message) WARN_UNUSED_RESULT; | |
172 virtual bool Send(IPC::Message* message, int timeout_ms) WARN_UNUSED_RESULT; | |
173 | |
174 // Wrapper over AutomationHandleTracker::InvalidateHandle. Receives the | |
175 // message from AutomationProxy, unpacks the messages and routes that call to | |
176 // the tracker. | |
177 virtual void InvalidateHandle(const IPC::Message& message); | |
178 | |
179 base::TimeDelta action_timeout() const { | |
180 return action_timeout_; | |
181 } | |
182 | |
183 // Sets the timeout for subsequent automation calls. | |
184 void set_action_timeout(base::TimeDelta timeout) { | |
185 DCHECK(timeout <= base::TimeDelta::FromMinutes(10)) | |
186 << "10+ min of automation timeout " | |
187 "can make the test hang and be killed by buildbot"; | |
188 action_timeout_ = timeout; | |
189 } | |
190 | |
191 // Returns the server version of the server connected. You may only call this | |
192 // method after WaitForAppLaunch() has returned SUCCESS or VERSION_MISMATCH. | |
193 // If you call it before this, the return value is undefined. | |
194 std::string server_version() const { | |
195 return server_version_; | |
196 } | |
197 | |
198 // Call this while passing true to tell the automation proxy to perform | |
199 // a version check when WaitForAppLaunch() is called. Note that | |
200 // platform_version_check_ defaults to false. | |
201 void set_perform_version_check(bool perform_version_check) { | |
202 perform_version_check_ = perform_version_check; | |
203 } | |
204 | |
205 // These functions set and reset the IPC::Channel pointer on the tracker. | |
206 void SetChannel(IPC::Channel* channel); | |
207 void ResetChannel(); | |
208 | |
209 // See description above |channel_disconnected_on_failure_|. | |
210 bool channel_disconnected_on_failure() const { | |
211 return channel_disconnected_on_failure_; | |
212 } | |
213 | |
214 protected: | |
215 template <class T> scoped_refptr<T> ProxyObjectFromHandle(int handle); | |
216 void InitializeThread(); | |
217 void InitializeHandleTracker(); | |
218 | |
219 scoped_ptr<base::Thread> thread_; | |
220 scoped_ptr<IPC::SyncChannel> channel_; | |
221 scoped_ptr<AutomationHandleTracker> tracker_; | |
222 | |
223 base::WaitableEvent app_launched_; | |
224 base::WaitableEvent initial_loads_complete_; | |
225 base::WaitableEvent new_tab_ui_load_complete_; | |
226 int new_tab_ui_load_time_; | |
227 | |
228 // An event that notifies when we are shutting-down. | |
229 scoped_ptr<base::WaitableEvent> shutdown_event_; | |
230 | |
231 // The version of the automation provider we are communicating with. | |
232 std::string server_version_; | |
233 | |
234 // Whether to perform a version check between the automation proxy and | |
235 // the automation provider at connection time. Defaults to false, you can | |
236 // set this to true if building the automation proxy into a module with | |
237 // a version resource. | |
238 bool perform_version_check_; | |
239 | |
240 // If true, the proxy will disconnect the IPC channel on first failure | |
241 // to send an IPC message. This helps avoid long timeouts in tests. | |
242 bool disconnect_on_failure_; | |
243 | |
244 // Set if disconnect_on_failure_ caused the connection to be dropped. | |
245 bool channel_disconnected_on_failure_; | |
246 | |
247 // Delay to let the browser execute the command. | |
248 base::TimeDelta action_timeout_; | |
249 | |
250 base::PlatformThreadId listener_thread_id_; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(AutomationProxy); | |
253 }; | |
254 | |
255 #endif // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_H_ | |
OLD | NEW |