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

Side by Side Diff: chrome/browser/budget_service/budget_manager_unittest.cc

Issue 2524533002: Added UMA for usage of BudgetAPI calls. (Closed)
Patch Set: Created 4 years, 1 month 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
OLDNEW
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
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 mojo::Array<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* GetHistogramTester() { 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_;
88 }; 113 };
89 114
90 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { 115 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) {
91 // Set initial SES. The first time we try to spend budget, the 116 // Set initial SES. The first time we try to spend budget, the
92 // engagement award will be granted which is 48.0. 117 // engagement award will be granted which is 48.0.
93 SetSiteEngagementScore(kTestSES); 118 SetSiteEngagementScore(kTestSES);
94 const blink::mojom::BudgetOperationType type = 119 const blink::mojom::BudgetOperationType type =
95 blink::mojom::BudgetOperationType::SILENT_PUSH; 120 blink::mojom::BudgetOperationType::SILENT_PUSH;
96 121
97 // Spend for 24 silent push messages. This should consume all the original 122 // Spend for 24 silent push messages. This should consume all the original
98 // budget grant. 123 // budget grant.
99 for (int i = 0; i < 24; i++) 124 for (int i = 0; i < 24; i++) {
125 ASSERT_TRUE(GetBudget());
100 ASSERT_TRUE(ReserveBudget(type)); 126 ASSERT_TRUE(ReserveBudget(type));
127 }
101 128
102 // Try to send one final silent push. The origin should be out of budget. 129 // Try to send one final silent push. The origin should be out of budget.
130 ASSERT_TRUE(GetBudget());
103 ASSERT_FALSE(ReserveBudget(type)); 131 ASSERT_FALSE(ReserveBudget(type));
104 132
105 // Try to consume for the 24 messages reserved. 133 // Try to consume for the 24 messages reserved.
106 for (int i = 0; i < 24; i++) 134 for (int i = 0; i < 24; i++)
107 ASSERT_TRUE(ConsumeBudget(type)); 135 ASSERT_TRUE(ConsumeBudget(type));
108 136
109 // The next consume should fail, since there is no reservation or budget 137 // The next consume should fail, since there is no reservation or budget
110 // available. 138 // available.
111 ASSERT_FALSE(ConsumeBudget(type)); 139 ASSERT_FALSE(ConsumeBudget(type));
140
141 // Check the the UMA recorded for the Reserve calls matches the operations
142 // that were executed.
143 std::vector<base::Bucket> buckets =
144 GetHistogramTester()->GetAllSamples("PushMessaging.BudgetAPIReserve");
145 ASSERT_EQ(2U, buckets.size());
146 // 1 failed reserve call.
147 EXPECT_EQ(0, buckets[0].min);
148 EXPECT_EQ(1, buckets[0].count);
149 // 24 successful reserve calls.
150 EXPECT_EQ(1, buckets[1].min);
151 EXPECT_EQ(24, buckets[1].count);
152
153 // Check that the UMA recorded for the GetBudget calls matches the operations
154 // that were executed.
155 buckets = GetHistogramTester()->GetAllSamples("PushMessaging.BudgetAPIQuery");
156
157 int num_samples = 0;
158 for (const base::Bucket bucket : buckets)
Peter Beverloo 2016/11/28 10:59:00 nit: const& (if we still need this)
harkness 2016/11/28 13:58:04 Done.
159 num_samples += bucket.count;
160 EXPECT_EQ(25, num_samples);
harkness 2016/11/21 23:05:44 This is a fairly low bar to validate the UMA recor
112 } 161 }
113 162
114 TEST_F(BudgetManagerTest, TestInsecureOrigin) { 163 TEST_F(BudgetManagerTest, TestInsecureOrigin) {
115 const blink::mojom::BudgetOperationType type = 164 const blink::mojom::BudgetOperationType type =
116 blink::mojom::BudgetOperationType::SILENT_PUSH; 165 blink::mojom::BudgetOperationType::SILENT_PUSH;
117 SetOrigin(url::Origin(GURL("http://example.com"))); 166 SetOrigin(url::Origin(GURL("http://example.com")));
118 SetSiteEngagementScore(kTestSES); 167 SetSiteEngagementScore(kTestSES);
119 168
120 // Methods on the BudgetManager should only be allowed for secure origins. 169 // Methods on the BudgetManager should only be allowed for secure origins.
121 ASSERT_FALSE(ReserveBudget(type)); 170 ASSERT_FALSE(ReserveBudget(type));
122 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); 171 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_);
123 ASSERT_FALSE(ConsumeBudget(type)); 172 ASSERT_FALSE(ConsumeBudget(type));
124 } 173 }
125 174
126 TEST_F(BudgetManagerTest, TestUniqueOrigin) { 175 TEST_F(BudgetManagerTest, TestUniqueOrigin) {
127 const blink::mojom::BudgetOperationType type = 176 const blink::mojom::BudgetOperationType type =
128 blink::mojom::BudgetOperationType::SILENT_PUSH; 177 blink::mojom::BudgetOperationType::SILENT_PUSH;
129 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd"))); 178 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd")));
130 179
131 // Methods on the BudgetManager should not be allowed for unique origins. 180 // Methods on the BudgetManager should not be allowed for unique origins.
132 ASSERT_FALSE(ReserveBudget(type)); 181 ASSERT_FALSE(ReserveBudget(type));
133 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); 182 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_);
134 ASSERT_FALSE(ConsumeBudget(type)); 183 ASSERT_FALSE(ConsumeBudget(type));
135 } 184 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698