Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/budget_service/budget_database.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 #include "chrome/browser/budget_service/budget.pb.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | |
| 11 #include "components/leveldb_proto/proto_database.h" | |
| 12 #include "components/leveldb_proto/proto_database_impl.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const double kDefaultDebt = -0.72; | |
| 20 const double kDefaultBudget1 = 1.234; | |
| 21 const double kDefaultBudget2 = 2.345; | |
| 22 const double kDefaultExpiration = 3600000000; | |
| 23 | |
| 24 const char kTestOrigin[] = "https://example.com"; | |
| 25 | |
| 26 class BudgetDatabaseTest : public ::testing::Test { | |
| 27 public: | |
| 28 BudgetDatabaseTest() | |
| 29 : success_(false), | |
| 30 db_(profile_.GetPath().Append("BudgetDabase"), | |
|
Peter Beverloo
2016/07/05 13:01:48
db_(profile_.GetPath().Append(FILE_PATH_LITERAL("B
harkness
2016/07/06 13:42:37
ahhh! Thanks for the explanation!
| |
| 31 base::ThreadTaskRunnerHandle::Get()) {} | |
| 32 | |
| 33 void SetBudgetComplete(base::Closure run_loop_closure, bool success) { | |
| 34 success_ = success; | |
| 35 run_loop_closure.Run(); | |
| 36 } | |
| 37 | |
| 38 // Set up basic default values. | |
| 39 bool SetBudgetWithDefaultValues() { | |
| 40 budget_service::Budget budget; | |
| 41 budget.set_debt(kDefaultDebt); | |
| 42 | |
| 43 // Create two chunks and give them default values. | |
| 44 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); | |
| 45 budget_chunk->set_budget_amount(kDefaultBudget1); | |
| 46 budget_chunk->set_expiration_timestamp(kDefaultExpiration); | |
| 47 budget_chunk = budget.add_budget(); | |
| 48 budget_chunk->set_budget_amount(kDefaultBudget2); | |
| 49 budget_chunk->set_expiration_timestamp(kDefaultExpiration + 1); | |
| 50 | |
| 51 base::RunLoop run_loop; | |
| 52 db_.SetValue(GURL(kTestOrigin), budget, | |
| 53 base::Bind(&BudgetDatabaseTest::SetBudgetComplete, | |
| 54 base::Unretained(this), run_loop.QuitClosure())); | |
| 55 run_loop.Run(); | |
| 56 | |
| 57 return success_; | |
| 58 } | |
| 59 | |
| 60 void GetBudgetComplete(base::Closure run_loop_closure, | |
| 61 bool success, | |
| 62 std::unique_ptr<budget_service::Budget> budget) { | |
| 63 budget_ = std::move(budget); | |
| 64 success_ = success; | |
| 65 run_loop_closure.Run(); | |
| 66 } | |
| 67 | |
| 68 // Excercise the very basic get method. | |
| 69 std::unique_ptr<budget_service::Budget> GetBudget() { | |
| 70 base::RunLoop run_loop; | |
| 71 db_.GetValue(GURL(kTestOrigin), | |
| 72 base::Bind(&BudgetDatabaseTest::GetBudgetComplete, | |
| 73 base::Unretained(this), run_loop.QuitClosure())); | |
| 74 run_loop.Run(); | |
| 75 return std::move(budget_); | |
| 76 } | |
| 77 | |
| 78 Profile* profile() { return &profile_; } | |
| 79 | |
| 80 protected: | |
| 81 bool success_; | |
| 82 | |
| 83 private: | |
| 84 content::TestBrowserThreadBundle thread_bundle_; | |
| 85 std::unique_ptr<budget_service::Budget> budget_; | |
| 86 TestingProfile profile_; | |
| 87 BudgetDatabase db_; | |
| 88 }; | |
| 89 | |
| 90 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | |
| 91 ASSERT_TRUE(SetBudgetWithDefaultValues()); | |
| 92 std::unique_ptr<budget_service::Budget> b = GetBudget(); | |
| 93 | |
| 94 ASSERT_TRUE(success_); | |
| 95 EXPECT_EQ(kDefaultDebt, b->debt()); | |
| 96 EXPECT_EQ(kDefaultBudget1, b->budget(0).budget_amount()); | |
| 97 EXPECT_EQ(kDefaultBudget2, b->budget(1).budget_amount()); | |
| 98 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration_timestamp()); | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| OLD | NEW |