Chromium Code Reviews| 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 return; | |
|
Peter Beverloo
2016/07/26 17:26:21
nit: drop (control flow ends after this anyway)
harkness
2016/07/27 10:59:03
Done.
| |
| 97 } | |
| 98 | |
| 78 Profile* profile() { return &profile_; } | 99 Profile* profile() { return &profile_; } |
| 100 const BudgetDatabase::BudgetExpectation& expectation() { | |
| 101 return expectation_; | |
| 102 } | |
| 79 | 103 |
| 80 protected: | 104 protected: |
| 81 bool success_; | 105 bool success_; |
| 82 | 106 |
| 83 private: | 107 private: |
| 84 content::TestBrowserThreadBundle thread_bundle_; | 108 content::TestBrowserThreadBundle thread_bundle_; |
| 85 std::unique_ptr<budget_service::Budget> budget_; | 109 std::unique_ptr<budget_service::Budget> budget_; |
| 86 TestingProfile profile_; | 110 TestingProfile profile_; |
| 87 BudgetDatabase db_; | 111 BudgetDatabase db_; |
| 112 double debt_; | |
|
Peter Beverloo
2016/07/26 17:26:21
init (double debt_ = 0;)
harkness
2016/07/27 10:59:03
Done.
| |
| 113 BudgetDatabase::BudgetExpectation expectation_; | |
| 88 }; | 114 }; |
| 89 | 115 |
| 90 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | 116 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { |
| 91 ASSERT_TRUE(SetBudgetWithDefaultValues()); | 117 ASSERT_TRUE(SetBudgetWithDefaultValues()); |
| 92 std::unique_ptr<budget_service::Budget> b = GetBudget(); | 118 std::unique_ptr<budget_service::Budget> b = GetBudget(); |
| 93 | 119 |
| 94 ASSERT_TRUE(success_); | 120 ASSERT_TRUE(success_); |
| 95 EXPECT_EQ(kDefaultDebt, b->debt()); | 121 EXPECT_EQ(kDefaultDebt, b->debt()); |
| 96 EXPECT_EQ(kDefaultBudget1, b->budget(0).budget_amount()); | 122 EXPECT_EQ(kDefaultBudget1, b->budget(0).amount()); |
| 97 EXPECT_EQ(kDefaultBudget2, b->budget(1).budget_amount()); | 123 EXPECT_EQ(kDefaultBudget2, b->budget(1).amount()); |
| 98 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration_timestamp()); | 124 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration()); |
| 125 } | |
| 126 | |
| 127 TEST_F(BudgetDatabaseTest, BudgetDetailsTest) { | |
| 128 ASSERT_TRUE(SetBudgetWithDefaultValues()); | |
| 129 GetBudgetDetails(); | |
| 130 | |
| 131 // Get the expectation and validate it. | |
| 132 const BudgetDatabase::BudgetExpectation exp = expectation(); | |
|
Peter Beverloo
2016/07/26 17:26:21
nit 2: perfectly fine to use const auto& here. Try
Peter Beverloo
2016/07/26 17:26:21
nit 1: expectation() returns a reference, so no ne
harkness
2016/07/27 10:59:03
Done.
harkness
2016/07/27 10:59:03
Done.
| |
| 133 ASSERT_TRUE(success_); | |
| 134 | |
| 135 // Make sure that the correct data is returned. | |
| 136 BudgetDatabase::BudgetExpectation::const_iterator iter = exp.begin(); | |
|
Peter Beverloo
2016/07/26 17:26:21
nit: const auto& iter = ...?
harkness
2016/07/27 10:59:03
I did some reading on const_iterator and auto, and
Peter Beverloo
2016/07/27 11:21:40
Ugh ok. Could still use `auto` (const-ness doesn't
| |
| 137 ASSERT_EQ(exp.size(), (const unsigned long)3); | |
|
Peter Beverloo
2016/07/26 17:26:21
nit 1: This ASSERT should be under line 133, it's
Peter Beverloo
2016/07/26 17:26:21
nit 3: We disallow c-style casts in Chromium. In t
Peter Beverloo
2016/07/26 17:26:21
nit 2: Canonical use of the {ASSERT,EXPECT}_EQ mac
harkness
2016/07/27 10:59:03
Done.
harkness
2016/07/27 10:59:03
Done.
harkness
2016/07/27 10:59:03
Done.
| |
| 138 | |
| 139 // First value should be [total_budget, now] | |
| 140 EXPECT_EQ(iter->first, kDefaultBudget1 + kDefaultBudget2); | |
| 141 // TODO(harkness): This will be "now" in the final version. For now, it's | |
| 142 // just 0.0. | |
| 143 EXPECT_EQ(iter->second, 0.0); | |
|
Peter Beverloo
2016/07/26 17:26:21
nit: s/0.0/0/ (dito on line 152)
harkness
2016/07/27 10:59:03
Done.
| |
| 144 | |
| 145 // The next value should be the budget after the first chunk expires. | |
| 146 iter++; | |
| 147 EXPECT_EQ(iter->first, kDefaultBudget2); | |
| 148 EXPECT_EQ(iter->second, kDefaultExpiration); | |
| 149 | |
| 150 // The final value gives the budget of 0.0 after the second chunk expires. | |
| 151 iter++; | |
| 152 EXPECT_EQ(iter->first, 0.0); | |
| 153 EXPECT_EQ(iter->second, kDefaultExpiration + 1); | |
| 99 } | 154 } |
| 100 | 155 |
| 101 } // namespace | 156 } // namespace |
| OLD | NEW |