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

Unified Diff: content/browser/background_fetch/background_fetch_registration_id_unittest.cc

Issue 2777063008: Connect BackgroundFetch to the OfflineItemCollection
Patch Set: Fixed incognito check and added tests Created 3 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/background_fetch/background_fetch_registration_id_unittest.cc
diff --git a/content/browser/background_fetch/background_fetch_registration_id_unittest.cc b/content/browser/background_fetch/background_fetch_registration_id_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ab2afe725651ab1e8b6a77330036011f142c84b8
--- /dev/null
+++ b/content/browser/background_fetch/background_fetch_registration_id_unittest.cc
@@ -0,0 +1,109 @@
+// Copyright 2017 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 "content/browser/background_fetch/background_fetch_registration_id.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace content {
+
+namespace {
+
+const char kTestOrigin[] = "https://www.example.com";
+const int64_t kTestServiceWorkerRegistrationId = 9001;
+const int64_t kTestLongServiceWorkerRegistrationId = 14294967296;
+const char kTestTag[] = "test_tag";
+const char kExpectedSerialized[] = "0#9001#https://www.example.com#test_tag";
+const char kExpectedLongServiceWorkerIdSerialized[] =
+ "0#14294967296#https://www.example.com#test_tag";
+
+} // anonymous namespace
+
+TEST(BackgroundFetchRegistrationIdTest, SerializeTest) {
+ BackgroundFetchRegistrationId registration_id(
+ kTestServiceWorkerRegistrationId, url::Origin(GURL(kTestOrigin)),
+ kTestTag);
+ std::string serialized_id = registration_id.Serialize();
+ EXPECT_EQ(kExpectedSerialized, serialized_id);
+}
+
+TEST(BackgroundFetchRegistrationIdTest, SerializeLongServiceWorkerIdTest) {
+ BackgroundFetchRegistrationId registration_id(
+ kTestLongServiceWorkerRegistrationId, url::Origin(GURL(kTestOrigin)),
+ kTestTag);
+ std::string serialized_id = registration_id.Serialize();
+ EXPECT_EQ(kExpectedLongServiceWorkerIdSerialized, serialized_id);
+}
+
+TEST(BackgroundFetchRegistrationIdTest, DeserializeSuccessTest) {
+ BackgroundFetchRegistrationId registration_id;
+ ASSERT_TRUE(BackgroundFetchRegistrationId::Deserialize(
+ kExpectedLongServiceWorkerIdSerialized, &registration_id));
+
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize());
+ EXPECT_EQ(kTestLongServiceWorkerRegistrationId,
+ registration_id.service_worker_registration_id());
+ EXPECT_EQ(kTestTag, registration_id.tag());
+}
+
+TEST(BackgroundFetchRegistrationIdTest,
+ DeserializeLongServiceWorkerSuccessTest) {
+ BackgroundFetchRegistrationId registration_id;
+ ASSERT_TRUE(BackgroundFetchRegistrationId::Deserialize(kExpectedSerialized,
+ &registration_id));
+
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize());
+ EXPECT_EQ(kTestServiceWorkerRegistrationId,
+ registration_id.service_worker_registration_id());
+ EXPECT_EQ(kTestTag, registration_id.tag());
+}
+
+TEST(BackgroundFetchRegistrationIdTest, DeserializeComplexTagTest) {
+ BackgroundFetchRegistrationId registration_id;
+ std::string silly_tag("0#9001#https://www.example.com#this#is#a#stupid#tag");
+ ASSERT_TRUE(
+ BackgroundFetchRegistrationId::Deserialize(silly_tag, &registration_id));
+
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize());
+ EXPECT_EQ(kTestServiceWorkerRegistrationId,
+ registration_id.service_worker_registration_id());
+ EXPECT_EQ("this#is#a#stupid#tag", registration_id.tag());
+}
+
+TEST(BackgroundFetchRegistrationIdTest, DeserializeFailureTest) {
+ BackgroundFetchRegistrationId registration_id;
+
+ // Should fail: Invalid version.
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "1#1#https://www.example.com#tag", &registration_id));
+
+ // Should fail: Invalid service worker registration id.
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "0#abc#https://www.example.com#tag", &registration_id));
+
+ // Should fail: Too large service worker registration id.
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "0#11111111111111111111111111111111111#https://www.example.com#tag",
+ &registration_id));
+
+ // Should fail: Invalid origin.
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "0#1#www.example.com#tag", &registration_id));
+
+ // Should fail: Empty origin.
+ EXPECT_FALSE(
+ BackgroundFetchRegistrationId::Deserialize("0#1##tag", &registration_id));
+
+ // Should fail: Not enough fields.
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "0#1#https://www.example.com", &registration_id));
+
+ // Should fail: Invalid tag
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize(
+ "0#1#https://www.example.com#", &registration_id));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698