Upstream version 8.37.183.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core_internal / javatests / src / org / xwalk / core / internal / 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.internal.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.internal.XWalkPreferencesInternal;
20 import org.xwalk.core.internal.XWalkViewInternal;
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 XWalkViewInternalTestBase {
32     class TestXWalkNotificationService extends XWalkNotificationServiceImpl {
33         private Notification mNotification;
34
35         public TestXWalkNotificationService(Context context, XWalkViewInternal 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         getInstrumentation().runOnMainSync(new Runnable() {
70             @Override
71             public void run() {
72                 mNotificationService = new TestXWalkNotificationService(
73                         getXWalkView().getActivity(), getXWalkView());
74                 getXWalkView().setNotificationService(mNotificationService);
75                 XWalkPreferencesInternal.setValue(XWalkPreferencesInternal.REMOTE_DEBUGGING, true);
76             }
77         });
78     }
79
80     @SmallTest
81     @Feature({"WebNotification"})
82     public void testWebNotificationShowAndCloseByJs() throws Throwable {
83         loadAssetFile("notification.html");
84         getInstrumentation().waitForIdleSync();
85         executeJavaScriptAndWaitForResult("notify();");
86         // Android Notification is sent in another thread,
87         // Sleep five seconds to let the notification sending/closing
88         // happen first, and then wait for the idle sync which means
89         // javascript callback is executed.
90         try {
91             Thread.sleep(1000);
92         } catch (InterruptedException e) {}
93         getInstrumentation().waitForIdleSync();
94         assertEquals("notification shown", getTitleOnUiThread());
95         executeJavaScriptAndWaitForResult("dismiss();");
96         try {
97             Thread.sleep(1000);
98         } catch (InterruptedException e) {}
99         getInstrumentation().waitForIdleSync();
100         assertEquals("notification closed", getTitleOnUiThread());
101     }
102
103     @SmallTest
104     @Feature({"WebNotification"})
105     public void testWebNotificationShowAndCloseByUser() throws Throwable {
106         loadAssetFile("notification.html");
107         getInstrumentation().waitForIdleSync();
108         executeJavaScriptAndWaitForResult("notify();");
109         try {
110             Thread.sleep(1000);
111         } catch (InterruptedException e) {}
112         getInstrumentation().waitForIdleSync();
113         assertEquals("notification shown", getTitleOnUiThread());
114         mNotificationService.mockClose();
115         try {
116             Thread.sleep(1000);
117         } catch (InterruptedException e) {}
118         getInstrumentation().waitForIdleSync();
119         assertEquals("notification closed", getTitleOnUiThread());
120     }
121 }