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 "base/message_loop/message_loop.h" | |
8 #include "chrome/test/base/testing_profile.h" | |
9 #include "content/public/test/test_browser_thread.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
7 | 11 |
12 using content::BrowserThread; | |
13 | |
8 class PushMessagingAppIdentifierTest : public testing::Test { | 14 class PushMessagingAppIdentifierTest : public testing::Test { |
9 protected: | 15 protected: |
10 PushMessagingAppIdentifier GenerateId( | 16 PushMessagingAppIdentifier GenerateId( |
11 const GURL& origin, | 17 const GURL& origin, |
12 int64_t service_worker_registration_id) { | 18 int64_t service_worker_registration_id) { |
13 // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it | 19 // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it |
14 // to generate app_id, and then use private constructor. | 20 // to generate app_id, and then use private constructor. |
15 std::string app_id = PushMessagingAppIdentifier::Generate( | 21 std::string app_id = PushMessagingAppIdentifier::Generate( |
16 GURL("https://www.example.com/"), 1).app_id(); | 22 GURL("https://www.example.com/"), 1).app_id(); |
17 return PushMessagingAppIdentifier(app_id, origin, | 23 return PushMessagingAppIdentifier(app_id, origin, |
(...skipping 13 matching lines...) Expand all Loading... | |
31 // The following one is invalid and will DCHECK in Generate and be null: | 37 // 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()); | 38 EXPECT_TRUE(GenerateId(GURL("https://www.example.com/"), -1).is_null()); |
33 } | 39 } |
34 | 40 |
35 TEST_F(PushMessagingAppIdentifierTest, UniqueGuids) { | 41 TEST_F(PushMessagingAppIdentifierTest, UniqueGuids) { |
36 EXPECT_NE(PushMessagingAppIdentifier::Generate( | 42 EXPECT_NE(PushMessagingAppIdentifier::Generate( |
37 GURL("https://www.example.com/"), 1).app_id(), | 43 GURL("https://www.example.com/"), 1).app_id(), |
38 PushMessagingAppIdentifier::Generate( | 44 PushMessagingAppIdentifier::Generate( |
39 GURL("https://www.example.com/"), 1).app_id()); | 45 GURL("https://www.example.com/"), 1).app_id()); |
40 } | 46 } |
47 | |
48 namespace { | |
49 | |
50 void ExpectEq(const PushMessagingAppIdentifier& a, | |
Peter Beverloo
2015/05/12 13:55:40
I'd slightly prefer to define an (anonymous) equal
johnme
2015/05/13 13:46:20
Discussed offline, and we've agreed that it's ok t
| |
51 const PushMessagingAppIdentifier& b) { | |
52 EXPECT_EQ(a.app_id(), b.app_id()); | |
53 EXPECT_EQ(a.origin(), b.origin()); | |
54 EXPECT_EQ(a.service_worker_registration_id(), | |
55 b.service_worker_registration_id()); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 TEST_F(PushMessagingAppIdentifierTest, PersistAndDeleteFromPrefs) { | |
61 base::MessageLoop message_loop; | |
62 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | |
Peter Beverloo
2015/05/12 13:55:40
Since we've already got the PushMessagingAppIdenti
johnme
2015/05/13 13:46:20
Done.
| |
63 TestingProfile profile; | |
64 | |
65 PushMessagingAppIdentifier original1 = | |
66 PushMessagingAppIdentifier::Generate(GURL("https://www.example.com/"), 1); | |
67 EXPECT_TRUE(PushMessagingAppIdentifier::Get(&profile, | |
68 original1.app_id()).is_null()); | |
69 EXPECT_TRUE(PushMessagingAppIdentifier::Get(&profile, original1.origin(), | |
70 original1.service_worker_registration_id()).is_null()); | |
71 | |
72 // Test basic PersistToPrefs round trips. | |
Peter Beverloo
2015/05/12 13:55:40
nit: This can be a separate test.
johnme
2015/05/13 13:46:20
Done.
| |
73 original1.PersistToPrefs(&profile); | |
74 { | |
75 PushMessagingAppIdentifier get_app_id_1 = | |
76 PushMessagingAppIdentifier::Get(&profile, original1.app_id()); | |
77 EXPECT_FALSE(get_app_id_1.is_null()); | |
78 ExpectEq(original1, get_app_id_1); | |
79 } | |
80 { | |
81 PushMessagingAppIdentifier get_origin_and_swr_id_1 = | |
82 PushMessagingAppIdentifier::Get(&profile, | |
83 original1.origin(), original1.service_worker_registration_id()); | |
84 EXPECT_FALSE(get_origin_and_swr_id_1.is_null()); | |
85 ExpectEq(original1, get_origin_and_swr_id_1); | |
86 } | |
87 | |
88 // Test that PersistToPrefs overwrites (when same origin and Service Worker). | |
Peter Beverloo
2015/05/12 13:55:39
nit: This can be a separate test.
johnme
2015/05/13 13:46:20
Done.
| |
89 PushMessagingAppIdentifier original2 = | |
90 PushMessagingAppIdentifier::Generate(GURL("https://www.example.com/"), 1); | |
91 EXPECT_NE(original1.app_id(), original2.app_id()); | |
92 EXPECT_EQ(original1.origin(), original2.origin()); | |
93 EXPECT_EQ(original1.service_worker_registration_id(), | |
94 original2.service_worker_registration_id()); | |
95 original2.PersistToPrefs(&profile); | |
96 { | |
97 PushMessagingAppIdentifier get_app_id_1 = | |
98 PushMessagingAppIdentifier::Get(&profile, original1.app_id()); | |
99 EXPECT_TRUE(get_app_id_1.is_null()); | |
100 } | |
101 { | |
102 PushMessagingAppIdentifier get_app_id_2 = | |
103 PushMessagingAppIdentifier::Get(&profile, original2.app_id()); | |
104 EXPECT_FALSE(get_app_id_2.is_null()); | |
105 ExpectEq(original2, get_app_id_2); | |
106 } | |
107 { | |
108 PushMessagingAppIdentifier get_origin_and_swr_id_1 = | |
109 PushMessagingAppIdentifier::Get(&profile, | |
110 original1.origin(), original1.service_worker_registration_id()); | |
111 EXPECT_FALSE(get_origin_and_swr_id_1.is_null()); | |
112 ExpectEq(original2, get_origin_and_swr_id_1); | |
113 } | |
114 | |
115 // Test that PersistToPrefs doesn't overwrite (when different origin or SW). | |
Peter Beverloo
2015/05/12 13:55:40
nit: This can be a separate test.
johnme
2015/05/13 13:46:20
Done.
| |
116 PushMessagingAppIdentifier original3 = | |
117 PushMessagingAppIdentifier::Generate(GURL("https://foo.example.com/"), 1); | |
118 PushMessagingAppIdentifier original4 = | |
119 PushMessagingAppIdentifier::Generate(GURL("https://www.example.com/"), 2); | |
120 EXPECT_NE(original2.app_id(), original3.app_id()); | |
121 EXPECT_NE(original2.app_id(), original4.app_id()); | |
122 original3.PersistToPrefs(&profile); | |
123 original4.PersistToPrefs(&profile); | |
124 { | |
125 PushMessagingAppIdentifier get_app_id_2 = | |
126 PushMessagingAppIdentifier::Get(&profile, original2.app_id()); | |
127 EXPECT_FALSE(get_app_id_2.is_null()); | |
128 ExpectEq(original2, get_app_id_2); | |
129 } | |
130 { | |
131 PushMessagingAppIdentifier get_origin_and_swr_id_1 = | |
132 PushMessagingAppIdentifier::Get(&profile, | |
133 original1.origin(), original1.service_worker_registration_id()); | |
134 EXPECT_FALSE(get_origin_and_swr_id_1.is_null()); | |
135 ExpectEq(original2, get_origin_and_swr_id_1); | |
136 } | |
137 | |
138 // Test DeleteFromPrefs. Deleted app identifier should be deleted. | |
Peter Beverloo
2015/05/12 13:55:39
nit: This can be a separate test.
johnme
2015/05/13 13:46:20
Done.
| |
139 original2.DeleteFromPrefs(&profile); | |
140 { | |
141 PushMessagingAppIdentifier get_app_id_2 = | |
142 PushMessagingAppIdentifier::Get(&profile, original2.app_id()); | |
143 EXPECT_TRUE(get_app_id_2.is_null()); | |
144 } | |
145 { | |
146 PushMessagingAppIdentifier get_origin_and_swr_id_1 = | |
147 PushMessagingAppIdentifier::Get(&profile, | |
148 original1.origin(), original1.service_worker_registration_id()); | |
149 EXPECT_TRUE(get_origin_and_swr_id_1.is_null()); | |
150 } | |
151 | |
152 // Test GetAll. Non-deleted app identifiers should all be listed. | |
Peter Beverloo
2015/05/12 13:55:39
nit: This can be a separate test.
johnme
2015/05/13 13:46:20
Done.
| |
153 std::vector<PushMessagingAppIdentifier> all_app_identifiers = | |
154 PushMessagingAppIdentifier::GetAll(&profile); | |
155 EXPECT_EQ(2u, all_app_identifiers.size()); | |
156 // Order is unspecified. | |
157 bool contained_3 = false; | |
158 bool contained_4 = false; | |
159 for (const PushMessagingAppIdentifier& app_identifier : all_app_identifiers) { | |
160 if (app_identifier.app_id() == original3.app_id()) { | |
161 ExpectEq(original3, app_identifier); | |
162 contained_3 = true; | |
163 } else { | |
164 ExpectEq(original4, app_identifier); | |
165 contained_4 = true; | |
166 } | |
167 } | |
168 EXPECT_TRUE(contained_3); | |
169 EXPECT_TRUE(contained_4); | |
170 } | |
OLD | NEW |