Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / WebNotificationTest.java
1 // Copyright (c) 2013 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.core.xwview.test;
6
7 import java.lang.Thread;
8 import java.lang.InterruptedException;
9
10 import android.app.Notification;
11 import android.app.PendingIntent;
12 import android.content.Context;
13 import android.content.Intent;
14 import android.graphics.Bitmap;
15 import android.test.suitebuilder.annotation.SmallTest;
16 import android.util.Log;
17
18 import org.chromium.base.test.util.Feature;
19 import org.xwalk.core.XWalkClient;
20 import org.xwalk.core.XWalkView;
21 import org.xwalk.core.XWalkWebChromeClient;
22 import org.xwalk.core.XWalkDefaultNotificationService;
23
24 /**
25  * Test suite for web notification API.
26  * This test will only cover notification.show() and notification.close().
27  * The event handler will be covered in runtime level test. Because that
28  * will need activity to participate. 
29  */
30 public class WebNotificationTest extends XWalkViewTestBase {
31     class TestXWalkNotificationService extends XWalkDefaultNotificationService {
32         private Notification mNotification;
33
34         public TestXWalkNotificationService(Context context, XWalkView view) {
35             super(context, view);
36         }
37
38         @Override
39         public void doShowNotification(int id, Notification notification) {
40             // For testing purpose, instead of sending out the notification,
41             // just keep the notification.
42             mNotification = notification;
43         }
44
45         protected void mockClick() {
46             if (mNotification != null && mNotification.contentIntent != null) {
47                 try {
48                     mNotification.contentIntent.send();
49                 } catch (android.app.PendingIntent.CanceledException e) {}
50             }
51         }
52
53         protected void mockClose() {
54             if (mNotification != null && mNotification.deleteIntent != null) {
55                 try {
56                     mNotification.deleteIntent.send();
57                 } catch (android.app.PendingIntent.CanceledException e) {}
58             }
59         }
60     }
61
62     private TestXWalkNotificationService mNotificationService;
63
64     @Override
65     public void setUp() throws Exception {
66         super.setUp();
67         class TestXWalkClient extends XWalkClient {
68             @Override
69             public void onPageStarted(XWalkView view, String url, Bitmap favicon) {
70                 mTestContentsClient.onPageStarted(url);
71             }
72
73             @Override
74             public void onPageFinished(XWalkView view, String url) {
75                 mTestContentsClient.didFinishLoad(url);
76             }
77         }
78         getInstrumentation().runOnMainSync(new Runnable() {
79             @Override
80             public void run() {
81                 getXWalkView().setXWalkClient(new TestXWalkClient());
82                 mNotificationService = new TestXWalkNotificationService(
83                         getXWalkView().getActivity(), getXWalkView());
84                 getXWalkView().setNotificationService(mNotificationService);
85                 getXWalkView().enableRemoteDebugging();
86             }
87         });
88     }
89
90     @SmallTest
91     @Feature({"WebNotification"})
92     public void testWebNotificationShowAndCloseByJs() throws Throwable {
93         loadAssetFile("notification.html");
94         getInstrumentation().waitForIdleSync();
95         executeJavaScriptAndWaitForResult("notify();");
96         // Android Notification is sent in another thread,
97         // Sleep five seconds to let the notification sending/closing
98         // happen first, and then wait for the idle sync which means
99         // javascript callback is executed.
100         try {
101             Thread.sleep(1000);
102         } catch (InterruptedException e) {}
103         getInstrumentation().waitForIdleSync();
104         assertEquals("notification shown", getTitleOnUiThread());
105         executeJavaScriptAndWaitForResult("dismiss();");
106         try {
107             Thread.sleep(1000);
108         } catch (InterruptedException e) {}
109         getInstrumentation().waitForIdleSync();
110         assertEquals("notification closed", getTitleOnUiThread());
111     }
112
113     @SmallTest
114     @Feature({"WebNotification"})
115     public void testWebNotificationShowAndCloseByUser() throws Throwable {
116         loadAssetFile("notification.html");
117         getInstrumentation().waitForIdleSync();
118         executeJavaScriptAndWaitForResult("notify();");
119         try {
120             Thread.sleep(1000);
121         } catch (InterruptedException e) {}
122         getInstrumentation().waitForIdleSync();
123         assertEquals("notification shown", getTitleOnUiThread());
124         mNotificationService.mockClose();
125         try {
126             Thread.sleep(1000);
127         } catch (InterruptedException e) {}
128         getInstrumentation().waitForIdleSync();
129         assertEquals("notification closed", getTitleOnUiThread());
130     }
131 }