1 // Copyright 2014 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 IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_H_ 6 #define IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "base/memory/weak_ptr.h" 14 #include "base/observer_list.h" 15 #include "base/strings/string16.h" 16 #include "ios/web/public/deprecated/url_verification_constants.h" 17 #import "ios/web/public/navigation/navigation_manager.h" 18 #import "ios/web/public/navigation/web_state_policy_decider.h" 19 #import "ios/web/public/web_state.h" 20 #include "ios/web/public/web_state_observer.h" 21 #include "url/gurl.h" 22 23 @class NSURLRequest; 24 @class NSURLResponse; 25 26 namespace web { 27 28 // Minimal implementation of WebState, to be used in tests. 29 class TestWebState : public WebState { 30 public: 31 TestWebState(); 32 ~TestWebState() override; 33 34 // WebState implementation. 35 Getter CreateDefaultGetter() override; 36 OnceGetter CreateDefaultOnceGetter() override; 37 WebStateDelegate* GetDelegate() override; 38 void SetDelegate(WebStateDelegate* delegate) override; 39 bool IsWebUsageEnabled() const override; 40 void SetWebUsageEnabled(bool enabled) override; 41 UIView* GetView() override; 42 void DidCoverWebContent() override; 43 void DidRevealWebContent() override; 44 void WasShown() override; 45 void WasHidden() override; 46 void SetKeepRenderProcessAlive(bool keep_alive) override; 47 BrowserState* GetBrowserState() const override; OpenURL(const OpenURLParams & params)48 void OpenURL(const OpenURLParams& params) override {} Stop()49 void Stop() override {} 50 const NavigationManager* GetNavigationManager() const override; 51 NavigationManager* GetNavigationManager() override; 52 const WebFramesManager* GetWebFramesManager() const override; 53 WebFramesManager* GetWebFramesManager() override; 54 const SessionCertificatePolicyCache* GetSessionCertificatePolicyCache() 55 const override; 56 SessionCertificatePolicyCache* GetSessionCertificatePolicyCache() override; 57 CRWSessionStorage* BuildSessionStorage() override; 58 CRWJSInjectionReceiver* GetJSInjectionReceiver() const override; 59 void LoadData(NSData* data, NSString* mime_type, const GURL& url) override; 60 void ExecuteJavaScript(const base::string16& javascript) override; 61 void ExecuteJavaScript(const base::string16& javascript, 62 JavaScriptResultCallback callback) override; 63 void ExecuteUserJavaScript(NSString* javaScript) override; 64 const std::string& GetContentsMimeType() const override; 65 bool ContentIsHTML() const override; 66 const base::string16& GetTitle() const override; 67 bool IsLoading() const override; 68 double GetLoadingProgress() const override; 69 bool IsVisible() const override; 70 bool IsCrashed() const override; 71 bool IsEvicted() const override; 72 bool IsBeingDestroyed() const override; 73 const GURL& GetVisibleURL() const override; 74 const GURL& GetLastCommittedURL() const override; 75 GURL GetCurrentURL(URLVerificationTrustLevel* trust_level) const override; 76 std::unique_ptr<ScriptCommandSubscription> AddScriptCommandCallback( 77 const ScriptCommandCallback& callback, 78 const std::string& command_prefix) override; 79 CRWWebViewProxyType GetWebViewProxy() const override; 80 bool IsShowingWebInterstitial() const override; 81 WebInterstitial* GetWebInterstitial() const override; 82 83 void AddObserver(WebStateObserver* observer) override; 84 85 void RemoveObserver(WebStateObserver* observer) override; 86 87 void CloseWebState() override; 88 89 void AddPolicyDecider(WebStatePolicyDecider* decider) override; 90 void RemovePolicyDecider(WebStatePolicyDecider* decider) override; 91 void DidChangeVisibleSecurityState() override; 92 bool HasOpener() const override; 93 void SetHasOpener(bool has_opener) override; 94 bool CanTakeSnapshot() const override; 95 void TakeSnapshot(const gfx::RectF& rect, SnapshotCallback callback) override; 96 void CreateFullPagePdf(base::OnceCallback<void(NSData*)> callback) override; 97 98 // Setters for test data. 99 void SetBrowserState(BrowserState* browser_state); 100 void SetJSInjectionReceiver(CRWJSInjectionReceiver* injection_receiver); 101 void SetTitle(const base::string16& title); 102 void SetContentIsHTML(bool content_is_html); 103 void SetContentsMimeType(const std::string& mime_type); 104 void SetLoading(bool is_loading); 105 void SetCurrentURL(const GURL& url); 106 void SetVisibleURL(const GURL& url); 107 void SetTrustLevel(URLVerificationTrustLevel trust_level); 108 void SetNavigationManager( 109 std::unique_ptr<NavigationManager> navigation_manager); 110 void SetWebFramesManager( 111 std::unique_ptr<WebFramesManager> web_frames_manager); 112 void SetView(UIView* view); 113 void SetIsCrashed(bool value); 114 void SetIsEvicted(bool value); 115 void SetWebViewProxy(CRWWebViewProxyType web_view_proxy); 116 void ClearLastExecutedJavascript(); 117 void SetCanTakeSnapshot(bool can_take_snapshot); 118 119 // Getters for test data. 120 // Uses |policy_deciders| to return whether the navigation corresponding to 121 // |request| should be allowed. Defaults to PolicyDecision::Allow(). 122 WebStatePolicyDecider::PolicyDecision ShouldAllowRequest( 123 NSURLRequest* request, 124 const WebStatePolicyDecider::RequestInfo& request_info); 125 // Uses |policy_deciders| to determine whether the navigation corresponding to 126 // |response| should be allowed. Calls |callback| with the decision. Defaults 127 // to PolicyDecision::Allow(). 128 void ShouldAllowResponse( 129 NSURLResponse* response, 130 bool for_main_frame, 131 base::OnceCallback<void(WebStatePolicyDecider::PolicyDecision)> callback); 132 base::string16 GetLastExecutedJavascript() const; 133 NSData* GetLastLoadedData() const; 134 bool IsClosed() const; 135 136 // Notifier for tests. 137 void OnPageLoaded(PageLoadCompletionStatus load_completion_status); 138 void OnNavigationStarted(NavigationContext* navigation_context); 139 void OnNavigationRedirected(NavigationContext* context); 140 void OnNavigationFinished(NavigationContext* navigation_context); 141 void OnRenderProcessGone(); 142 void OnBackForwardStateChanged(); 143 void OnVisibleSecurityStateChanged(); 144 void OnWebFrameDidBecomeAvailable(WebFrame* frame); 145 void OnWebFrameWillBecomeUnavailable(WebFrame* frame); 146 147 private: 148 BrowserState* browser_state_; 149 CRWJSInjectionReceiver* injection_receiver_; 150 bool web_usage_enabled_; 151 bool is_loading_; 152 bool is_visible_; 153 bool is_crashed_; 154 bool is_evicted_; 155 bool has_opener_; 156 bool can_take_snapshot_; 157 bool is_closed_; 158 GURL url_; 159 base::string16 title_; 160 base::string16 last_executed_javascript_; 161 URLVerificationTrustLevel trust_level_; 162 bool content_is_html_; 163 std::string mime_type_; 164 std::unique_ptr<NavigationManager> navigation_manager_; 165 std::unique_ptr<WebFramesManager> web_frames_manager_; 166 UIView* view_; 167 CRWWebViewProxyType web_view_proxy_; 168 NSData* last_loaded_data_; 169 base::RepeatingCallbackList<ScriptCommandCallbackSignature> callback_list_; 170 171 // A list of observers notified when page state changes. Weak references. 172 base::ObserverList<WebStateObserver, true>::Unchecked observers_; 173 // All the WebStatePolicyDeciders asked for navigation decision. Weak 174 // references. 175 base::ObserverList<WebStatePolicyDecider, true>::Unchecked policy_deciders_; 176 177 base::WeakPtrFactory<TestWebState> weak_factory_{this}; 178 }; 179 180 } // namespace web 181 182 #endif // IOS_WEB_PUBLIC_TEST_FAKES_TEST_WEB_STATE_H_ 183