MockWebView.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma warning disable CS0067
  17. using System;
  18. using System.Collections.Generic;
  19. using UnityEngine;
  20. #if NET_4_6 || NET_STANDARD_2_0
  21. using System.Threading.Tasks;
  22. #endif
  23. namespace Vuplex.WebView {
  24. /// <summary>
  25. /// Mock IWebView implementation used for running in the Unity editor.
  26. /// </summary>
  27. /// <remarks>
  28. /// MockWebView logs messages to the console to indicate when its methods are
  29. /// called, but it doesn't actually load or render web content.
  30. /// </remarks>
  31. class MockWebView : MonoBehaviour, IWebView {
  32. public event EventHandler CloseRequested;
  33. public event EventHandler<ConsoleMessageEventArgs> ConsoleMessageLogged;
  34. public event EventHandler<FocusedInputFieldChangedEventArgs> FocusedInputFieldChanged;
  35. public event EventHandler<ProgressChangedEventArgs> LoadProgressChanged;
  36. public event EventHandler<EventArgs<string>> MessageEmitted;
  37. public event EventHandler PageLoadFailed;
  38. public event EventHandler<EventArgs<string>> TitleChanged;
  39. public event EventHandler<UrlChangedEventArgs> UrlChanged;
  40. public event EventHandler<EventArgs<Rect>> VideoRectChanged;
  41. public bool IsDisposed { get; private set; }
  42. public bool IsInitialized { get; private set; }
  43. public List<string> PageLoadScripts {
  44. get {
  45. return _pageLoadScripts;
  46. }
  47. }
  48. public WebPluginType PluginType {
  49. get {
  50. return WebPluginType.Mock;
  51. }
  52. }
  53. public float Resolution {
  54. get {
  55. return _numberOfPixelsPerUnityUnit;
  56. }
  57. }
  58. public Vector2 Size { get; private set; }
  59. public Vector2 SizeInPixels {
  60. get {
  61. return new Vector2(Size.x * _numberOfPixelsPerUnityUnit, Size.y * _numberOfPixelsPerUnityUnit);
  62. }
  63. }
  64. public Texture2D Texture { get; private set; }
  65. public string Url { get; private set; }
  66. public Texture2D VideoTexture { get; private set; }
  67. public void Init(Texture2D viewportTexture, float width, float height) {
  68. Init(viewportTexture, width, height, null);
  69. }
  70. public static MockWebView Instantiate() {
  71. return (MockWebView) new GameObject("MockWebView").AddComponent<MockWebView>();
  72. }
  73. public void Init(Texture2D viewportTexture, float width, float height, Texture2D videoTexture) {
  74. Texture = viewportTexture;
  75. VideoTexture = videoTexture;
  76. Size = new Vector2(width, height);
  77. IsInitialized = true;
  78. DontDestroyOnLoad(gameObject);
  79. _log("Init() width: {0}, height: {1}", width.ToString("n4"), height.ToString("n4"));
  80. }
  81. public void Blur() {
  82. _log("Blur()");
  83. }
  84. #if NET_4_6 || NET_STANDARD_2_0
  85. public Task<bool> CanGoBack() {
  86. var task = new TaskCompletionSource<bool>();
  87. CanGoBack(task.SetResult);
  88. return task.Task;
  89. }
  90. public Task<bool> CanGoForward() {
  91. var task = new TaskCompletionSource<bool>();
  92. CanGoForward(task.SetResult);
  93. return task.Task;
  94. }
  95. #endif
  96. public void CanGoBack(Action<bool> callback) {
  97. _log("CanGoBack()");
  98. callback(false);
  99. }
  100. public void CanGoForward(Action<bool> callback) {
  101. _log("CanGoForward()");
  102. callback(false);
  103. }
  104. #if NET_4_6 || NET_STANDARD_2_0
  105. public Task<byte[]> CaptureScreenshot() {
  106. var task = new TaskCompletionSource<byte[]>();
  107. CaptureScreenshot(task.SetResult);
  108. return task.Task;
  109. }
  110. #endif
  111. public void CaptureScreenshot(Action<byte[]> callback) {
  112. _log("CaptureScreenshot()");
  113. callback(new byte[0]);
  114. }
  115. public void Click(Vector2 point) {
  116. _log("Click({0})", point.ToString("n4"));
  117. }
  118. public void Click(Vector2 point, bool preventStealingFocus) {
  119. _log("Click({0}, {1})", point.ToString("n4"), preventStealingFocus);
  120. }
  121. public void Copy() {
  122. _log("Copy()");
  123. }
  124. public void Cut() {
  125. _log("Cut()");
  126. }
  127. public void DisableViewUpdates() {
  128. _log("DisableViewUpdates()");
  129. }
  130. public void Dispose() {
  131. IsDisposed = true;
  132. _log("Dispose()");
  133. if (this != null) {
  134. Destroy(gameObject);
  135. }
  136. }
  137. public void EnableViewUpdates() {
  138. _log("EnableViewUpdates()");
  139. }
  140. #if NET_4_6 || NET_STANDARD_2_0
  141. public Task<string> ExecuteJavaScript(string javaScript) {
  142. var task = new TaskCompletionSource<string>();
  143. ExecuteJavaScript(javaScript, task.SetResult);
  144. return task.Task;
  145. }
  146. #else
  147. public void ExecuteJavaScript(string javaScript) {
  148. _log("ExecuteJavaScript(\"{0}...\")", javaScript.Substring(0, 25));
  149. }
  150. #endif
  151. public void ExecuteJavaScript(string javaScript, Action<string> callback) {
  152. _log("ExecuteJavaScript(\"{0}...\")", javaScript.Substring(0, 25));
  153. callback("");
  154. }
  155. public void Focus() {
  156. _log("Focus()");
  157. }
  158. #if NET_4_6 || NET_STANDARD_2_0
  159. public Task<byte[]> GetRawTextureData() {
  160. var task = new TaskCompletionSource<byte[]>();
  161. GetRawTextureData(task.SetResult);
  162. return task.Task;
  163. }
  164. #endif
  165. public void GetRawTextureData(Action<byte[]> callback) {
  166. _log("GetRawTextureData()");
  167. callback(new byte[0]);
  168. }
  169. public void GoBack() {
  170. _log("GoBack()");
  171. }
  172. public void GoForward() {
  173. _log("GoForward()");
  174. }
  175. public void HandleKeyboardInput(string input) {
  176. _log("HandleKeyboardInput(\"{0}\")", input);
  177. }
  178. public virtual void LoadHtml(string html) {
  179. Url = html.Substring(0, 25);
  180. _log("LoadHtml(\"{0}...\")", Url);
  181. if (UrlChanged != null) {
  182. UrlChanged(this, new UrlChangedEventArgs(Url, "Title", UrlActionType.Load));
  183. }
  184. if (LoadProgressChanged != null) {
  185. LoadProgressChanged(this, new ProgressChangedEventArgs(ProgressChangeType.Finished, 1.0f));
  186. }
  187. }
  188. public virtual void LoadUrl(string url) {
  189. LoadUrl(url, null);
  190. }
  191. public virtual void LoadUrl(string url, Dictionary<string, string> additionalHttpHeaders) {
  192. Url = url;
  193. _log("LoadUrl(\"{0}\")", url);
  194. if (UrlChanged != null) {
  195. UrlChanged(this, new UrlChangedEventArgs(url, "Title", UrlActionType.Load));
  196. }
  197. if (LoadProgressChanged != null) {
  198. LoadProgressChanged(this, new ProgressChangedEventArgs(ProgressChangeType.Finished, 1.0f));
  199. }
  200. }
  201. public void Paste() {
  202. _log("Paste()");
  203. }
  204. public void PostMessage(string data) {
  205. _log("PostMessage(\"{0}\")", data);
  206. }
  207. public void Reload() {
  208. _log("Reload()");
  209. }
  210. public void Resize(float width, float height) {
  211. Size = new Vector2(width, height);
  212. _log("Resize({0}, {1})", width.ToString("n4"), height.ToString("n4"));
  213. }
  214. public void Scroll(Vector2 delta) {
  215. _log("Scroll({0})", delta.ToString("n4"));
  216. }
  217. public void Scroll(Vector2 delta, Vector2 point) {
  218. _log("Scroll({0}, {1})", delta.ToString("n4"), point.ToString("n4"));
  219. }
  220. public void SelectAll() {
  221. _log("SelectAll()");
  222. }
  223. public void SetResolution(float pixelsPerUnityUnit) {
  224. _numberOfPixelsPerUnityUnit = pixelsPerUnityUnit;
  225. _log("SetResolution({0})", pixelsPerUnityUnit);
  226. }
  227. public void ZoomIn() {
  228. _log("ZoomIn()");
  229. }
  230. public void ZoomOut() {
  231. _log("ZoomOut()");
  232. }
  233. List<string> _pageLoadScripts = new List<string>();
  234. float _numberOfPixelsPerUnityUnit = Config.NumberOfPixelsPerUnityUnit;
  235. void _log(string message, params object[] args) {
  236. WebViewLogger.LogFormat("[MockWebView] " + message, args);
  237. }
  238. }
  239. }