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.internal.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.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;
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 XWalkViewInternalTestBase {
32 class TestXWalkNotificationService extends XWalkNotificationServiceImpl {
33 private Notification mNotification;
35 public TestXWalkNotificationService(Context context, XWalkViewInternal 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 getInstrumentation().runOnMainSync(new Runnable() {
72 mNotificationService = new TestXWalkNotificationService(
73 getXWalkView().getActivity(), getXWalkView());
74 getXWalkView().setNotificationService(mNotificationService);
75 XWalkPreferencesInternal.setValue(XWalkPreferencesInternal.REMOTE_DEBUGGING, true);
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.
92 } catch (InterruptedException e) {}
93 getInstrumentation().waitForIdleSync();
94 assertEquals("notification shown", getTitleOnUiThread());
95 executeJavaScriptAndWaitForResult("dismiss();");
98 } catch (InterruptedException e) {}
99 getInstrumentation().waitForIdleSync();
100 assertEquals("notification closed", getTitleOnUiThread());
104 @Feature({"WebNotification"})
105 public void testWebNotificationShowAndCloseByUser() throws Throwable {
106 loadAssetFile("notification.html");
107 getInstrumentation().waitForIdleSync();
108 executeJavaScriptAndWaitForResult("notify();");
111 } catch (InterruptedException e) {}
112 getInstrumentation().waitForIdleSync();
113 assertEquals("notification shown", getTitleOnUiThread());
114 mNotificationService.mockClose();
117 } catch (InterruptedException e) {}
118 getInstrumentation().waitForIdleSync();
119 assertEquals("notification closed", getTitleOnUiThread());