OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include <vector> |
| 6 |
| 7 #include "content/browser/notifications/notification_message_filter.h" |
| 8 #include "content/public/browser/platform_notification_service.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/common/platform_notification_data.h" |
| 11 #include "content/public/test/browser_test_utils.h" |
| 12 #include "content/public/test/content_browser_test.h" |
| 13 #include "content/public/test/content_browser_test_utils.h" |
| 14 #include "content/shell/browser/shell.h" |
| 15 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 const char kNotificationPage[] = "files/notifications/test.html"; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Fake implementation of the Platform Notification Service. |
| 26 class FakePlatformNotificationService : public PlatformNotificationService { |
| 27 public: |
| 28 ~FakePlatformNotificationService() override {} |
| 29 |
| 30 // Structure representing a shown persistent notification. |
| 31 struct PersistentNotification { |
| 32 BrowserContext* browser_context; |
| 33 int64_t persistent_notification_id; |
| 34 GURL origin; |
| 35 SkBitmap icon; |
| 36 PlatformNotificationData notification_data; |
| 37 }; |
| 38 |
| 39 size_t GetPersistentNotificationCount() const { |
| 40 return persistent_notifications_.size(); |
| 41 } |
| 42 |
| 43 const PersistentNotification& GetPersistentNotificationAt(size_t index) { |
| 44 DCHECK_LT(index, persistent_notifications_.size()); |
| 45 return persistent_notifications_[index]; |
| 46 } |
| 47 |
| 48 // PlatformNotificationService implementation. |
| 49 // --------------------------------------------------------------------------- |
| 50 |
| 51 blink::WebNotificationPermission CheckPermissionOnUIThread( |
| 52 BrowserContext* browser_context, |
| 53 const GURL& origin, |
| 54 int render_process_id) override { |
| 55 // TODO(peter): Implement this method to enable browser tests for permission |
| 56 // behavior of the Notification API. |
| 57 |
| 58 return blink::WebNotificationPermissionAllowed; |
| 59 } |
| 60 |
| 61 blink::WebNotificationPermission CheckPermissionOnIOThread( |
| 62 ResourceContext* resource_context, |
| 63 const GURL& origin, |
| 64 int render_process_id) override { |
| 65 // TODO(peter): Implement this method to enable browser tests for behavior |
| 66 // of the Notification.permission property. |
| 67 |
| 68 return blink::WebNotificationPermissionAllowed; |
| 69 } |
| 70 |
| 71 void DisplayNotification( |
| 72 BrowserContext* browser_context, |
| 73 const GURL& origin, |
| 74 const SkBitmap& icon, |
| 75 const PlatformNotificationData& notification_data, |
| 76 scoped_ptr<DesktopNotificationDelegate> delegate, |
| 77 base::Closure* cancel_callback) override { |
| 78 // TODO(peter): Implement this method to enable browser tests for non- |
| 79 // persistent notifications. |
| 80 } |
| 81 |
| 82 void DisplayPersistentNotification( |
| 83 BrowserContext* browser_context, |
| 84 int64_t persistent_notification_id, |
| 85 const GURL& origin, |
| 86 const SkBitmap& icon, |
| 87 const PlatformNotificationData& notification_data) override { |
| 88 LOG(INFO) << "Shown a persistent notification."; |
| 89 |
| 90 PersistentNotification notification; |
| 91 notification.browser_context = browser_context; |
| 92 notification.persistent_notification_id = persistent_notification_id; |
| 93 notification.origin = origin; |
| 94 notification.icon = icon; |
| 95 notification.notification_data = notification_data; |
| 96 |
| 97 persistent_notifications_.push_back(notification); |
| 98 } |
| 99 |
| 100 void ClosePersistentNotification( |
| 101 BrowserContext* browser_context, |
| 102 int64_t persistent_notification_id) override { |
| 103 LOG(INFO) << "Closed a persistent notification."; |
| 104 |
| 105 for (auto iter = persistent_notifications_.begin(); |
| 106 iter != persistent_notifications_.end(); ++iter) { |
| 107 const PersistentNotification& notification = *iter; |
| 108 |
| 109 if (notification.persistent_notification_id != persistent_notification_id) |
| 110 continue; |
| 111 |
| 112 persistent_notifications_.erase(iter); |
| 113 } |
| 114 } |
| 115 |
| 116 private: |
| 117 std::vector<PersistentNotification> persistent_notifications_; |
| 118 }; |
| 119 |
| 120 } // namespace |
| 121 |
| 122 class NotificationBrowserTest : public ContentBrowserTest { |
| 123 public: |
| 124 void SetUp() override { |
| 125 NotificationMessageFilter::SetNotificationServiceForTests(&service_); |
| 126 |
| 127 https_server_.reset(new net::SpawnedTestServer( |
| 128 net::SpawnedTestServer::TYPE_HTTPS, |
| 129 net::BaseTestServer::SSLOptions( |
| 130 net::BaseTestServer::SSLOptions::CERT_OK), |
| 131 base::FilePath(FILE_PATH_LITERAL("content/test/data")))); |
| 132 |
| 133 ASSERT_TRUE(https_server_->Start()); |
| 134 |
| 135 ContentBrowserTest::SetUp(); |
| 136 } |
| 137 |
| 138 void TearDown() override { |
| 139 NotificationMessageFilter::SetNotificationServiceForTests(nullptr); |
| 140 |
| 141 ContentBrowserTest::TearDown(); |
| 142 } |
| 143 |
| 144 protected: |
| 145 // Provide an alias to prevent long verbose code in the unit tests. |
| 146 using PersistentNotification = |
| 147 FakePlatformNotificationService::PersistentNotification; |
| 148 |
| 149 // Loads the test page used for Web Notifications and waits until the |
| 150 // navigation has finished. |
| 151 void LoadTestPageAndWait() { |
| 152 ASSERT_TRUE(NavigateToURL(shell(), |
| 153 https_server_->GetURL(kNotificationPage))); |
| 154 } |
| 155 |
| 156 // Executes |script| and asserts that the result is the string "ok". |
| 157 void ExecuteScriptAndWait(const std::string& script) { |
| 158 RenderFrameHost* render_frame_host = |
| 159 shell()->web_contents()->GetMainFrame(); |
| 160 |
| 161 std::string result; |
| 162 ASSERT_TRUE(ExecuteScriptAndExtractString(render_frame_host, |
| 163 script, |
| 164 &result)); |
| 165 |
| 166 ASSERT_EQ("ok", result); |
| 167 } |
| 168 |
| 169 // Returns the fake PlatformNotificationService being used by the test. |
| 170 FakePlatformNotificationService* service() { |
| 171 return &service_; |
| 172 } |
| 173 |
| 174 private: |
| 175 FakePlatformNotificationService service_; |
| 176 |
| 177 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 178 }; |
| 179 |
| 180 IN_PROC_BROWSER_TEST_F(NotificationBrowserTest, DisplayPersistentNotification) { |
| 181 ASSERT_NO_FATAL_FAILURE(LoadTestPageAndWait()); |
| 182 ASSERT_NO_FATAL_FAILURE(ExecuteScriptAndWait( |
| 183 "DisplayPersistentNotification('My Notification');")); |
| 184 |
| 185 ASSERT_EQ(1u, service()->GetPersistentNotificationCount()); |
| 186 |
| 187 const auto& notification = service()->GetPersistentNotificationAt(0); |
| 188 EXPECT_EQ(1, notification.persistent_notification_id); |
| 189 } |
| 190 |
| 191 } // namespace content |
OLD | NEW |