1 // Copyright 2019 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 "weblayer/test/weblayer_browser_test_utils.h"
6 
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/bind.h"
9 #include "url/gurl.h"
10 #include "weblayer/browser/tab_impl.h"
11 #include "weblayer/public/navigation_controller.h"
12 #include "weblayer/public/tab.h"
13 #include "weblayer/shell/browser/shell.h"
14 #include "weblayer/test/stub_autofill_provider.h"
15 #include "weblayer/test/test_navigation_observer.h"
16 
17 namespace weblayer {
18 
19 namespace {
20 
21 // Navigates to |url| in |tab| and waits for |event| to occur.
NavigateAndWaitForEvent(const GURL & url,Tab * tab,TestNavigationObserver::NavigationEvent event)22 void NavigateAndWaitForEvent(const GURL& url,
23                              Tab* tab,
24                              TestNavigationObserver::NavigationEvent event) {
25   TestNavigationObserver test_observer(url, event, tab);
26   tab->GetNavigationController()->Navigate(url);
27   test_observer.Wait();
28 }
29 
30 }  // namespace
31 
NavigateAndWaitForCompletion(const GURL & url,Shell * shell)32 void NavigateAndWaitForCompletion(const GURL& url, Shell* shell) {
33   NavigateAndWaitForEvent(url, shell->tab(),
34                           TestNavigationObserver::NavigationEvent::kCompletion);
35 }
36 
NavigateAndWaitForCompletion(const GURL & url,Tab * tab)37 void NavigateAndWaitForCompletion(const GURL& url, Tab* tab) {
38   NavigateAndWaitForEvent(url, tab,
39                           TestNavigationObserver::NavigationEvent::kCompletion);
40 }
41 
NavigateAndWaitForFailure(const GURL & url,Shell * shell)42 void NavigateAndWaitForFailure(const GURL& url, Shell* shell) {
43   NavigateAndWaitForEvent(url, shell->tab(),
44                           TestNavigationObserver::NavigationEvent::kFailure);
45 }
46 
NavigateAndWaitForStart(const GURL & url,Tab * tab)47 void NavigateAndWaitForStart(const GURL& url, Tab* tab) {
48   NavigateAndWaitForEvent(url, tab,
49                           TestNavigationObserver::NavigationEvent::kStart);
50 }
51 
ExecuteScript(Shell * shell,const std::string & script,bool use_separate_isolate)52 base::Value ExecuteScript(Shell* shell,
53                           const std::string& script,
54                           bool use_separate_isolate) {
55   return ExecuteScript(shell->tab(), script, use_separate_isolate);
56 }
57 
ExecuteScript(Tab * tab,const std::string & script,bool use_separate_isolate)58 base::Value ExecuteScript(Tab* tab,
59                           const std::string& script,
60                           bool use_separate_isolate) {
61   base::Value final_result;
62   base::RunLoop run_loop;
63   tab->ExecuteScript(base::ASCIIToUTF16(script), use_separate_isolate,
64                      base::BindLambdaForTesting(
65                          [&run_loop, &final_result](base::Value result) {
66                            final_result = std::move(result);
67                            run_loop.Quit();
68                          }));
69   run_loop.Run();
70   return final_result;
71 }
72 
ExecuteScriptWithUserGesture(Shell * shell,const std::string & script)73 void ExecuteScriptWithUserGesture(Shell* shell, const std::string& script) {
74   ExecuteScriptWithUserGesture(shell->tab(), script);
75 }
76 
ExecuteScriptWithUserGesture(Tab * tab,const std::string & script)77 void ExecuteScriptWithUserGesture(Tab* tab, const std::string& script) {
78   TabImpl* tab_impl = static_cast<TabImpl*>(tab);
79   tab_impl->ExecuteScriptWithUserGestureForTests(base::ASCIIToUTF16(script));
80 }
81 
GetTitle(Shell * shell)82 const base::string16& GetTitle(Shell* shell) {
83   TabImpl* tab_impl = static_cast<TabImpl*>(shell->tab());
84 
85   return tab_impl->web_contents()->GetTitle();
86 }
87 
InitializeAutofillWithEventForwarding(Shell * shell,const base::RepeatingCallback<void (const autofill::FormData &)> & on_received_form_data)88 void InitializeAutofillWithEventForwarding(
89     Shell* shell,
90     const base::RepeatingCallback<void(const autofill::FormData&)>&
91         on_received_form_data) {
92   TabImpl* tab_impl = static_cast<TabImpl*>(shell->tab());
93 
94   tab_impl->InitializeAutofillForTests(
95       std::make_unique<StubAutofillProvider>(on_received_form_data));
96 }
97 
OneShotNavigationObserver(Shell * shell)98 OneShotNavigationObserver::OneShotNavigationObserver(Shell* shell)
99     : tab_(shell->tab()) {
100   tab_->GetNavigationController()->AddObserver(this);
101 }
102 
~OneShotNavigationObserver()103 OneShotNavigationObserver::~OneShotNavigationObserver() {
104   tab_->GetNavigationController()->RemoveObserver(this);
105 }
106 
WaitForNavigation()107 void OneShotNavigationObserver::WaitForNavigation() {
108   run_loop_.Run();
109 }
110 
NavigationCompleted(Navigation * navigation)111 void OneShotNavigationObserver::NavigationCompleted(Navigation* navigation) {
112   completed_ = true;
113   Finish(navigation);
114 }
115 
NavigationFailed(Navigation * navigation)116 void OneShotNavigationObserver::NavigationFailed(Navigation* navigation) {
117   Finish(navigation);
118 }
119 
Finish(Navigation * navigation)120 void OneShotNavigationObserver::Finish(Navigation* navigation) {
121   is_error_page_ = navigation->IsErrorPage();
122   is_download_ = navigation->IsDownload();
123   is_reload_ = navigation->IsReload();
124   was_stop_called_ = navigation->WasStopCalled();
125   load_error_ = navigation->GetLoadError();
126   http_status_code_ = navigation->GetHttpStatusCode();
127   navigation_state_ = navigation->GetState();
128   run_loop_.Quit();
129 }
130 
131 }  // namespace weblayer
132