Index: content/browser/notifications/notification_id_generator_unittest.cc |
diff --git a/content/browser/notifications/notification_id_generator_unittest.cc b/content/browser/notifications/notification_id_generator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..67b65aadf2dd6099d5c3b1fd67c0f6cdecd04994 |
--- /dev/null |
+++ b/content/browser/notifications/notification_id_generator_unittest.cc |
@@ -0,0 +1,259 @@ |
+// 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 "base/strings/string_number_conversions.h" |
+#include "content/browser/notifications/notification_id_generator.h" |
+#include "content/public/test/test_browser_context.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+namespace { |
+ |
+class TestBrowserContextConfigurableIncognito : public TestBrowserContext { |
+ public: |
+ TestBrowserContextConfigurableIncognito() {} |
+ ~TestBrowserContextConfigurableIncognito() override {} |
+ |
+ void set_incognito(bool incognito) { incognito_ = incognito; } |
+ |
+ // TestBrowserContext implementation. |
+ bool IsOffTheRecord() const override { return incognito_; } |
+ |
+ private: |
+ bool incognito_ = false; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestBrowserContextConfigurableIncognito); |
+}; |
+ |
+} // namespace |
+ |
+const int kRenderProcessId = 42; |
+const int64_t kPersistentNotificationId = 430; |
+const int kNonPersistentNotificationId = 5400; |
+ |
+const char kExampleTag[] = "example"; |
+const char kDifferentTag[] = "different"; |
+ |
+TEST(NotificationIdGeneratorTest, PersistentNotifications) { |
+ TestBrowserContextConfigurableIncognito browser_context; |
+ TestBrowserContext second_browser_context; |
+ |
+ NotificationIdGenerator generator(&browser_context, kRenderProcessId); |
+ |
+ NotificationIdGenerator different_browser_context_generator( |
+ &second_browser_context, kRenderProcessId); |
+ NotificationIdGenerator different_render_process_generator( |
+ &browser_context, kRenderProcessId + 1); |
+ |
+ GURL origin("https://example.com"); |
+ GURL different_origin("https://example.com:81"); |
+ |
+ // Notification ids are unique for persistent notifications given a browser |
+ // context, incognito state, origin and either the tag when non-empty, or the |
+ // persistent notification id when the tag is empty. The render process id |
+ // does not matter, as the persistent notification ids are owned by the |
+ // browser process orthogonal to the renderer that created the notification. |
+ |
+ // Uniqueness of notification ids will be impacted by the browser context. |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId), |
+ different_browser_context_generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId)); |
+ |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId), |
+ different_browser_context_generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId)); |
+ |
+ // Uniqueness of notification ids will be impacted by the fact whether the |
+ // browser context is in Incognito mode. |
+ EXPECT_FALSE(browser_context.IsOffTheRecord()); |
+ std::string normal_notification_id = |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId); |
+ |
+ browser_context.set_incognito(true); |
+ |
+ EXPECT_TRUE(browser_context.IsOffTheRecord()); |
+ std::string incognito_notification_id = |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId); |
+ |
+ EXPECT_NE(normal_notification_id, incognito_notification_id); |
+ |
+ browser_context.set_incognito(false); |
+ |
+ // Uniqueness of notification ids does not depend on render process id that is |
+ // creating the notification for persistent notifications. |
+ EXPECT_EQ( |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId), |
+ different_render_process_generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId)); |
+ |
+ EXPECT_EQ( |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId), |
+ different_render_process_generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId)); |
+ |
+ // The origin of the notification will impact the generated notification id. |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ different_origin, kExampleTag, kPersistentNotificationId)); |
+ |
+ // The tag, when empty, will impact the generated notification id. |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ origin, kDifferentTag, kPersistentNotificationId)); |
+ |
+ // The persistent notification id will impact the generated notification id |
+ // when the given tag is empty. |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId + 1)); |
+ |
+ // Using a numeric tag that could resemble a persistent notification id should |
+ // not be equal to a notification without a tag, but with that id. |
+ EXPECT_NE( |
+ generator.GenerateForPersistentNotification( |
+ origin, |
+ base::IntToString(kPersistentNotificationId), |
+ kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId)); |
+ |
+ // Two notification ids given exactly the same information should be equal. |
+ EXPECT_EQ( |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ origin, kExampleTag, kPersistentNotificationId)); |
+ |
+ EXPECT_EQ( |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId), |
+ generator.GenerateForPersistentNotification( |
+ origin, "" /* tag */, kPersistentNotificationId)); |
+} |
+ |
+TEST(NotificationIdGeneratorTest, NonPersistentNotifications) { |
+ TestBrowserContextConfigurableIncognito browser_context; |
+ TestBrowserContext second_browser_context; |
+ |
+ NotificationIdGenerator generator(&browser_context, kRenderProcessId); |
+ |
+ NotificationIdGenerator different_browser_context_generator( |
+ &second_browser_context, kRenderProcessId); |
+ NotificationIdGenerator different_render_process_generator( |
+ &browser_context, kRenderProcessId + 1); |
+ |
+ GURL origin("https://example.com"); |
+ GURL different_origin("https://example.com:81"); |
+ |
+ // Notification ids are unique for non-persistent notifications given a |
+ // browser context, incognito state, origin, render process id and either the |
+ // tag when non-empty, or the non-persistent notification id when it is empty. |
+ |
+ // Uniqueness of notification ids will be impacted by the browser context. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId), |
+ different_browser_context_generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId)); |
+ |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId), |
+ different_browser_context_generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId)); |
+ |
+ // Uniqueness of notification ids will be impacted by the fact whether the |
+ // browser context is in Incognito mode. |
+ EXPECT_FALSE(browser_context.IsOffTheRecord()); |
+ std::string normal_notification_id = |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId); |
+ |
+ browser_context.set_incognito(true); |
+ |
+ EXPECT_TRUE(browser_context.IsOffTheRecord()); |
+ std::string incognito_notification_id = |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId); |
+ |
+ EXPECT_NE(normal_notification_id, incognito_notification_id); |
+ |
+ browser_context.set_incognito(false); |
+ |
+ // Uniqueness of notification ids does depend on render process id, as each |
+ // of the render processes will generate their own incrementing id. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId), |
+ different_render_process_generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId)); |
+ |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId), |
+ different_render_process_generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId)); |
+ |
+ // The origin of the notification will impact the generated notification id. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ different_origin, kExampleTag, kNonPersistentNotificationId)); |
+ |
+ // The tag, when empty, will impact the generated notification id. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kDifferentTag, kNonPersistentNotificationId)); |
+ |
+ // The non-persistent notification id will impact the generated notification |
+ // id when the given tag is empty. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId + 1)); |
+ |
+ // Using a numeric tag that could resemble a non-persistent notification id |
+ // should not be equal to a notification without a tag, but with that id. |
+ EXPECT_NE( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, |
+ base::IntToString(kNonPersistentNotificationId), |
+ kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId)); |
+ |
+ // Two notification ids given exactly the same information should be equal. |
+ EXPECT_EQ( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ origin, kExampleTag, kNonPersistentNotificationId)); |
+ |
+ EXPECT_EQ( |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId), |
+ generator.GenerateForNonPersistentNotification( |
+ origin, "" /* tag */, kNonPersistentNotificationId)); |
+} |
+ |
+} // namespace content |