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. 4 5 #include "chrome/test/chromedriver/alert_commands.h" 6 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" 15 16 Status ExecuteAlertCommand( 17 const AlertCommand& alert_command, 18 Session* session, 19 const base::DictionaryValue& params, 20 scoped_ptr<base::Value>* value) { 21 WebView* web_view = NULL; 22 Status status = session->GetTargetWindow(&web_view); 23 if (status.IsError()) 24 return status; 25 26 status = web_view->ConnectIfNecessary(); 27 if (status.IsError()) 28 return status; 29 30 status = web_view->HandleReceivedEvents(); 31 if (status.IsError()) 32 return status; 33 34 status = web_view->WaitForPendingNavigations( 35 session->GetCurrentFrameId(), session->page_load_timeout, true); 36 if (status.IsError() && status.code() != kUnexpectedAlertOpen) 37 return status; 38 39 return alert_command.Run(session, web_view, params, value); 40 } 41 42 Status ExecuteGetAlert( 43 Session* session, 44 WebView* web_view, 45 const base::DictionaryValue& params, 46 scoped_ptr<base::Value>* value) { 47 value->reset(base::Value::CreateBooleanValue( 48 web_view->GetJavaScriptDialogManager()->IsDialogOpen())); 49 return Status(kOk); 50 } 51 52 Status ExecuteGetAlertText( 53 Session* session, 54 WebView* web_view, 55 const base::DictionaryValue& params, 56 scoped_ptr<base::Value>* value) { 57 std::string message; 58 Status status = 59 web_view->GetJavaScriptDialogManager()->GetDialogMessage(&message); 60 if (status.IsError()) 61 return status; 62 value->reset(base::Value::CreateStringValue(message)); 63 return Status(kOk); 64 } 65 66 Status ExecuteSetAlertValue( 67 Session* session, 68 WebView* web_view, 69 const base::DictionaryValue& params, 70 scoped_ptr<base::Value>* value) { 71 std::string text; 72 if (!params.GetString("text", &text)) 73 return Status(kUnknownError, "missing or invalid 'text'"); 74 75 if (!web_view->GetJavaScriptDialogManager()->IsDialogOpen()) 76 return Status(kNoAlertOpen); 77 78 session->prompt_text.reset(new std::string(text)); 79 return Status(kOk); 80 } 81 82 Status ExecuteAcceptAlert( 83 Session* session, 84 WebView* web_view, 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(); 90 return status; 91 } 92 93 Status ExecuteDismissAlert( 94 Session* session, 95 WebView* web_view, 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(); 101 return status; 102 } 103