summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/user_policy_test_helper.h
blob: 941a0674c3cdd5c715bbcc36d1229789a3ada223 (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
// Copyright 2015 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.

#ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_TEST_HELPER_H_
#define CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_TEST_HELPER_H_

#include <string>

#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"

class Profile;

namespace base {
class CommandLine;
class FilePath;
class DictionaryValue;
}

namespace policy {

class LocalPolicyTestServer;

// This class can be used to apply a user policy to the profile in a
// BrowserTest.
class UserPolicyTestHelper {
 public:
  explicit UserPolicyTestHelper(const std::string& account_id);
  virtual ~UserPolicyTestHelper();

  // Must be called after construction to start the policy test server.
  void Init(const base::DictionaryValue& mandatory_policy,
            const base::DictionaryValue& recommended_policy);

  // Must be used during BrowserTestBase::SetUpCommandLine to direct Chrome to
  // the policy test server.
  void UpdateCommandLine(base::CommandLine* command_line) const;

  // Can be optionally used to wait for the initial policy to be applied to the
  // profile. Alternatively, a login can be simulated, which makes it
  // unnecessary to call this function.
  void WaitForInitialPolicy(Profile* profile);

  // Update the policy test server with the given policy. Then refresh and wait
  // for the new policy being applied to |profile|.
  void UpdatePolicy(const base::DictionaryValue& mandatory_policy,
                    const base::DictionaryValue& recommended_policy,
                    Profile* profile);

 private:
  void WritePolicyFile(const base::DictionaryValue& mandatory,
                       const base::DictionaryValue& recommended);
  base::FilePath PolicyFilePath() const;

  const std::string account_id_;
  base::ScopedTempDir temp_dir_;
  scoped_ptr<LocalPolicyTestServer> test_server_;

  DISALLOW_COPY_AND_ASSIGN(UserPolicyTestHelper);
};

}  // namespace policy

#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_TEST_HELPER_H_