1 // Copyright (c) 2013 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 #include "chrome/test/chromedriver/alert_commands.h"
7 #include "base/callback.h"
8 #include "base/values.h"
9 #include "chrome/test/chromedriver/chrome/chrome.h"
10 #include "chrome/test/chromedriver/chrome/devtools_client.h"
11 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h"
12 #include "chrome/test/chromedriver/chrome/status.h"
13 #include "chrome/test/chromedriver/chrome/web_view.h"
14 #include "chrome/test/chromedriver/session.h"
16 Status
ExecuteAlertCommand(
17 const AlertCommand
& alert_command
,
19 const base::DictionaryValue
& params
,
20 scoped_ptr
<base::Value
>* value
) {
21 WebView
* web_view
= NULL
;
22 Status status
= session
->GetTargetWindow(&web_view
);
26 status
= web_view
->ConnectIfNecessary();
30 status
= web_view
->HandleReceivedEvents();
34 status
= web_view
->WaitForPendingNavigations(
35 session
->GetCurrentFrameId(), session
->page_load_timeout
, true);
36 if (status
.IsError() && status
.code() != kUnexpectedAlertOpen
)
39 return alert_command
.Run(session
, web_view
, params
, value
);
42 Status
ExecuteGetAlert(
45 const base::DictionaryValue
& params
,
46 scoped_ptr
<base::Value
>* value
) {
47 value
->reset(new base::FundamentalValue(
48 web_view
->GetJavaScriptDialogManager()->IsDialogOpen()));
52 Status
ExecuteGetAlertText(
55 const base::DictionaryValue
& params
,
56 scoped_ptr
<base::Value
>* value
) {
59 web_view
->GetJavaScriptDialogManager()->GetDialogMessage(&message
);
62 value
->reset(new base::StringValue(message
));
66 Status
ExecuteSetAlertValue(
69 const base::DictionaryValue
& params
,
70 scoped_ptr
<base::Value
>* value
) {
72 if (!params
.GetString("text", &text
))
73 return Status(kUnknownError
, "missing or invalid 'text'");
75 if (!web_view
->GetJavaScriptDialogManager()->IsDialogOpen())
76 return Status(kNoAlertOpen
);
78 session
->prompt_text
.reset(new std::string(text
));
82 Status
ExecuteAcceptAlert(
85 const base::DictionaryValue
& params
,
86 scoped_ptr
<base::Value
>* value
) {
87 Status status
= web_view
->GetJavaScriptDialogManager()
88 ->HandleDialog(true, session
->prompt_text
.get());
89 session
->prompt_text
.reset();
93 Status
ExecuteDismissAlert(
96 const base::DictionaryValue
& params
,
97 scoped_ptr
<base::Value
>* value
) {
98 Status status
= web_view
->GetJavaScriptDialogManager()
99 ->HandleDialog(false, session
->prompt_text
.get());
100 session
->prompt_text
.reset();