Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 5 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 6 | |
| 7 #include "chrome/test/base/testing_profile.h" | |
| 8 #include "content/public/test/test_browser_thread_bundle.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 10 |
| 11 namespace { | |
| 12 | |
| 13 void ExpectAppIdentifiersEqual(const PushMessagingAppIdentifier& a, | |
| 14 const PushMessagingAppIdentifier& b) { | |
|
Peter Beverloo
2015/05/13 13:49:29
nit: indent
johnme
2015/05/13 14:05:49
Done.
| |
| 15 EXPECT_EQ(a.app_id(), b.app_id()); | |
| 16 EXPECT_EQ(a.origin(), b.origin()); | |
| 17 EXPECT_EQ(a.service_worker_registration_id(), | |
| 18 b.service_worker_registration_id()); | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 8 class PushMessagingAppIdentifierTest : public testing::Test { | 23 class PushMessagingAppIdentifierTest : public testing::Test { |
| 9 protected: | 24 protected: |
| 10 PushMessagingAppIdentifier GenerateId( | 25 PushMessagingAppIdentifier GenerateId( |
| 11 const GURL& origin, | 26 const GURL& origin, |
| 12 int64_t service_worker_registration_id) { | 27 int64_t service_worker_registration_id) { |
| 13 // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it | 28 // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it |
| 14 // to generate app_id, and then use private constructor. | 29 // to generate app_id, and then use private constructor. |
| 15 std::string app_id = PushMessagingAppIdentifier::Generate( | 30 std::string app_id = PushMessagingAppIdentifier::Generate( |
| 16 GURL("https://www.example.com/"), 1).app_id(); | 31 GURL("https://www.example.com/"), 1).app_id(); |
| 17 return PushMessagingAppIdentifier(app_id, origin, | 32 return PushMessagingAppIdentifier(app_id, origin, |
| 18 service_worker_registration_id); | 33 service_worker_registration_id); |
| 19 } | 34 } |
| 35 | |
| 36 void GenerateAppIdentifiers() { | |
|
Peter Beverloo
2015/05/13 13:49:29
nit: why can't we do this in SetUp()?
johnme
2015/05/13 14:05:49
Done.
| |
| 37 original_ = PushMessagingAppIdentifier::Generate( | |
| 38 GURL("https://www.example.com/"), 1); | |
| 39 same_origin_and_sw_ = PushMessagingAppIdentifier::Generate( | |
| 40 GURL("https://www.example.com"), 1); | |
| 41 different_origin_ = PushMessagingAppIdentifier::Generate( | |
| 42 GURL("https://foobar.example.com/"), 1); | |
| 43 different_sw_ = PushMessagingAppIdentifier::Generate( | |
| 44 GURL("https://www.example.com/"), 42); | |
| 45 } | |
| 46 | |
| 47 Profile* profile() { return &profile_; } | |
| 48 | |
| 49 PushMessagingAppIdentifier original_; | |
|
Peter Beverloo
2015/05/13 13:49:29
nit: private w/ getters
johnme
2015/05/13 14:05:49
Kept this, as you mentioned offline that the style
| |
| 50 PushMessagingAppIdentifier same_origin_and_sw_; | |
| 51 PushMessagingAppIdentifier different_origin_; | |
| 52 PushMessagingAppIdentifier different_sw_; | |
| 53 | |
| 54 private: | |
| 55 content::TestBrowserThreadBundle thread_bundle_; | |
| 56 TestingProfile profile_; | |
| 20 }; | 57 }; |
| 21 | 58 |
| 22 TEST_F(PushMessagingAppIdentifierTest, ConstructorValidity) { | 59 TEST_F(PushMessagingAppIdentifierTest, ConstructorValidity) { |
| 23 // The following two are valid: | 60 // The following two are valid: |
| 24 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/"), 1).is_null()); | 61 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/"), 1).is_null()); |
| 25 EXPECT_FALSE(GenerateId(GURL("https://www.example.com"), 1).is_null()); | 62 EXPECT_FALSE(GenerateId(GURL("https://www.example.com"), 1).is_null()); |
| 26 // The following four are invalid and will DCHECK in Generate: | 63 // The following four are invalid and will DCHECK in Generate: |
| 27 EXPECT_FALSE(GenerateId(GURL(""), 1).is_null()); | 64 EXPECT_FALSE(GenerateId(GURL(""), 1).is_null()); |
| 28 EXPECT_FALSE(GenerateId(GURL("foo"), 1).is_null()); | 65 EXPECT_FALSE(GenerateId(GURL("foo"), 1).is_null()); |
| 29 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/foo"), 1).is_null()); | 66 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/foo"), 1).is_null()); |
| 30 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/#foo"), 1).is_null()); | 67 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/#foo"), 1).is_null()); |
| 31 // The following one is invalid and will DCHECK in Generate and be null: | 68 // The following one is invalid and will DCHECK in Generate and be null: |
| 32 EXPECT_TRUE(GenerateId(GURL("https://www.example.com/"), -1).is_null()); | 69 EXPECT_TRUE(GenerateId(GURL("https://www.example.com/"), -1).is_null()); |
| 33 } | 70 } |
| 34 | 71 |
| 35 TEST_F(PushMessagingAppIdentifierTest, UniqueGuids) { | 72 TEST_F(PushMessagingAppIdentifierTest, UniqueGuids) { |
| 36 EXPECT_NE(PushMessagingAppIdentifier::Generate( | 73 EXPECT_NE(PushMessagingAppIdentifier::Generate( |
| 37 GURL("https://www.example.com/"), 1).app_id(), | 74 GURL("https://www.example.com/"), 1).app_id(), |
| 38 PushMessagingAppIdentifier::Generate( | 75 PushMessagingAppIdentifier::Generate( |
| 39 GURL("https://www.example.com/"), 1).app_id()); | 76 GURL("https://www.example.com/"), 1).app_id()); |
| 40 } | 77 } |
| 78 | |
| 79 TEST_F(PushMessagingAppIdentifierTest, PersistAndGet) { | |
| 80 GenerateAppIdentifiers(); | |
| 81 | |
| 82 ASSERT_TRUE(PushMessagingAppIdentifier::Get(profile(), | |
| 83 original_.app_id()).is_null()); | |
| 84 ASSERT_TRUE(PushMessagingAppIdentifier::Get(profile(), original_.origin(), | |
| 85 original_.service_worker_registration_id()).is_null()); | |
| 86 | |
| 87 // Test basic PersistToPrefs round trips. | |
| 88 original_.PersistToPrefs(profile()); | |
| 89 { | |
| 90 PushMessagingAppIdentifier get_by_app_id = | |
| 91 PushMessagingAppIdentifier::Get(profile(), original_.app_id()); | |
| 92 EXPECT_FALSE(get_by_app_id.is_null()); | |
| 93 ExpectAppIdentifiersEqual(original_, get_by_app_id); | |
| 94 } | |
| 95 { | |
| 96 PushMessagingAppIdentifier get_by_origin_and_swr_id = | |
| 97 PushMessagingAppIdentifier::Get(profile(), | |
| 98 original_.origin(), original_.service_worker_registration_id()); | |
| 99 EXPECT_FALSE(get_by_origin_and_swr_id.is_null()); | |
| 100 ExpectAppIdentifiersEqual(original_, get_by_origin_and_swr_id); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 TEST_F(PushMessagingAppIdentifierTest, PersistOverwritesSameOriginAndSW) { | |
| 105 GenerateAppIdentifiers(); | |
| 106 | |
| 107 original_.PersistToPrefs(profile()); | |
| 108 | |
| 109 // Test that PersistToPrefs overwrites when same origin and Service Worker. | |
| 110 ASSERT_NE(original_.app_id(), same_origin_and_sw_.app_id()); | |
| 111 ASSERT_EQ(original_.origin(), same_origin_and_sw_.origin()); | |
| 112 ASSERT_EQ(original_.service_worker_registration_id(), | |
| 113 same_origin_and_sw_.service_worker_registration_id()); | |
| 114 same_origin_and_sw_.PersistToPrefs(profile()); | |
| 115 { | |
| 116 PushMessagingAppIdentifier get_by_original_app_id = | |
| 117 PushMessagingAppIdentifier::Get(profile(), original_.app_id()); | |
| 118 EXPECT_TRUE(get_by_original_app_id.is_null()); | |
| 119 } | |
| 120 { | |
| 121 PushMessagingAppIdentifier get_by_soas_app_id = | |
| 122 PushMessagingAppIdentifier::Get(profile(), | |
| 123 same_origin_and_sw_.app_id()); | |
| 124 EXPECT_FALSE(get_by_soas_app_id.is_null()); | |
| 125 ExpectAppIdentifiersEqual(same_origin_and_sw_, get_by_soas_app_id); | |
| 126 } | |
| 127 { | |
| 128 PushMessagingAppIdentifier get_by_original_origin_and_swr_id = | |
| 129 PushMessagingAppIdentifier::Get(profile(), | |
| 130 original_.origin(), original_.service_worker_registration_id()); | |
| 131 EXPECT_FALSE(get_by_original_origin_and_swr_id.is_null()); | |
| 132 ExpectAppIdentifiersEqual(same_origin_and_sw_, | |
| 133 get_by_original_origin_and_swr_id); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 TEST_F(PushMessagingAppIdentifierTest, PersistDoesNotOverwriteDifferent) { | |
| 138 GenerateAppIdentifiers(); | |
| 139 | |
| 140 original_.PersistToPrefs(profile()); | |
| 141 | |
| 142 // Test that PersistToPrefs doesn't overwrite when different origin or SW. | |
| 143 ASSERT_NE(original_.app_id(), different_origin_.app_id()); | |
| 144 ASSERT_NE(original_.app_id(), different_sw_.app_id()); | |
| 145 different_origin_.PersistToPrefs(profile()); | |
| 146 different_sw_.PersistToPrefs(profile()); | |
| 147 { | |
| 148 PushMessagingAppIdentifier get_by_original_app_id = | |
| 149 PushMessagingAppIdentifier::Get(profile(), original_.app_id()); | |
| 150 EXPECT_FALSE(get_by_original_app_id.is_null()); | |
| 151 ExpectAppIdentifiersEqual(original_, get_by_original_app_id); | |
| 152 } | |
| 153 { | |
| 154 PushMessagingAppIdentifier get_by_original_origin_and_swr_id = | |
| 155 PushMessagingAppIdentifier::Get(profile(), | |
| 156 original_.origin(), original_.service_worker_registration_id()); | |
| 157 EXPECT_FALSE(get_by_original_origin_and_swr_id.is_null()); | |
| 158 ExpectAppIdentifiersEqual(original_, get_by_original_origin_and_swr_id); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 TEST_F(PushMessagingAppIdentifierTest, DeleteFromPrefs) { | |
| 163 GenerateAppIdentifiers(); | |
| 164 | |
| 165 original_.PersistToPrefs(profile()); | |
| 166 different_origin_.PersistToPrefs(profile()); | |
| 167 different_sw_.PersistToPrefs(profile()); | |
| 168 | |
| 169 // Test DeleteFromPrefs. Deleted app identifier should be deleted. | |
| 170 original_.DeleteFromPrefs(profile()); | |
| 171 { | |
| 172 PushMessagingAppIdentifier get_by_original_app_id = | |
| 173 PushMessagingAppIdentifier::Get(profile(), original_.app_id()); | |
| 174 EXPECT_TRUE(get_by_original_app_id.is_null()); | |
| 175 } | |
| 176 { | |
| 177 PushMessagingAppIdentifier get_by_original_origin_and_swr_id = | |
| 178 PushMessagingAppIdentifier::Get(profile(), | |
| 179 original_.origin(), original_.service_worker_registration_id()); | |
| 180 EXPECT_TRUE(get_by_original_origin_and_swr_id.is_null()); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 TEST_F(PushMessagingAppIdentifierTest, GetAll) { | |
| 185 GenerateAppIdentifiers(); | |
| 186 | |
| 187 original_.PersistToPrefs(profile()); | |
| 188 different_origin_.PersistToPrefs(profile()); | |
| 189 different_sw_.PersistToPrefs(profile()); | |
| 190 | |
| 191 original_.DeleteFromPrefs(profile()); | |
| 192 | |
| 193 // Test GetAll. Non-deleted app identifiers should all be listed. | |
| 194 std::vector<PushMessagingAppIdentifier> all_app_identifiers = | |
| 195 PushMessagingAppIdentifier::GetAll(profile()); | |
| 196 EXPECT_EQ(2u, all_app_identifiers.size()); | |
| 197 // Order is unspecified. | |
| 198 bool contained_different_origin = false; | |
| 199 bool contained_different_sw = false; | |
| 200 for (const PushMessagingAppIdentifier& app_identifier : all_app_identifiers) { | |
| 201 if (app_identifier.app_id() == different_origin_.app_id()) { | |
| 202 ExpectAppIdentifiersEqual(different_origin_, app_identifier); | |
| 203 contained_different_origin = true; | |
| 204 } else { | |
| 205 ExpectAppIdentifiersEqual(different_sw_, app_identifier); | |
| 206 contained_different_sw = true; | |
| 207 } | |
| 208 } | |
| 209 EXPECT_TRUE(contained_different_origin); | |
| 210 EXPECT_TRUE(contained_different_sw); | |
| 211 } | |
| OLD | NEW |