Index: content/browser/notifications/notification_browsertest.cc |
diff --git a/content/browser/notifications/notification_browsertest.cc b/content/browser/notifications/notification_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c0b9a7b0658465abc8a4c92355561af2afacf0da |
--- /dev/null |
+++ b/content/browser/notifications/notification_browsertest.cc |
@@ -0,0 +1,191 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <vector> |
+ |
+#include "content/browser/notifications/notification_message_filter.h" |
+#include "content/public/browser/platform_notification_service.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/common/platform_notification_data.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/content_browser_test.h" |
+#include "content/public/test/content_browser_test_utils.h" |
+#include "content/shell/browser/shell.h" |
+#include "net/test/spawned_test_server/spawned_test_server.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+const char kNotificationPage[] = "files/notifications/test.html"; |
+ |
+namespace { |
+ |
+// Fake implementation of the Platform Notification Service. |
+class FakePlatformNotificationService : public PlatformNotificationService { |
+ public: |
+ ~FakePlatformNotificationService() override {} |
+ |
+ // Structure representing a shown persistent notification. |
+ struct PersistentNotification { |
+ BrowserContext* browser_context; |
+ int64_t persistent_notification_id; |
+ GURL origin; |
+ SkBitmap icon; |
+ PlatformNotificationData notification_data; |
+ }; |
+ |
+ size_t GetPersistentNotificationCount() const { |
+ return persistent_notifications_.size(); |
+ } |
+ |
+ const PersistentNotification& GetPersistentNotificationAt(size_t index) { |
+ DCHECK_LT(index, persistent_notifications_.size()); |
+ return persistent_notifications_[index]; |
+ } |
+ |
+ // PlatformNotificationService implementation. |
+ // --------------------------------------------------------------------------- |
+ |
+ blink::WebNotificationPermission CheckPermissionOnUIThread( |
+ BrowserContext* browser_context, |
+ const GURL& origin, |
+ int render_process_id) override { |
+ // TODO(peter): Implement this method to enable browser tests for permission |
+ // behavior of the Notification API. |
+ |
+ return blink::WebNotificationPermissionAllowed; |
+ } |
+ |
+ blink::WebNotificationPermission CheckPermissionOnIOThread( |
+ ResourceContext* resource_context, |
+ const GURL& origin, |
+ int render_process_id) override { |
+ // TODO(peter): Implement this method to enable browser tests for behavior |
+ // of the Notification.permission property. |
+ |
+ return blink::WebNotificationPermissionAllowed; |
+ } |
+ |
+ void DisplayNotification( |
+ BrowserContext* browser_context, |
+ const GURL& origin, |
+ const SkBitmap& icon, |
+ const PlatformNotificationData& notification_data, |
+ scoped_ptr<DesktopNotificationDelegate> delegate, |
+ base::Closure* cancel_callback) override { |
+ // TODO(peter): Implement this method to enable browser tests for non- |
+ // persistent notifications. |
+ } |
+ |
+ void DisplayPersistentNotification( |
+ BrowserContext* browser_context, |
+ int64_t persistent_notification_id, |
+ const GURL& origin, |
+ const SkBitmap& icon, |
+ const PlatformNotificationData& notification_data) override { |
+ LOG(INFO) << "Shown a persistent notification."; |
+ |
+ PersistentNotification notification; |
+ notification.browser_context = browser_context; |
+ notification.persistent_notification_id = persistent_notification_id; |
+ notification.origin = origin; |
+ notification.icon = icon; |
+ notification.notification_data = notification_data; |
+ |
+ persistent_notifications_.push_back(notification); |
+ } |
+ |
+ void ClosePersistentNotification( |
+ BrowserContext* browser_context, |
+ int64_t persistent_notification_id) override { |
+ LOG(INFO) << "Closed a persistent notification."; |
+ |
+ for (auto iter = persistent_notifications_.begin(); |
+ iter != persistent_notifications_.end(); ++iter) { |
+ const PersistentNotification& notification = *iter; |
+ |
+ if (notification.persistent_notification_id != persistent_notification_id) |
+ continue; |
+ |
+ persistent_notifications_.erase(iter); |
+ } |
+ } |
+ |
+ private: |
+ std::vector<PersistentNotification> persistent_notifications_; |
+}; |
+ |
+} // namespace |
+ |
+class NotificationBrowserTest : public ContentBrowserTest { |
+ public: |
+ void SetUp() override { |
+ NotificationMessageFilter::SetNotificationServiceForTests(&service_); |
+ |
+ https_server_.reset(new net::SpawnedTestServer( |
+ net::SpawnedTestServer::TYPE_HTTPS, |
+ net::BaseTestServer::SSLOptions( |
+ net::BaseTestServer::SSLOptions::CERT_OK), |
+ base::FilePath(FILE_PATH_LITERAL("content/test/data")))); |
+ |
+ ASSERT_TRUE(https_server_->Start()); |
+ |
+ ContentBrowserTest::SetUp(); |
+ } |
+ |
+ void TearDown() override { |
+ NotificationMessageFilter::SetNotificationServiceForTests(nullptr); |
+ |
+ ContentBrowserTest::TearDown(); |
+ } |
+ |
+ protected: |
+ // Provide an alias to prevent long verbose code in the unit tests. |
+ using PersistentNotification = |
+ FakePlatformNotificationService::PersistentNotification; |
+ |
+ // Loads the test page used for Web Notifications and waits until the |
+ // navigation has finished. |
+ void LoadTestPageAndWait() { |
+ ASSERT_TRUE(NavigateToURL(shell(), |
+ https_server_->GetURL(kNotificationPage))); |
+ } |
+ |
+ // Executes |script| and asserts that the result is the string "ok". |
+ void ExecuteScriptAndWait(const std::string& script) { |
+ RenderFrameHost* render_frame_host = |
+ shell()->web_contents()->GetMainFrame(); |
+ |
+ std::string result; |
+ ASSERT_TRUE(ExecuteScriptAndExtractString(render_frame_host, |
+ script, |
+ &result)); |
+ |
+ ASSERT_EQ("ok", result); |
+ } |
+ |
+ // Returns the fake PlatformNotificationService being used by the test. |
+ FakePlatformNotificationService* service() { |
+ return &service_; |
+ } |
+ |
+ private: |
+ FakePlatformNotificationService service_; |
+ |
+ scoped_ptr<net::SpawnedTestServer> https_server_; |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBrowserTest, DisplayPersistentNotification) { |
+ ASSERT_NO_FATAL_FAILURE(LoadTestPageAndWait()); |
+ ASSERT_NO_FATAL_FAILURE(ExecuteScriptAndWait( |
+ "DisplayPersistentNotification('My Notification');")); |
+ |
+ ASSERT_EQ(1u, service()->GetPersistentNotificationCount()); |
+ |
+ const auto& notification = service()->GetPersistentNotificationAt(0); |
+ EXPECT_EQ(1, notification.persistent_notification_id); |
+} |
+ |
+} // namespace content |