1 // Copyright (c) 2013 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.XWalkClient;
20 import org.xwalk.core.XWalkView;
21 import org.xwalk.core.XWalkWebChromeClient;
22 import org.xwalk.core.XWalkDefaultNotificationService;
25 * Test suite for web notification API.
26 * This test will only cover notification.show() and notification.close().
27 * The event handler will be covered in runtime level test. Because that
28 * will need activity to participate.
30 public class WebNotificationTest extends XWalkViewTestBase {
31 class TestXWalkNotificationService extends XWalkDefaultNotificationService {
32 private Notification mNotification;
34 public TestXWalkNotificationService(Context context, XWalkView view) {
39 public void doShowNotification(int id, Notification notification) {
40 // For testing purpose, instead of sending out the notification,
41 // just keep the notification.
42 mNotification = notification;
45 protected void mockClick() {
46 if (mNotification != null && mNotification.contentIntent != null) {
48 mNotification.contentIntent.send();
49 } catch (android.app.PendingIntent.CanceledException e) {}
53 protected void mockClose() {
54 if (mNotification != null && mNotification.deleteIntent != null) {
56 mNotification.deleteIntent.send();
57 } catch (android.app.PendingIntent.CanceledException e) {}
62 private TestXWalkNotificationService mNotificationService;
65 public void setUp() throws Exception {
67 class TestXWalkClient extends XWalkClient {
69 public void onPageStarted(XWalkView view, String url, Bitmap favicon) {
70 mTestContentsClient.onPageStarted(url);
74 public void onPageFinished(XWalkView view, String url) {
75 mTestContentsClient.didFinishLoad(url);
78 getInstrumentation().runOnMainSync(new Runnable() {
81 getXWalkView().setXWalkClient(new TestXWalkClient());
82 mNotificationService = new TestXWalkNotificationService(
83 getXWalkView().getActivity(), getXWalkView());
84 getXWalkView().setNotificationService(mNotificationService);
85 getXWalkView().enableRemoteDebugging();
91 @Feature({"WebNotification"})
92 public void testWebNotificationShowAndCloseByJs() throws Throwable {
93 loadAssetFile("notification.html");
94 getInstrumentation().waitForIdleSync();
95 executeJavaScriptAndWaitForResult("notify();");
96 // Android Notification is sent in another thread,
97 // Sleep five seconds to let the notification sending/closing
98 // happen first, and then wait for the idle sync which means
99 // javascript callback is executed.
102 } catch (InterruptedException e) {}
103 getInstrumentation().waitForIdleSync();
104 assertEquals("notification shown", getTitleOnUiThread());
105 executeJavaScriptAndWaitForResult("dismiss();");
108 } catch (InterruptedException e) {}
109 getInstrumentation().waitForIdleSync();
110 assertEquals("notification closed", getTitleOnUiThread());
114 @Feature({"WebNotification"})
115 public void testWebNotificationShowAndCloseByUser() throws Throwable {
116 loadAssetFile("notification.html");
117 getInstrumentation().waitForIdleSync();
118 executeJavaScriptAndWaitForResult("notify();");
121 } catch (InterruptedException e) {}
122 getInstrumentation().waitForIdleSync();
123 assertEquals("notification shown", getTitleOnUiThread());
124 mNotificationService.mockClose();
127 } catch (InterruptedException e) {}
128 getInstrumentation().waitForIdleSync();
129 assertEquals("notification closed", getTitleOnUiThread());