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 * TODO(wang16): This test should be moved into internal test.
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.
33 public class WebNotificationTest extends XWalkViewTestBase {
34 class TestXWalkNotificationService extends XWalkNotificationServiceImpl {
35 private Notification mNotification;
37 public TestXWalkNotificationService(Context context, XWalkView view) {
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;
48 protected void mockClick() {
49 if (mNotification != null && mNotification.contentIntent != null) {
51 mNotification.contentIntent.send();
52 } catch (android.app.PendingIntent.CanceledException e) {}
56 protected void mockClose() {
57 if (mNotification != null && mNotification.deleteIntent != null) {
59 mNotification.deleteIntent.send();
60 } catch (android.app.PendingIntent.CanceledException e) {}
65 private TestXWalkNotificationService mNotificationService;
68 public void setUp() throws Exception {
71 getInstrumentation().runOnMainSync(new Runnable() {
74 mNotificationService = new TestXWalkNotificationService(
75 getXWalkView().getActivity(), getXWalkView());
76 getXWalkView().setNotificationService(mNotificationService);
77 XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
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.
94 } catch (InterruptedException e) {}
95 getInstrumentation().waitForIdleSync();
96 assertEquals("notification shown", getTitleOnUiThread());
97 executeJavaScriptAndWaitForResult("dismiss();");
100 } catch (InterruptedException e) {}
101 getInstrumentation().waitForIdleSync();
102 assertEquals("notification closed", getTitleOnUiThread());
106 @Feature({"WebNotification"})
107 public void testWebNotificationShowAndCloseByUser() throws Throwable {
108 loadAssetFile("notification.html");
109 getInstrumentation().waitForIdleSync();
110 executeJavaScriptAndWaitForResult("notify();");
113 } catch (InterruptedException e) {}
114 getInstrumentation().waitForIdleSync();
115 assertEquals("notification shown", getTitleOnUiThread());
116 mNotificationService.mockClose();
119 } catch (InterruptedException e) {}
120 getInstrumentation().waitForIdleSync();
121 assertEquals("notification closed", getTitleOnUiThread());