OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/test/histogram_tester.h" |
10 #include "chrome/browser/budget_service/budget_manager.h" | 11 #include "chrome/browser/budget_service/budget_manager.h" |
11 #include "chrome/browser/budget_service/budget_manager_factory.h" | 12 #include "chrome/browser/budget_service/budget_manager_factory.h" |
12 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #include "chrome/browser/engagement/site_engagement_service.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
15 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
16 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 20 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
(...skipping 17 matching lines...) Expand all Loading... |
37 | 38 |
38 void SetSiteEngagementScore(double score) { | 39 void SetSiteEngagementScore(double score) { |
39 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 40 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
40 service->ResetScoreForURL(GURL(origin().Serialize()), score); | 41 service->ResetScoreForURL(GURL(origin().Serialize()), score); |
41 } | 42 } |
42 | 43 |
43 Profile* profile() { return &profile_; } | 44 Profile* profile() { return &profile_; } |
44 const url::Origin origin() const { return origin_; } | 45 const url::Origin origin() const { return origin_; } |
45 void SetOrigin(const url::Origin& origin) { origin_ = origin; } | 46 void SetOrigin(const url::Origin& origin) { origin_ = origin; } |
46 | 47 |
| 48 void GetBudgetCallback(base::Closure run_loop_closure, |
| 49 blink::mojom::BudgetServiceErrorType error, |
| 50 std::vector<blink::mojom::BudgetStatePtr> budget) { |
| 51 // The BudgetDatabaseTest class tests all of the budget values returned. |
| 52 // This is just checking that the UMA records correctly, so no need to keep |
| 53 // track of the budget value. |
| 54 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE); |
| 55 error_ = error; |
| 56 run_loop_closure.Run(); |
| 57 } |
| 58 |
47 void ReserveCallback(base::Closure run_loop_closure, | 59 void ReserveCallback(base::Closure run_loop_closure, |
48 blink::mojom::BudgetServiceErrorType error, | 60 blink::mojom::BudgetServiceErrorType error, |
49 bool success) { | 61 bool success) { |
50 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE) && success; | 62 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE) && success; |
51 error_ = error; | 63 error_ = error; |
52 run_loop_closure.Run(); | 64 run_loop_closure.Run(); |
53 } | 65 } |
54 | 66 |
55 void StatusCallback(base::Closure run_loop_closure, bool success) { | 67 void ConsumeCallback(base::Closure run_loop_closure, bool success) { |
56 success_ = success; | 68 success_ = success; |
57 run_loop_closure.Run(); | 69 run_loop_closure.Run(); |
58 } | 70 } |
59 | 71 |
| 72 bool GetBudget() { |
| 73 base::RunLoop run_loop; |
| 74 GetManager()->GetBudget( |
| 75 origin(), base::Bind(&BudgetManagerTest::GetBudgetCallback, |
| 76 base::Unretained(this), run_loop.QuitClosure())); |
| 77 run_loop.Run(); |
| 78 return success_; |
| 79 } |
| 80 |
60 bool ReserveBudget(blink::mojom::BudgetOperationType type) { | 81 bool ReserveBudget(blink::mojom::BudgetOperationType type) { |
61 base::RunLoop run_loop; | 82 base::RunLoop run_loop; |
62 GetManager()->Reserve( | 83 GetManager()->Reserve( |
63 origin(), type, | 84 origin(), type, |
64 base::Bind(&BudgetManagerTest::ReserveCallback, base::Unretained(this), | 85 base::Bind(&BudgetManagerTest::ReserveCallback, base::Unretained(this), |
65 run_loop.QuitClosure())); | 86 run_loop.QuitClosure())); |
66 run_loop.Run(); | 87 run_loop.Run(); |
67 return success_; | 88 return success_; |
68 } | 89 } |
69 | 90 |
70 bool ConsumeBudget(blink::mojom::BudgetOperationType type) { | 91 bool ConsumeBudget(blink::mojom::BudgetOperationType type) { |
71 base::RunLoop run_loop; | 92 base::RunLoop run_loop; |
72 GetManager()->Consume( | 93 GetManager()->Consume( |
73 origin(), type, | 94 origin(), type, |
74 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), | 95 base::Bind(&BudgetManagerTest::ConsumeCallback, base::Unretained(this), |
75 run_loop.QuitClosure())); | 96 run_loop.QuitClosure())); |
76 run_loop.Run(); | 97 run_loop.Run(); |
77 return success_; | 98 return success_; |
78 } | 99 } |
79 | 100 |
80 // Members for callbacks to set. | 101 // Members for callbacks to set. |
81 bool success_; | 102 bool success_; |
82 blink::mojom::BudgetServiceErrorType error_; | 103 blink::mojom::BudgetServiceErrorType error_; |
83 | 104 |
| 105 protected: |
| 106 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
| 107 |
84 private: | 108 private: |
85 content::TestBrowserThreadBundle thread_bundle_; | 109 content::TestBrowserThreadBundle thread_bundle_; |
86 TestingProfile profile_; | 110 TestingProfile profile_; |
| 111 base::HistogramTester histogram_tester_; |
87 url::Origin origin_; | 112 url::Origin origin_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(BudgetManagerTest); |
88 }; | 115 }; |
89 | 116 |
90 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { | 117 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { |
91 // Set initial SES. The first time we try to spend budget, the | 118 // Set initial SES. The first time we try to spend budget, the |
92 // engagement award will be granted which is 48.0. | 119 // engagement award will be granted which is 48.0. |
93 SetSiteEngagementScore(kTestSES); | 120 SetSiteEngagementScore(kTestSES); |
94 const blink::mojom::BudgetOperationType type = | 121 const blink::mojom::BudgetOperationType type = |
95 blink::mojom::BudgetOperationType::SILENT_PUSH; | 122 blink::mojom::BudgetOperationType::SILENT_PUSH; |
96 | 123 |
97 // Spend for 24 silent push messages. This should consume all the original | 124 // Spend for 24 silent push messages. This should consume all the original |
98 // budget grant. | 125 // budget grant. |
99 for (int i = 0; i < 24; i++) | 126 for (int i = 0; i < 24; i++) { |
| 127 ASSERT_TRUE(GetBudget()); |
100 ASSERT_TRUE(ReserveBudget(type)); | 128 ASSERT_TRUE(ReserveBudget(type)); |
| 129 } |
101 | 130 |
102 // Try to send one final silent push. The origin should be out of budget. | 131 // Try to send one final silent push. The origin should be out of budget. |
| 132 ASSERT_TRUE(GetBudget()); |
103 ASSERT_FALSE(ReserveBudget(type)); | 133 ASSERT_FALSE(ReserveBudget(type)); |
104 | 134 |
105 // Try to consume for the 24 messages reserved. | 135 // Try to consume for the 24 messages reserved. |
106 for (int i = 0; i < 24; i++) | 136 for (int i = 0; i < 24; i++) |
107 ASSERT_TRUE(ConsumeBudget(type)); | 137 ASSERT_TRUE(ConsumeBudget(type)); |
108 | 138 |
109 // The next consume should fail, since there is no reservation or budget | 139 // The next consume should fail, since there is no reservation or budget |
110 // available. | 140 // available. |
111 ASSERT_FALSE(ConsumeBudget(type)); | 141 ASSERT_FALSE(ConsumeBudget(type)); |
| 142 |
| 143 // Check the the UMA recorded for the Reserve calls matches the operations |
| 144 // that were executed. |
| 145 std::vector<base::Bucket> buckets = |
| 146 histogram_tester()->GetAllSamples("Blink.BudgetAPI.Reserve"); |
| 147 ASSERT_EQ(2U, buckets.size()); |
| 148 // 1 failed reserve call. |
| 149 EXPECT_EQ(0, buckets[0].min); |
| 150 EXPECT_EQ(1, buckets[0].count); |
| 151 // 24 successful reserve calls. |
| 152 EXPECT_EQ(1, buckets[1].min); |
| 153 EXPECT_EQ(24, buckets[1].count); |
| 154 |
| 155 // Check that the UMA recorded for the GetBudget calls matches the operations |
| 156 // that were executed. |
| 157 buckets = histogram_tester()->GetAllSamples("Blink.BudgetAPI.QueryBudget"); |
| 158 |
| 159 int num_samples = 0; |
| 160 for (const base::Bucket& bucket : buckets) |
| 161 num_samples += bucket.count; |
| 162 EXPECT_EQ(25, num_samples); |
112 } | 163 } |
113 | 164 |
114 TEST_F(BudgetManagerTest, TestInsecureOrigin) { | 165 TEST_F(BudgetManagerTest, TestInsecureOrigin) { |
115 const blink::mojom::BudgetOperationType type = | 166 const blink::mojom::BudgetOperationType type = |
116 blink::mojom::BudgetOperationType::SILENT_PUSH; | 167 blink::mojom::BudgetOperationType::SILENT_PUSH; |
117 SetOrigin(url::Origin(GURL("http://example.com"))); | 168 SetOrigin(url::Origin(GURL("http://example.com"))); |
118 SetSiteEngagementScore(kTestSES); | 169 SetSiteEngagementScore(kTestSES); |
119 | 170 |
120 // Methods on the BudgetManager should only be allowed for secure origins. | 171 // Methods on the BudgetManager should only be allowed for secure origins. |
121 ASSERT_FALSE(ReserveBudget(type)); | 172 ASSERT_FALSE(ReserveBudget(type)); |
122 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); | 173 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); |
123 ASSERT_FALSE(ConsumeBudget(type)); | 174 ASSERT_FALSE(ConsumeBudget(type)); |
124 } | 175 } |
125 | 176 |
126 TEST_F(BudgetManagerTest, TestUniqueOrigin) { | 177 TEST_F(BudgetManagerTest, TestUniqueOrigin) { |
127 const blink::mojom::BudgetOperationType type = | 178 const blink::mojom::BudgetOperationType type = |
128 blink::mojom::BudgetOperationType::SILENT_PUSH; | 179 blink::mojom::BudgetOperationType::SILENT_PUSH; |
129 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd"))); | 180 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd"))); |
130 | 181 |
131 // Methods on the BudgetManager should not be allowed for unique origins. | 182 // Methods on the BudgetManager should not be allowed for unique origins. |
132 ASSERT_FALSE(ReserveBudget(type)); | 183 ASSERT_FALSE(ReserveBudget(type)); |
133 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); | 184 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); |
134 ASSERT_FALSE(ConsumeBudget(type)); | 185 ASSERT_FALSE(ConsumeBudget(type)); |
135 } | 186 } |
OLD | NEW |