Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / runtime / src / org / xwalk / runtime / XWalkRuntimeTestHelper.java
1 // Copyright (c) 2013-2014 Intel Corporation. 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 package org.xwalk.runtime;
6
7 import android.content.Context;
8 import android.graphics.Bitmap;
9 import android.net.http.SslError;
10 import android.webkit.ValueCallback;
11
12 import java.lang.reflect.Method;
13
14 import org.xwalk.core.XWalkResourceClient;
15 import org.xwalk.core.XWalkUIClient;
16 import org.xwalk.core.XWalkView;
17
18 class XWalkRuntimeTestHelper {
19
20     class TestXWalkResourceClient extends XWalkResourceClient {
21         TestXWalkResourceClient(Context context, XWalkView view) {
22             super(view);
23         }
24
25         @Override
26         public void onReceivedLoadError(XWalkView view, int errorCode,
27                 String description, String failingUrl) {
28             super.onReceivedLoadError(view, errorCode, description, failingUrl);
29             if (mCallbackForTest != null) {
30                 try {
31                     Class<?> objectClass = mCallbackForTest.getClass();
32                     Method onReceivedLoadError = objectClass.getMethod(
33                             "onReceivedLoadError", int.class, String.class, String.class);
34                     onReceivedLoadError.invoke(mCallbackForTest, errorCode, description,
35                             failingUrl);
36                 } catch (Exception e) {
37                     e.printStackTrace();
38                 }
39             }
40         }
41     }
42
43     class TestXWalkUIClient extends XWalkUIClient {
44         TestXWalkUIClient(Context context, XWalkView view) {
45             super(view);
46         }
47
48         @Override
49         public void onPageLoadStarted(XWalkView view, String url) {
50             super.onPageLoadStarted(view, url);
51             if (mCallbackForTest != null) {
52                 try {
53                     Class<?> objectClass = mCallbackForTest.getClass();
54                     Method onPageStarted = objectClass.getMethod("onPageStarted", String.class);
55                     onPageStarted.invoke(mCallbackForTest, url);
56                 } catch (Exception e) {
57                     e.printStackTrace();
58                 }
59             }
60         }
61
62         @Override
63         public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
64             super.onPageLoadStopped(view, url, status);
65             if (mCallbackForTest != null) {
66                 try {
67                     Class<?> objectClass = mCallbackForTest.getClass();
68                     Method onPageFinished = objectClass.getMethod("onPageFinished", String.class);
69                     onPageFinished.invoke(mCallbackForTest, url);
70                 } catch (Exception e) {
71                     e.printStackTrace();
72                 }
73             }
74         }
75
76         @Override
77         public void onReceivedTitle(XWalkView view, String title) {
78             super.onReceivedTitle(view, title);
79             if (mCallbackForTest != null) {
80                 try {
81                     Class<?> objectClass = mCallbackForTest.getClass();
82                     Method onReceivedTitle = objectClass.getMethod("onReceivedTitle", String.class);
83                     onReceivedTitle.invoke(mCallbackForTest, title);
84                 } catch (Exception e) {
85                     e.printStackTrace();
86                 }
87             }
88         }
89     }
90
91     private Object mCallbackForTest;
92     private TestXWalkUIClient mUIClient;
93     private TestXWalkResourceClient mResourceClient;
94
95     XWalkRuntimeTestHelper(Context context, XWalkView view) {
96         mUIClient = new TestXWalkUIClient(context, view);
97         mResourceClient = new TestXWalkResourceClient(context, view);
98     }
99
100     void setCallbackForTest(Object callback) {
101         mCallbackForTest = callback;
102     }
103
104     XWalkUIClient getUIClient() {
105         return mUIClient;
106     }
107
108     XWalkResourceClient getResourceClient() {
109         return mResourceClient;
110     }
111 }