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