blob: 260b0007ffc5cc41b6d308fe5a929aaf344357d5 [file] [log] [blame]
Owen Min42947e82018-01-30 23:36:451// Copyright 2018 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 <string>
6
7#include "base/macros.h"
8#include "base/strings/string16.h"
9#include "base/strings/utf_string_conversions.h"
10#include "build/build_config.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/chrome_browser_main.h"
13#include "chrome/browser/chrome_browser_main_extra_parts.h"
14#include "chrome/common/pref_names.h"
15#include "chrome/test/base/in_process_browser_test.h"
16#include "components/policy/policy_constants.h"
17#include "components/prefs/pref_service.h"
18
19#if defined(OS_WIN)
20#include <windows.h>
21#include "base/test/test_reg_util_win.h"
22#include "base/win/registry.h"
23#endif
24
25namespace {
26
27#if defined(OS_WIN)
28const char kMockPolicyName[] = "AllowFileSelectionDialogs";
29#endif
30
31void VerifyLocalState() {
32 const PrefService* prefs = g_browser_process->local_state();
33 ASSERT_NE(nullptr, prefs);
34 const PrefService::Preference* pref =
35 prefs->FindPreference(prefs::kAllowFileSelectionDialogs);
36 ASSERT_NE(nullptr, pref);
37 EXPECT_FALSE(pref->IsDefaultValue());
38}
39
40class ChromeBrowserMainExtraPartsPolicyValueChecker
41 : public ChromeBrowserMainExtraParts {
42 public:
43 ChromeBrowserMainExtraPartsPolicyValueChecker() {}
44
45 // ChromeBrowserMainExtraParts
46 void PreCreateThreads() override { VerifyLocalState(); }
47 void PreBrowserStart() override { VerifyLocalState(); }
48 void PreMainMessageLoopRun() override { VerifyLocalState(); }
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainExtraPartsPolicyValueChecker);
52};
53
54} // namespace
55
56// Test if the policy value can be read from the pref properly on Windows.
57class PolicyInitializationBrowserTest : public InProcessBrowserTest {
58 protected:
59 PolicyInitializationBrowserTest() {}
60
61 // content::BrowserTestBase:
62 void SetUpInProcessBrowserTestFixture() override {
63 SetUpPlatformPolicyValue();
64 }
65 void CreatedBrowserMainParts(content::BrowserMainParts* parts) override {
66 static_cast<ChromeBrowserMainParts*>(parts)->AddParts(
67 new ChromeBrowserMainExtraPartsPolicyValueChecker());
68 }
69
70 private:
71#if defined(OS_WIN)
72 // Set up policy value for windows platform
73 void SetUpPlatformPolicyValue() {
74 HKEY root = HKEY_CURRENT_USER;
75 ASSERT_NO_FATAL_FAILURE(registry_override_manager_.OverrideRegistry(root));
76
77 base::win::RegKey key;
78
79 ASSERT_EQ(ERROR_SUCCESS, key.Create(root, policy::kRegistryChromePolicyKey,
80 KEY_SET_VALUE | KEY_WOW64_32KEY));
81 ASSERT_EQ(ERROR_SUCCESS,
82 key.WriteValue(base::ASCIIToUTF16(kMockPolicyName).c_str(), 1));
83 }
84
85 registry_util::RegistryOverrideManager registry_override_manager_;
86#else
87 // This test hasn't supported other platform yet.
88 void SetUpPlatformPolicyValue() {}
89#endif
90
91 DISALLOW_COPY_AND_ASSIGN(PolicyInitializationBrowserTest);
92};
93
94#if defined(OS_WIN)
95IN_PROC_BROWSER_TEST_F(PolicyInitializationBrowserTest, VerifyLocalState) {
96 VerifyLocalState();
97}
98#endif