prefs_unittest.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <memory>
  5. #include "base/run_loop.h"
  6. #include "base/task/thread_pool.h"
  7. #include "base/test/bind.h"
  8. #include "base/test/task_environment.h"
  9. #include "build/build_config.h"
  10. #include "chrome/updater/persisted_data.h"
  11. #include "chrome/updater/prefs.h"
  12. #include "chrome/updater/prefs_impl.h"
  13. #include "chrome/updater/registration_data.h"
  14. #include "chrome/updater/updater_scope.h"
  15. #include "components/prefs/testing_pref_service.h"
  16. #include "components/update_client/update_client.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace updater {
  19. TEST(PrefsTest, PrefsCommitPendingWrites) {
  20. base::test::TaskEnvironment task_environment;
  21. auto pref = std::make_unique<TestingPrefServiceSimple>();
  22. update_client::RegisterPrefs(pref->registry());
  23. auto metadata = base::MakeRefCounted<PersistedData>(pref.get());
  24. // Writes something to prefs.
  25. metadata->SetBrandCode("someappid", "brand");
  26. EXPECT_STREQ(metadata->GetBrandCode("someappid").c_str(), "brand");
  27. // Tests writing to storage completes.
  28. PrefsCommitPendingWrites(pref.get());
  29. }
  30. TEST(PrefsTest, AcquireGlobalPrefsLock_LockThenTryLockInThreadFail) {
  31. base::test::TaskEnvironment task_environment;
  32. std::unique_ptr<ScopedPrefsLock> lock =
  33. AcquireGlobalPrefsLock(GetUpdaterScope(), base::Seconds(0));
  34. EXPECT_TRUE(lock);
  35. base::RunLoop run_loop;
  36. base::ThreadPool::PostTaskAndReplyWithResult(
  37. FROM_HERE, base::BindOnce([]() {
  38. std::unique_ptr<ScopedPrefsLock> lock =
  39. AcquireGlobalPrefsLock(GetUpdaterScope(), base::Seconds(0));
  40. return lock.get() != nullptr;
  41. }),
  42. base::OnceCallback<void(bool)>(
  43. base::BindLambdaForTesting([&run_loop](bool acquired_lock) {
  44. EXPECT_FALSE(acquired_lock);
  45. run_loop.Quit();
  46. })));
  47. run_loop.Run();
  48. }
  49. TEST(PrefsTest, AcquireGlobalPrefsLock_TryLockInThreadSuccess) {
  50. base::test::TaskEnvironment task_environment;
  51. base::RunLoop run_loop;
  52. base::ThreadPool::PostTaskAndReplyWithResult(
  53. FROM_HERE, base::BindOnce([]() {
  54. std::unique_ptr<ScopedPrefsLock> lock =
  55. AcquireGlobalPrefsLock(GetUpdaterScope(), base::Seconds(0));
  56. return lock.get() != nullptr;
  57. }),
  58. base::OnceCallback<void(bool)>(
  59. base::BindLambdaForTesting([&run_loop](bool acquired_lock) {
  60. EXPECT_TRUE(acquired_lock);
  61. run_loop.Quit();
  62. })));
  63. run_loop.Run();
  64. auto lock = AcquireGlobalPrefsLock(GetUpdaterScope(), base::Seconds(0));
  65. EXPECT_TRUE(lock);
  66. }
  67. } // namespace updater