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