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

Side by Side Diff: chrome/browser/push_messaging/push_messaging_app_identifier_unittest.cc

Issue 1122403009: Add tests for PushMessagingAppIdentifier (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ident_refactor
Patch Set: Address peter's final nits Created 5 years, 7 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) {
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 SetUp() override {
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_;
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 ASSERT_TRUE(PushMessagingAppIdentifier::Get(profile(),
81 original_.app_id()).is_null());
82 ASSERT_TRUE(PushMessagingAppIdentifier::Get(profile(), original_.origin(),
83 original_.service_worker_registration_id()).is_null());
84
85 // Test basic PersistToPrefs round trips.
86 original_.PersistToPrefs(profile());
87 {
88 PushMessagingAppIdentifier get_by_app_id =
89 PushMessagingAppIdentifier::Get(profile(), original_.app_id());
90 EXPECT_FALSE(get_by_app_id.is_null());
91 ExpectAppIdentifiersEqual(original_, get_by_app_id);
92 }
93 {
94 PushMessagingAppIdentifier get_by_origin_and_swr_id =
95 PushMessagingAppIdentifier::Get(profile(),
96 original_.origin(), original_.service_worker_registration_id());
97 EXPECT_FALSE(get_by_origin_and_swr_id.is_null());
98 ExpectAppIdentifiersEqual(original_, get_by_origin_and_swr_id);
99 }
100 }
101
102 TEST_F(PushMessagingAppIdentifierTest, PersistOverwritesSameOriginAndSW) {
103 original_.PersistToPrefs(profile());
104
105 // Test that PersistToPrefs overwrites when same origin and Service Worker.
106 ASSERT_NE(original_.app_id(), same_origin_and_sw_.app_id());
107 ASSERT_EQ(original_.origin(), same_origin_and_sw_.origin());
108 ASSERT_EQ(original_.service_worker_registration_id(),
109 same_origin_and_sw_.service_worker_registration_id());
110 same_origin_and_sw_.PersistToPrefs(profile());
111 {
112 PushMessagingAppIdentifier get_by_original_app_id =
113 PushMessagingAppIdentifier::Get(profile(), original_.app_id());
114 EXPECT_TRUE(get_by_original_app_id.is_null());
115 }
116 {
117 PushMessagingAppIdentifier get_by_soas_app_id =
118 PushMessagingAppIdentifier::Get(profile(),
119 same_origin_and_sw_.app_id());
120 EXPECT_FALSE(get_by_soas_app_id.is_null());
121 ExpectAppIdentifiersEqual(same_origin_and_sw_, get_by_soas_app_id);
122 }
123 {
124 PushMessagingAppIdentifier get_by_original_origin_and_swr_id =
125 PushMessagingAppIdentifier::Get(profile(),
126 original_.origin(), original_.service_worker_registration_id());
127 EXPECT_FALSE(get_by_original_origin_and_swr_id.is_null());
128 ExpectAppIdentifiersEqual(same_origin_and_sw_,
129 get_by_original_origin_and_swr_id);
130 }
131 }
132
133 TEST_F(PushMessagingAppIdentifierTest, PersistDoesNotOverwriteDifferent) {
134 original_.PersistToPrefs(profile());
135
136 // Test that PersistToPrefs doesn't overwrite when different origin or SW.
137 ASSERT_NE(original_.app_id(), different_origin_.app_id());
138 ASSERT_NE(original_.app_id(), different_sw_.app_id());
139 different_origin_.PersistToPrefs(profile());
140 different_sw_.PersistToPrefs(profile());
141 {
142 PushMessagingAppIdentifier get_by_original_app_id =
143 PushMessagingAppIdentifier::Get(profile(), original_.app_id());
144 EXPECT_FALSE(get_by_original_app_id.is_null());
145 ExpectAppIdentifiersEqual(original_, get_by_original_app_id);
146 }
147 {
148 PushMessagingAppIdentifier get_by_original_origin_and_swr_id =
149 PushMessagingAppIdentifier::Get(profile(),
150 original_.origin(), original_.service_worker_registration_id());
151 EXPECT_FALSE(get_by_original_origin_and_swr_id.is_null());
152 ExpectAppIdentifiersEqual(original_, get_by_original_origin_and_swr_id);
153 }
154 }
155
156 TEST_F(PushMessagingAppIdentifierTest, DeleteFromPrefs) {
157 original_.PersistToPrefs(profile());
158 different_origin_.PersistToPrefs(profile());
159 different_sw_.PersistToPrefs(profile());
160
161 // Test DeleteFromPrefs. Deleted app identifier should be deleted.
162 original_.DeleteFromPrefs(profile());
163 {
164 PushMessagingAppIdentifier get_by_original_app_id =
165 PushMessagingAppIdentifier::Get(profile(), original_.app_id());
166 EXPECT_TRUE(get_by_original_app_id.is_null());
167 }
168 {
169 PushMessagingAppIdentifier get_by_original_origin_and_swr_id =
170 PushMessagingAppIdentifier::Get(profile(),
171 original_.origin(), original_.service_worker_registration_id());
172 EXPECT_TRUE(get_by_original_origin_and_swr_id.is_null());
173 }
174 }
175
176 TEST_F(PushMessagingAppIdentifierTest, GetAll) {
177 original_.PersistToPrefs(profile());
178 different_origin_.PersistToPrefs(profile());
179 different_sw_.PersistToPrefs(profile());
180
181 original_.DeleteFromPrefs(profile());
182
183 // Test GetAll. Non-deleted app identifiers should all be listed.
184 std::vector<PushMessagingAppIdentifier> all_app_identifiers =
185 PushMessagingAppIdentifier::GetAll(profile());
186 EXPECT_EQ(2u, all_app_identifiers.size());
187 // Order is unspecified.
188 bool contained_different_origin = false;
189 bool contained_different_sw = false;
190 for (const PushMessagingAppIdentifier& app_identifier : all_app_identifiers) {
191 if (app_identifier.app_id() == different_origin_.app_id()) {
192 ExpectAppIdentifiersEqual(different_origin_, app_identifier);
193 contained_different_origin = true;
194 } else {
195 ExpectAppIdentifiersEqual(different_sw_, app_identifier);
196 contained_different_sw = true;
197 }
198 }
199 EXPECT_TRUE(contained_different_origin);
200 EXPECT_TRUE(contained_different_sw);
201 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698