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 const char kTestOrigin[] = "https://example.com"; | |
| 28 | |
| 29 class BudgetDatabaseTest : public ::testing::Test { | |
| 30 public: | |
| 31 BudgetDatabaseTest() | |
| 32 : success_(false), | |
| 33 db_(profile_.GetPath().Append(FILE_PATH_LITERAL(kDatabaseFileDir)), | |
|
Peter Beverloo
2016/06/30 13:12:46
A constant is not a literal :-). You use FILE_PATH
harkness
2016/07/01 12:29:54
hah! That's what I get for trying to clean things
| |
| 34 base::ThreadTaskRunnerHandle::Get()) {} | |
| 35 | |
| 36 void SetBudgetComplete(base::Closure run_loop_closure, bool success) { | |
| 37 success_ = success; | |
| 38 run_loop_closure.Run(); | |
| 39 } | |
| 40 | |
| 41 // Set up basic default values. | |
| 42 bool SetBudgetWithDefaultValues() { | |
| 43 budget_service::Budget b; | |
|
Peter Beverloo
2016/06/30 13:12:46
nit re: b, bc1, bc2: please don't use such acronym
harkness
2016/07/01 12:29:54
Done.
| |
| 44 b.set_debt(kDefaultDebt); | |
| 45 budget_service::BudgetChunk* bc1 = b.add_budget(); | |
| 46 bc1->set_budget_amount(kDefaultBudget1); | |
| 47 bc1->set_expiration_timestamp(kDefaultExpiration); | |
| 48 budget_service::BudgetChunk* bc2 = b.add_budget(); | |
| 49 bc2->set_budget_amount(kDefaultBudget2); | |
| 50 bc2->set_expiration_timestamp(kDefaultExpiration + 1); | |
| 51 | |
| 52 base::RunLoop run_loop; | |
| 53 db_.SetValue(GURL(kTestOrigin), b, | |
| 54 base::Bind(&BudgetDatabaseTest::SetBudgetComplete, | |
| 55 base::Unretained(this), run_loop.QuitClosure())); | |
| 56 run_loop.Run(); | |
| 57 | |
| 58 return success_; | |
| 59 } | |
| 60 | |
| 61 void GetBudgetComplete(base::Closure run_loop_closure, | |
| 62 bool success, | |
| 63 std::unique_ptr<budget_service::Budget> budget) { | |
| 64 budget_ = std::move(budget); | |
| 65 success_ = success; | |
| 66 run_loop_closure.Run(); | |
| 67 } | |
| 68 | |
| 69 // Excercise the very basic get method. | |
| 70 std::unique_ptr<budget_service::Budget> GetBudget() { | |
| 71 base::RunLoop run_loop; | |
| 72 db_.GetValue(GURL(kTestOrigin), | |
| 73 base::Bind(&BudgetDatabaseTest::GetBudgetComplete, | |
| 74 base::Unretained(this), run_loop.QuitClosure())); | |
| 75 run_loop.Run(); | |
| 76 return std::move(budget_); | |
| 77 } | |
| 78 | |
| 79 Profile* profile() { return &profile_; } | |
| 80 | |
| 81 protected: | |
| 82 bool success_; | |
| 83 | |
| 84 private: | |
| 85 content::TestBrowserThreadBundle thread_bundle_; | |
| 86 std::unique_ptr<budget_service::Budget> budget_; | |
| 87 TestingProfile profile_; | |
| 88 BudgetDatabase db_; | |
| 89 }; | |
| 90 | |
| 91 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | |
| 92 ASSERT_TRUE(SetBudgetWithDefaultValues()); | |
| 93 std::unique_ptr<budget_service::Budget> b = GetBudget(); | |
| 94 | |
| 95 ASSERT_TRUE(success_); | |
| 96 EXPECT_EQ(kDefaultDebt, b->debt()); | |
| 97 EXPECT_EQ(kDefaultBudget1, b->budget(0).budget_amount()); | |
| 98 EXPECT_EQ(kDefaultBudget2, b->budget(1).budget_amount()); | |
| 99 EXPECT_EQ(kDefaultExpiration, b->budget(0).expiration_timestamp()); | |
| 100 } | |
| 101 | |
| 102 } // namespace | |
| OLD | NEW |