1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 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 17 package android.webgl; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.cts.util.NullWebViewUtils; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.webgl.cts.R; 26 import android.webkit.WebView; 27 import android.webkit.JavascriptInterface; 28 import android.webkit.WebViewClient; 29 import android.widget.Toast; 30 import java.lang.Override; 31 import java.io.InputStream; 32 import java.util.concurrent.Semaphore; 33 import java.util.concurrent.TimeUnit; 34 35 /** 36 * A simple activity for testing WebGL Conformance with WebView. 37 */ 38 public class WebGLActivity extends Activity { 39 40 Semaphore mFinished = new Semaphore(0, false); 41 Semaphore mDestroyed = new Semaphore(0, false); 42 String mWebGlHarnessUrl; 43 WebView mWebView; 44 45 // The following members are synchronized. 46 String mWebGLTestUrl; 47 boolean mPassed = true; 48 StringBuilder mMessage = new StringBuilder("\n"); 49 50 @Override onCreate(Bundle icicle)51 public void onCreate(Bundle icicle) { 52 super.onCreate(icicle); 53 54 mWebGlHarnessUrl = "file://" + getCacheDir() + "/harness.html"; 55 try { 56 mWebView = new WebView(this); 57 } catch (Exception e) { 58 NullWebViewUtils.determineIfWebViewAvailable(this, e); 59 } 60 61 if (mWebView == null) { 62 return; 63 } 64 65 mWebView.getSettings().setJavaScriptEnabled(true); 66 mWebView.getSettings().setAllowFileAccessFromFileURLs(true); 67 mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false); 68 mWebView.setWebViewClient(new WebViewClient() { 69 @Override 70 public boolean shouldOverrideUrlLoading(WebView webView, String url) { 71 return false; 72 } 73 }); 74 75 mWebView.addJavascriptInterface(new Object() { 76 @JavascriptInterface 77 public String getUrlToTest() { 78 synchronized(WebGLActivity.this) { 79 return mWebGLTestUrl; 80 } 81 } 82 83 @JavascriptInterface 84 public void reportResults(String type, boolean success, String message) { 85 synchronized(WebGLActivity.this) { 86 mMessage.append((success ? "PASS " : "FAIL ") + message + "\n"); 87 mPassed &= success; 88 } 89 } 90 91 @JavascriptInterface 92 public void notifyFinished() { 93 mFinished.release(); 94 } 95 96 @JavascriptInterface 97 public void alert(String string) { 98 Log.i(mWebGLTestUrl, string); 99 } 100 }, "WebGLCallback"); 101 setContentView(mWebView); 102 } 103 navigateToTest(String url)104 public void navigateToTest(String url) throws Exception { 105 if (!NullWebViewUtils.isWebViewAvailable()) { 106 return; 107 } 108 109 synchronized(WebGLActivity.this) { 110 mWebGLTestUrl = url; 111 } 112 113 // Load harness.html, which will load mWebGLTestUrl in an <iframe>. 114 runOnUiThread(new Runnable() { 115 public void run() { 116 mWebView.loadUrl(mWebGlHarnessUrl); 117 } 118 }); 119 120 // Wait on test completion. 121 boolean finished = mFinished.tryAcquire(60, TimeUnit.SECONDS); 122 String message; 123 synchronized(WebGLActivity.this) { 124 message = mMessage.toString(); 125 } 126 127 // Destroy the webview and wait for it. 128 runOnUiThread(new Runnable() { 129 public void run() { 130 mWebView.destroy(); 131 finish(); 132 mDestroyed.release(); 133 } 134 }); 135 mDestroyed.acquire(); 136 137 if (!finished) 138 throw new Exception("\n" + url + "\n Test timed-out after 60 seconds: " + message); 139 if(!mPassed) 140 throw new Exception("\n" + url + "\n Test failed: " + message); 141 } 142 } 143