shell_prefs_unittest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2014 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 "extensions/shell/browser/shell_prefs.h"
  5. #include "base/path_service.h"
  6. #include "build/build_config.h"
  7. #include "build/chromeos_buildflags.h"
  8. #include "components/prefs/pref_service.h"
  9. #include "components/user_prefs/user_prefs.h"
  10. #include "content/public/test/browser_task_environment.h"
  11. #include "content/public/test/test_browser_context.h"
  12. #include "extensions/common/extension_paths.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace extensions {
  15. namespace {
  16. // A BrowserContext that uses a test data directory as its data path.
  17. class PrefsTestBrowserContext : public content::TestBrowserContext {
  18. public:
  19. PrefsTestBrowserContext() {}
  20. PrefsTestBrowserContext(const PrefsTestBrowserContext&) = delete;
  21. PrefsTestBrowserContext& operator=(const PrefsTestBrowserContext&) = delete;
  22. ~PrefsTestBrowserContext() override {}
  23. // content::BrowserContext:
  24. base::FilePath GetPath() override {
  25. base::FilePath path;
  26. base::PathService::Get(extensions::DIR_TEST_DATA, &path);
  27. return path.AppendASCII("shell_prefs");
  28. }
  29. };
  30. class ShellPrefsTest : public testing::Test {
  31. public:
  32. ShellPrefsTest() {}
  33. ~ShellPrefsTest() override {}
  34. protected:
  35. content::BrowserTaskEnvironment task_environment_;
  36. PrefsTestBrowserContext browser_context_;
  37. };
  38. TEST_F(ShellPrefsTest, CreateLocalState) {
  39. std::unique_ptr<PrefService> local_state =
  40. shell_prefs::CreateLocalState(browser_context_.GetPath());
  41. ASSERT_TRUE(local_state);
  42. #if BUILDFLAG(IS_CHROMEOS_ASH)
  43. // Verify prefs were registered.
  44. EXPECT_TRUE(local_state->FindPreference("hardware.audio_output_enabled"));
  45. // Verify the test values were read.
  46. EXPECT_FALSE(local_state->GetBoolean("hardware.audio_output_enabled"));
  47. #endif
  48. }
  49. TEST_F(ShellPrefsTest, CreateUserPrefService) {
  50. // Create the pref service. This loads the test pref file.
  51. std::unique_ptr<PrefService> service =
  52. shell_prefs::CreateUserPrefService(&browser_context_);
  53. // Some basic extension preferences are registered.
  54. EXPECT_TRUE(service->FindPreference("extensions.settings"));
  55. EXPECT_FALSE(service->FindPreference("should.not.exist"));
  56. // User prefs from the file have been read correctly.
  57. EXPECT_EQ("1.2.3.4", service->GetString("extensions.last_chrome_version"));
  58. // The user prefs system has been initialized.
  59. EXPECT_EQ(service.get(), user_prefs::UserPrefs::Get(&browser_context_));
  60. }
  61. } // namespace
  62. } // namespace extensions