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