Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Side by Side Diff: content/test/layout_tests/runner/NotificationPresenter.cpp

Issue 110533009: Import TestRunner library into chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. 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 /*
6 * Copyright (C) 2010 Google Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Google Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "content/test/layout_tests/runner/NotificationPresenter.h"
36
37 #include "content/public/test/layout_tests/WebTestDelegate.h"
38 #include "third_party/WebKit/public/platform/Platform.h"
39 #include "third_party/WebKit/public/platform/WebString.h"
40 #include "third_party/WebKit/public/platform/WebURL.h"
41 #include "third_party/WebKit/public/web/WebKit.h"
42 #include "third_party/WebKit/public/web/WebNotificationPermissionCallback.h"
43 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
44 #include "url/gurl.h"
45
46 using namespace blink;
47 using namespace std;
48
49 namespace WebTestRunner {
50
51 namespace {
52
53 WebString identifierForNotification(const WebNotification& notification)
54 {
55 if (notification.isHTML())
56 return notification.url().spec().utf16();
57 return notification.title();
58 }
59
60 void deferredDisplayDispatch(void* context)
61 {
62 WebNotification* notification = static_cast<WebNotification*>(context);
63 notification->dispatchDisplayEvent();
64 delete notification;
65 }
66
67 }
68
69 NotificationPresenter::NotificationPresenter()
70 : m_delegate(0)
71 {
72 }
73
74 NotificationPresenter::~NotificationPresenter()
75 {
76 }
77
78 void NotificationPresenter::grantPermission(const WebString& origin)
79 {
80 m_allowedOrigins.insert(origin.utf8());
81 }
82
83 bool NotificationPresenter::simulateClick(const WebString& title)
84 {
85 string id(title.utf8());
86 if (m_activeNotifications.find(id) == m_activeNotifications.end())
87 return false;
88
89 const WebNotification& notification = m_activeNotifications.find(id)->second ;
90 WebNotification eventTarget(notification);
91 eventTarget.dispatchClickEvent();
92 return true;
93 }
94
95 void NotificationPresenter::cancelAllActiveNotifications()
96 {
97 while (!m_activeNotifications.empty()) {
98 const WebNotification& notification = m_activeNotifications.begin()->sec ond;
99 cancel(notification);
100 }
101 }
102
103 // The output from all these methods matches what DumpRenderTree produces.
104 bool NotificationPresenter::show(const WebNotification& notification)
105 {
106 WebString identifier = identifierForNotification(notification);
107 if (!notification.replaceId().isEmpty()) {
108 string replaceId(notification.replaceId().utf8());
109 if (m_replacements.find(replaceId) != m_replacements.end())
110 m_delegate->printMessage(string("REPLACING NOTIFICATION ") + m_repla cements.find(replaceId)->second + "\n");
111
112 m_replacements[replaceId] = identifier.utf8();
113 }
114
115 if (notification.isHTML())
116 m_delegate->printMessage(string("DESKTOP NOTIFICATION: contents at ") + string(notification.url().spec()) + "\n");
117 else {
118 m_delegate->printMessage("DESKTOP NOTIFICATION:");
119 m_delegate->printMessage(notification.direction() == WebTextDirectionRig htToLeft ? "(RTL)" : "");
120 m_delegate->printMessage(" icon ");
121 m_delegate->printMessage(notification.iconURL().isEmpty() ? "" : notific ation.iconURL().spec().data());
122 m_delegate->printMessage(", title ");
123 m_delegate->printMessage(notification.title().isEmpty() ? "" : notificat ion.title().utf8().data());
124 m_delegate->printMessage(", text ");
125 m_delegate->printMessage(notification.body().isEmpty() ? "" : notificati on.body().utf8().data());
126 m_delegate->printMessage("\n");
127 }
128
129 string id(identifier.utf8());
130 m_activeNotifications[id] = notification;
131
132 Platform::current()->callOnMainThread(deferredDisplayDispatch, new WebNotifi cation(notification));
133 return true;
134 }
135
136 void NotificationPresenter::cancel(const WebNotification& notification)
137 {
138 WebString identifier = identifierForNotification(notification);
139 m_delegate->printMessage(string("DESKTOP NOTIFICATION CLOSED: ") + string(id entifier.utf8()) + "\n");
140 WebNotification eventTarget(notification);
141 eventTarget.dispatchCloseEvent(false);
142
143 string id(identifier.utf8());
144 m_activeNotifications.erase(id);
145 }
146
147 void NotificationPresenter::objectDestroyed(const blink::WebNotification& notifi cation)
148 {
149 WebString identifier = identifierForNotification(notification);
150 string id(identifier.utf8());
151 m_activeNotifications.erase(id);
152 }
153
154 WebNotificationPresenter::Permission NotificationPresenter::checkPermission(cons t WebSecurityOrigin& origin)
155 {
156 // Check with the layout test controller
157 WebString originString = origin.toString();
158 bool allowed = m_allowedOrigins.find(string(originString.utf8())) != m_allow edOrigins.end();
159 return allowed ? WebNotificationPresenter::PermissionAllowed
160 : WebNotificationPresenter::PermissionDenied;
161 }
162
163 void NotificationPresenter::requestPermission(
164 const WebSecurityOrigin& origin,
165 WebNotificationPermissionCallback* callback)
166 {
167 m_delegate->printMessage("DESKTOP NOTIFICATION PERMISSION REQUESTED: " + str ing(origin.toString().utf8()) + "\n");
168 callback->permissionRequestComplete();
169 }
170
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698