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 #ifndef WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
6 #define WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/optional.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string16.h"
12 #include "base/values.h"
13 #include "weblayer/public/navigation.h"
14 #include "weblayer/public/navigation_observer.h"
15 
16 class GURL;
17 
18 namespace autofill {
19 struct FormData;
20 }
21 
22 namespace weblayer {
23 class Shell;
24 class Tab;
25 
26 // Navigates |shell| to |url| and wait for completed navigation.
27 void NavigateAndWaitForCompletion(const GURL& url, Shell* shell);
28 
29 void NavigateAndWaitForCompletion(const GURL& url, Tab* tab);
30 
31 // Navigates |shell| to |url| and wait for failed navigation.
32 void NavigateAndWaitForFailure(const GURL& url, Shell* shell);
33 
34 // Initiates navigation to |url| in |tab| and waits for it to start.
35 void NavigateAndWaitForStart(const GURL& url, Tab* tab);
36 
37 // Executes |script| in |shell| and returns the result.
38 base::Value ExecuteScript(Shell* shell,
39                           const std::string& script,
40                           bool use_separate_isolate);
41 base::Value ExecuteScript(Tab* tab,
42                           const std::string& script,
43                           bool use_separate_isolate);
44 
45 // Executes |script| in |shell| with a user gesture. Useful for tests of
46 // functionality that gates action on a user gesture having occurred.
47 // Differs from ExecuteScript() as follows:
48 // - Does not notify the caller of the result as the underlying implementation
49 //   does not. Thus, unlike the above, the caller of this function will need to
50 //   explicitly listen *after* making this call for any expected event to
51 //   occur.
52 // - Does not allow running in a separate isolate as the  machinery for
53 //   setting a user gesture works only in the main isolate.
54 void ExecuteScriptWithUserGesture(Shell* shell, const std::string& script);
55 void ExecuteScriptWithUserGesture(Tab* tab, const std::string& script);
56 
57 /// Gets the title of the current webpage in |shell|.
58 const base::string16& GetTitle(Shell* shell);
59 
60 // Sets up the autofill system to be one that simply forwards detected forms to
61 // the passed-in callback.
62 void InitializeAutofillWithEventForwarding(
63     Shell* shell,
64     const base::RepeatingCallback<void(const autofill::FormData&)>&
65         on_received_form_data);
66 
67 class OneShotNavigationObserver : public NavigationObserver {
68  public:
69   explicit OneShotNavigationObserver(Shell* shell);
70 
71   ~OneShotNavigationObserver() override;
72 
73   void WaitForNavigation();
74 
completed()75   bool completed() { return completed_; }
is_error_page()76   bool is_error_page() { return is_error_page_; }
is_download()77   bool is_download() { return is_download_; }
is_reload()78   bool is_reload() { return is_reload_; }
was_stop_called()79   bool was_stop_called() { return was_stop_called_; }
load_error()80   Navigation::LoadError load_error() { return load_error_; }
http_status_code()81   int http_status_code() { return http_status_code_; }
navigation_state()82   NavigationState navigation_state() { return navigation_state_; }
83 
84  private:
85   // NavigationObserver implementation:
86   void NavigationCompleted(Navigation* navigation) override;
87   void NavigationFailed(Navigation* navigation) override;
88 
89   void Finish(Navigation* navigation);
90 
91   base::RunLoop run_loop_;
92   Tab* tab_;
93   bool completed_ = false;
94   bool is_error_page_ = false;
95   bool is_download_ = false;
96   bool is_reload_ = false;
97   bool was_stop_called_ = false;
98   Navigation::LoadError load_error_ = Navigation::kNoError;
99   int http_status_code_ = 0;
100   NavigationState navigation_state_ = NavigationState::kWaitingResponse;
101 };
102 
103 }  // namespace weblayer
104 
105 #endif  // WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
106