summaryrefslogtreecommitdiffstats
path: root/extensions/shell/browser/shell_prefs_unittest.cc
blob: 3f91f171a5042bdc6b3c547d4e442a2ee8a8f8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/shell/browser/shell_prefs.h"

#include "base/macros.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/common/extension_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {
namespace {

// A BrowserContext that uses a test data directory as its data path.
class PrefsTestBrowserContext : public content::TestBrowserContext {
 public:
  PrefsTestBrowserContext() {}
  ~PrefsTestBrowserContext() override {}

  // content::BrowserContext:
  base::FilePath GetPath() const override {
    base::FilePath path;
    PathService::Get(extensions::DIR_TEST_DATA, &path);
    return path.AppendASCII("shell_prefs");
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(PrefsTestBrowserContext);
};

class ShellPrefsTest : public testing::Test {
 public:
  ShellPrefsTest() {}
  ~ShellPrefsTest() override {}

 protected:
  content::TestBrowserThreadBundle thread_bundle_;
  PrefsTestBrowserContext browser_context_;
};

TEST_F(ShellPrefsTest, CreateLocalState) {
  scoped_ptr<PrefService> local_state =
      shell_prefs::CreateLocalState(browser_context_.GetPath());
  ASSERT_TRUE(local_state);

#if defined(OS_CHROMEOS)
  // Verify prefs were registered.
  EXPECT_TRUE(local_state->FindPreference("hardware.audio_output_enabled"));

  // Verify the test values were read.
  EXPECT_FALSE(local_state->GetBoolean("hardware.audio_output_enabled"));
#endif
}

TEST_F(ShellPrefsTest, CreateUserPrefService) {
  // Create the pref service. This loads the test pref file.
  scoped_ptr<PrefService> service =
      shell_prefs::CreateUserPrefService(&browser_context_);

  // Some basic extension preferences are registered.
  EXPECT_TRUE(service->FindPreference("extensions.settings"));
  EXPECT_TRUE(service->FindPreference("extensions.toolbarsize"));
  EXPECT_FALSE(service->FindPreference("should.not.exist"));

  // User prefs from the file have been read correctly.
  EXPECT_EQ("1.2.3.4", service->GetString("extensions.last_chrome_version"));
  EXPECT_EQ(123, service->GetInteger("extensions.toolbarsize"));

  // The user prefs system has been initialized.
  EXPECT_EQ(service.get(), user_prefs::UserPrefs::Get(&browser_context_));
}

}  // namespace
}  // namespace extensions