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 "chrome/browser/budget_service/budget_database.h" | 5 #include "chrome/browser/budget_service/budget_database.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
9 #include "chrome/browser/budget_service/budget.pb.h" | 9 #include "chrome/browser/budget_service/budget.pb.h" |
10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 run_loop_closure.Run(); | 35 run_loop_closure.Run(); |
36 } | 36 } |
37 | 37 |
38 // Set up basic default values. | 38 // Set up basic default values. |
39 bool SetBudgetWithDefaultValues() { | 39 bool SetBudgetWithDefaultValues() { |
40 budget_service::Budget budget; | 40 budget_service::Budget budget; |
41 budget.set_debt(kDefaultDebt); | 41 budget.set_debt(kDefaultDebt); |
42 | 42 |
43 // Create two chunks and give them default values. | 43 // Create two chunks and give them default values. |
44 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); | 44 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); |
45 budget_chunk->set_budget_amount(kDefaultBudget1); | 45 budget_chunk->set_amount(kDefaultBudget1); |
46 budget_chunk->set_expiration_timestamp(kDefaultExpiration); | 46 budget_chunk->set_expiration(kDefaultExpiration); |
47 budget_chunk = budget.add_budget(); | 47 budget_chunk = budget.add_budget(); |
48 budget_chunk->set_budget_amount(kDefaultBudget2); | 48 budget_chunk->set_amount(kDefaultBudget2); |
49 budget_chunk->set_expiration_timestamp(kDefaultExpiration + 1); | 49 budget_chunk->set_expiration(kDefaultExpiration + 1); |
50 | 50 |
51 base::RunLoop run_loop; | 51 base::RunLoop run_loop; |
52 db_.SetValue(GURL(kTestOrigin), budget, | 52 db_.SetValue(GURL(kTestOrigin), budget, |
53 base::Bind(&BudgetDatabaseTest::SetBudgetComplete, | 53 base::Bind(&BudgetDatabaseTest::SetBudgetComplete, |
54 base::Unretained(this), run_loop.QuitClosure())); | 54 base::Unretained(this), run_loop.QuitClosure())); |
55 run_loop.Run(); | 55 run_loop.Run(); |
56 | 56 |
57 return success_; | 57 return success_; |
58 } | 58 } |
59 | 59 |
60 void GetBudgetComplete(base::Closure run_loop_closure, | 60 void GetBudgetComplete(base::Closure run_loop_closure, |
61 bool success, | 61 bool success, |
62 std::unique_ptr<budget_service::Budget> budget) { | 62 std::unique_ptr<budget_service::Budget> budget) { |
63 budget_ = std::move(budget); | 63 budget_ = std::move(budget); |
64 success_ = success; | 64 success_ = success; |
65 run_loop_closure.Run(); | 65 run_loop_closure.Run(); |
66 } | 66 } |
67 | 67 |
68 // Excercise the very basic get method. | 68 // Excercise the very basic get method. |
69 std::unique_ptr<budget_service::Budget> GetBudget() { | 69 std::unique_ptr<budget_service::Budget> GetBudget() { |
70 base::RunLoop run_loop; | 70 base::RunLoop run_loop; |
71 db_.GetValue(GURL(kTestOrigin), | 71 db_.GetValue(GURL(kTestOrigin), |
72 base::Bind(&BudgetDatabaseTest::GetBudgetComplete, | 72 base::Bind(&BudgetDatabaseTest::GetBudgetComplete, |
73 base::Unretained(this), run_loop.QuitClosure())); | 73 base::Unretained(this), run_loop.QuitClosure())); |
74 run_loop.Run(); | 74 run_loop.Run(); |
75 return std::move(budget_); | 75 return std::move(budget_); |
76 } | 76 } |
77 | 77 |
| 78 void GetBudgetDetailsComplete( |
| 79 base::Closure run_loop_closure, |
| 80 bool success, |
| 81 double debt, |
| 82 const BudgetDatabase::BudgetExpectation& expectation) { |
| 83 success_ = success; |
| 84 debt_ = debt; |
| 85 expectation_ = expectation; |
| 86 run_loop_closure.Run(); |
| 87 } |
| 88 |
| 89 void GetBudgetDetails() { |
| 90 base::RunLoop run_loop; |
| 91 db_.GetBudgetDetails( |
| 92 GURL(kTestOrigin), |
| 93 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, |
| 94 base::Unretained(this), run_loop.QuitClosure())); |
| 95 run_loop.Run(); |
| 96 } |
| 97 |
78 Profile* profile() { return &profile_; } | 98 Profile* profile() { return &profile_; } |
| 99 const BudgetDatabase::BudgetExpectation& expectation() { |
| 100 return expectation_; |
| 101 } |
79 | 102 |
80 protected: | 103 protected: |
81 bool success_; | 104 bool success_; |
82 | 105 |
83 private: | 106 private: |
84 content::TestBrowserThreadBundle thread_bundle_; | 107 content::TestBrowserThreadBundle thread_bundle_; |
85 std::unique_ptr<budget_service::Budget> budget_; | 108 std::unique_ptr<budget_service::Budget> budget_; |
86 TestingProfile profile_; | 109 TestingProfile profile_; |
87 BudgetDatabase db_; | 110 BudgetDatabase db_; |
| 111 double debt_ = 0; |
| 112 BudgetDatabase::BudgetExpectation expectation_; |
88 }; | 113 }; |
89 | 114 |
90 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | 115 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { |
91 ASSERT_TRUE(SetBudgetWithDefaultValues()); | 116 ASSERT_TRUE(SetBudgetWithDefaultValues()); |
92 std::unique_ptr<budget_service::Budget> b = GetBudget(); | 117 std::unique_ptr<budget_service::Budget> b = GetBudget(); |
93 | 118 |
94 ASSERT_TRUE(success_); | 119 ASSERT_TRUE(success_); |
95 EXPECT_EQ(kDefaultDebt, b->debt()); | 120 EXPECT_EQ(kDefaultDebt, b->debt()); |
96 EXPECT_EQ(kDefaultBudget1, b->budget(0).budget_amount()); | 121 EXPECT_EQ(kDefaultBudget1, b->budget(0).amount()); |
97 EXPECT_EQ(kDefaultBudget2, b->budget(1).budget_amount()); | 122 EXPECT_EQ(kDefaultBudget2, b->budget(1).amount()); |
98 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration_timestamp()); | 123 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration()); |
| 124 } |
| 125 |
| 126 TEST_F(BudgetDatabaseTest, BudgetDetailsTest) { |
| 127 ASSERT_TRUE(SetBudgetWithDefaultValues()); |
| 128 GetBudgetDetails(); |
| 129 |
| 130 // Get the expectation and validate it. |
| 131 const auto& expected_value = expectation(); |
| 132 ASSERT_TRUE(success_); |
| 133 ASSERT_EQ(3U, expected_value.size()); |
| 134 |
| 135 // Make sure that the correct data is returned. |
| 136 auto iter = expected_value.begin(); |
| 137 |
| 138 // First value should be [total_budget, now] |
| 139 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); |
| 140 // TODO(harkness): This will be "now" in the final version. For now, it's |
| 141 // just 0.0. |
| 142 EXPECT_EQ(0, iter->second); |
| 143 |
| 144 // The next value should be the budget after the first chunk expires. |
| 145 iter++; |
| 146 EXPECT_EQ(kDefaultBudget2, iter->first); |
| 147 EXPECT_EQ(kDefaultExpiration, iter->second); |
| 148 |
| 149 // The final value gives the budget of 0.0 after the second chunk expires. |
| 150 iter++; |
| 151 EXPECT_EQ(0, iter->first); |
| 152 EXPECT_EQ(kDefaultExpiration + 1, iter->second); |
99 } | 153 } |
100 | 154 |
101 } // namespace | 155 } // namespace |
OLD | NEW |