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 "chrome/browser/budget_service/budget_manager.h" | 10 #include "chrome/browser/budget_service/budget_manager.h" |
11 #include "chrome/browser/budget_service/budget_manager_factory.h" | 11 #include "chrome/browser/budget_service/budget_manager_factory.h" |
12 #include "chrome/browser/engagement/site_engagement_service.h" | 12 #include "chrome/browser/engagement/site_engagement_service.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
16 #include "components/prefs/scoped_user_pref_update.h" | 16 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" | 19 #include "third_party/WebKit/public/platform/modules/budget_service/budget_servi
ce.mojom.h" |
| 20 #include "url/origin.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kTestOrigin[] = "https://example.com"; | 24 const char kTestOrigin[] = "https://example.com"; |
24 const double kTestSES = 48.0; | 25 const double kTestSES = 48.0; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 class BudgetManagerTest : public testing::Test { | 29 class BudgetManagerTest : public testing::Test { |
29 public: | 30 public: |
| 31 BudgetManagerTest() : origin_(url::Origin(GURL(kTestOrigin))) {} |
30 ~BudgetManagerTest() override {} | 32 ~BudgetManagerTest() override {} |
31 | 33 |
32 BudgetManager* GetManager() { | 34 BudgetManager* GetManager() { |
33 return BudgetManagerFactory::GetForProfile(&profile_); | 35 return BudgetManagerFactory::GetForProfile(&profile_); |
34 } | 36 } |
35 | 37 |
36 void SetSiteEngagementScore(const GURL& url, double score) { | 38 void SetSiteEngagementScore(double score) { |
37 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 39 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
38 service->ResetScoreForURL(url, score); | 40 service->ResetScoreForURL(GURL(kTestOrigin), score); |
39 } | 41 } |
40 | 42 |
41 Profile* profile() { return &profile_; } | 43 Profile* profile() { return &profile_; } |
| 44 const url::Origin origin() const { return origin_; } |
42 | 45 |
43 void StatusCallback(base::Closure run_loop_closure, bool success) { | 46 void StatusCallback(base::Closure run_loop_closure, bool success) { |
44 success_ = success; | 47 success_ = success; |
45 run_loop_closure.Run(); | 48 run_loop_closure.Run(); |
46 } | 49 } |
47 | 50 |
48 bool ReserveBudget(blink::mojom::BudgetOperationType type) { | 51 bool ReserveBudget(blink::mojom::BudgetOperationType type) { |
49 const GURL origin(kTestOrigin); | |
50 base::RunLoop run_loop; | 52 base::RunLoop run_loop; |
51 GetManager()->Reserve( | 53 GetManager()->Reserve( |
52 origin, type, | 54 origin(), type, |
53 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), | 55 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), |
54 run_loop.QuitClosure())); | 56 run_loop.QuitClosure())); |
55 run_loop.Run(); | 57 run_loop.Run(); |
56 return success_; | 58 return success_; |
57 } | 59 } |
58 | 60 |
59 bool ConsumeBudget(blink::mojom::BudgetOperationType type) { | 61 bool ConsumeBudget(blink::mojom::BudgetOperationType type) { |
60 const GURL origin(kTestOrigin); | |
61 base::RunLoop run_loop; | 62 base::RunLoop run_loop; |
62 GetManager()->Consume( | 63 GetManager()->Consume( |
63 origin, type, | 64 origin(), type, |
64 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), | 65 base::Bind(&BudgetManagerTest::StatusCallback, base::Unretained(this), |
65 run_loop.QuitClosure())); | 66 run_loop.QuitClosure())); |
66 run_loop.Run(); | 67 run_loop.Run(); |
67 return success_; | 68 return success_; |
68 } | 69 } |
69 | 70 |
70 // Members for callbacks to set. | 71 // Members for callbacks to set. |
71 bool success_; | 72 bool success_; |
72 | 73 |
73 private: | 74 private: |
74 content::TestBrowserThreadBundle thread_bundle_; | 75 content::TestBrowserThreadBundle thread_bundle_; |
75 TestingProfile profile_; | 76 TestingProfile profile_; |
| 77 const url::Origin origin_; |
76 }; | 78 }; |
77 | 79 |
78 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { | 80 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { |
79 // Set initial SES. The first time we try to spend budget, the | 81 // Set initial SES. The first time we try to spend budget, the |
80 // engagement award will be granted which is 48.0. | 82 // engagement award will be granted which is 48.0. |
81 const GURL origin(kTestOrigin); | 83 SetSiteEngagementScore(kTestSES); |
82 SetSiteEngagementScore(origin, kTestSES); | |
83 const blink::mojom::BudgetOperationType type = | 84 const blink::mojom::BudgetOperationType type = |
84 blink::mojom::BudgetOperationType::SILENT_PUSH; | 85 blink::mojom::BudgetOperationType::SILENT_PUSH; |
85 | 86 |
86 // Spend for 24 silent push messages. This should consume all the original | 87 // Spend for 24 silent push messages. This should consume all the original |
87 // budget grant. | 88 // budget grant. |
88 for (int i = 0; i < 24; i++) | 89 for (int i = 0; i < 24; i++) |
89 ASSERT_TRUE(ReserveBudget(type)); | 90 ASSERT_TRUE(ReserveBudget(type)); |
90 | 91 |
91 // Try to send one final silent push. The origin should be out of budget. | 92 // Try to send one final silent push. The origin should be out of budget. |
92 ASSERT_FALSE(ReserveBudget(type)); | 93 ASSERT_FALSE(ReserveBudget(type)); |
93 | 94 |
94 // Try to consume for the 24 messages reserved. | 95 // Try to consume for the 24 messages reserved. |
95 for (int i = 0; i < 24; i++) | 96 for (int i = 0; i < 24; i++) |
96 ASSERT_TRUE(ConsumeBudget(type)); | 97 ASSERT_TRUE(ConsumeBudget(type)); |
97 | 98 |
98 // The next consume should fail, since there is no reservation or budget | 99 // The next consume should fail, since there is no reservation or budget |
99 // available. | 100 // available. |
100 ASSERT_FALSE(ConsumeBudget(type)); | 101 ASSERT_FALSE(ConsumeBudget(type)); |
101 } | 102 } |
OLD | NEW |