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 // Name of the directory where the budget database is stored on disk. | |
| 20 const char kDatabaseFileDir[] = "BudgetDatabase"; | |
| 21 | |
| 22 const double kDefaultDebt = -0.72; | |
| 23 const double kDefaultBudget1 = 1.234; | |
| 24 const double kDefaultBudget2 = 2.345; | |
| 25 const double kDefaultExpiration = 3600000000; | |
| 26 | |
| 27 using chrome_browser_budget_service::Budget; | |
| 28 using chrome_browser_budget_service::BudgetChunk; | |
| 29 | |
| 30 const char kTestOrigin[] = "https://example.com"; | |
| 31 | |
| 32 class BudgetDatabaseTest : public ::testing::Test { | |
| 33 public: | |
| 34 BudgetDatabaseTest() | |
| 35 : success_(false), | |
| 36 db_(profile_.GetPath().Append(FILE_PATH_LITERAL(kDatabaseFileDir)), | |
| 37 base::ThreadTaskRunnerHandle::Get()) {} | |
| 38 | |
| 39 void SetBudgetComplete(base::Closure run_loop_closure, bool success) { | |
| 40 success_ = success; | |
| 41 run_loop_closure.Run(); | |
| 42 } | |
| 43 | |
| 44 // Set up basic default values. | |
| 45 bool SetBudgetWithDefaultValues() { | |
| 46 Budget b; | |
| 47 b.set_debt(kDefaultDebt); | |
| 48 BudgetChunk* bc1 = b.add_budget(); | |
| 49 bc1->set_budget_amount(kDefaultBudget1); | |
| 50 bc1->set_expiration_timestamp(kDefaultExpiration); | |
| 51 BudgetChunk* bc2 = b.add_budget(); | |
| 52 bc2->set_budget_amount(kDefaultBudget2); | |
| 53 bc2->set_expiration_timestamp(kDefaultExpiration + 1); | |
| 54 | |
| 55 base::RunLoop run_loop; | |
| 56 db_.SetValue(GURL(kTestOrigin), b, | |
| 57 base::Bind(&BudgetDatabaseTest::SetBudgetComplete, | |
| 58 base::Unretained(this), run_loop.QuitClosure())); | |
| 59 run_loop.Run(); | |
| 60 | |
| 61 return success_; | |
| 62 } | |
| 63 | |
| 64 void GetBudgetComplete(base::Closure run_loop_closure, | |
| 65 bool success, | |
| 66 std::unique_ptr<Budget> budget) { | |
| 67 budget_ = std::move(budget); | |
| 68 success_ = success; | |
| 69 run_loop_closure.Run(); | |
| 70 } | |
| 71 | |
| 72 // Excercise the very basic get method. | |
| 73 std::unique_ptr<Budget> GetBudget() { | |
| 74 base::RunLoop run_loop; | |
| 75 db_.GetValue(GURL(kTestOrigin), | |
| 76 base::Bind(&BudgetDatabaseTest::GetBudgetComplete, | |
| 77 base::Unretained(this), run_loop.QuitClosure())); | |
| 78 run_loop.Run(); | |
| 79 return std::move(budget_); | |
| 80 } | |
| 81 | |
| 82 Profile* profile() { return &profile_; } | |
| 83 | |
| 84 content::TestBrowserThreadBundle thread_bundle_; | |
| 85 bool success_; | |
|
Peter Beverloo
2016/06/29 13:37:28
|thread_bundle_| should be private.
|success_| sho
harkness
2016/06/30 10:41:57
Done.
| |
| 86 | |
| 87 private: | |
| 88 std::unique_ptr<Budget> budget_; | |
| 89 TestingProfile profile_; | |
| 90 BudgetDatabase db_; | |
| 91 }; | |
| 92 | |
| 93 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | |
| 94 ASSERT_TRUE(SetBudgetWithDefaultValues()); | |
| 95 std::unique_ptr<Budget> b = GetBudget(); | |
| 96 | |
| 97 ASSERT_TRUE(success_); | |
| 98 EXPECT_EQ(kDefaultDebt, b->debt()); | |
| 99 EXPECT_EQ(kDefaultBudget1, b->budget(0).budget_amount()); | |
| 100 EXPECT_EQ(kDefaultBudget2, b->budget(1).budget_amount()); | |
| 101 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration_timestamp()); | |
| 102 } | |
|
Peter Beverloo
2016/06/29 13:37:28
What is your plan with this class? Will there be o
harkness
2016/06/30 10:41:57
I'm assuming there will be at least another 3-4 te
| |
| 103 | |
| 104 } // namespace | |
| OLD | NEW |