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.
5 package org.xwalk.core.xwview.test;
7 import java.lang.Thread;
8 import java.lang.InterruptedException;
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;
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;
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.
31 public class WebNotificationTest extends XWalkViewTestBase {
32 class TestXWalkNotificationService extends XWalkNotificationServiceImpl {
33 private Notification mNotification;
35 public TestXWalkNotificationService(Context context, XWalkView view) {
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;
46 protected void mockClick() {
47 if (mNotification != null && mNotification.contentIntent != null) {
49 mNotification.contentIntent.send();
50 } catch (android.app.PendingIntent.CanceledException e) {}
54 protected void mockClose() {
55 if (mNotification != null && mNotification.deleteIntent != null) {
57 mNotification.deleteIntent.send();
58 } catch (android.app.PendingIntent.CanceledException e) {}
63 private TestXWalkNotificationService mNotificationService;
66 public void setUp() throws Exception {
69 setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
70 getInstrumentation().runOnMainSync(new Runnable() {
73 mNotificationService = new TestXWalkNotificationService(
74 getXWalkView().getActivity(), getXWalkView());
75 getXWalkView().setNotificationService(mNotificationService);
76 XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
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.
93 } catch (InterruptedException e) {}
94 getInstrumentation().waitForIdleSync();
95 assertEquals("notification shown", getTitleOnUiThread());
96 executeJavaScriptAndWaitForResult("dismiss();");
99 } catch (InterruptedException e) {}
100 getInstrumentation().waitForIdleSync();
101 assertEquals("notification closed", getTitleOnUiThread());
105 @Feature({"WebNotification"})
106 public void testWebNotificationShowAndCloseByUser() throws Throwable {
107 loadAssetFile("notification.html");
108 getInstrumentation().waitForIdleSync();
109 executeJavaScriptAndWaitForResult("notify();");
112 } catch (InterruptedException e) {}
113 getInstrumentation().waitForIdleSync();
114 assertEquals("notification shown", getTitleOnUiThread());
115 mNotificationService.mockClose();
118 } catch (InterruptedException e) {}
119 getInstrumentation().waitForIdleSync();
120 assertEquals("notification closed", getTitleOnUiThread());