File: mock_configuration_policy_provider.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 4,185 bytes parent folder | download | duplicates (11)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POLICY_CORE_COMMON_MOCK_CONFIGURATION_POLICY_PROVIDER_H_
#define COMPONENTS_POLICY_CORE_COMMON_MOCK_CONFIGURATION_POLICY_PROVIDER_H_

#include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/core/common/schema_registry.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace policy {

// Mock ConfigurationPolicyProvider implementation that supplies canned
// values for polices.
class MockConfigurationPolicyProvider : public ConfigurationPolicyProvider,
                                        public policy::PolicyService::Observer {
 public:
  MockConfigurationPolicyProvider();
  MockConfigurationPolicyProvider(const MockConfigurationPolicyProvider&) =
      delete;
  MockConfigurationPolicyProvider& operator=(
      const MockConfigurationPolicyProvider&) = delete;
  ~MockConfigurationPolicyProvider() override;

  MOCK_CONST_METHOD1(IsInitializationComplete, bool(PolicyDomain domain));
  MOCK_CONST_METHOD1(IsFirstPolicyLoadComplete, bool(PolicyDomain domain));
  MOCK_METHOD1(RefreshPolicies, void(PolicyFetchReason reason));

  // Use this for a more accurate policy update events. Not using this may
  // may result in flaky tests where we expect an event to be propagated and
  // it is not.
  void SetupPolicyServiceForPolicyUpdates(
      policy::PolicyService* policy_service);

  // Make public for tests.
  using ConfigurationPolicyProvider::UpdatePolicy;

  // Utility method that invokes UpdatePolicy() with a PolicyBundle that maps
  // the Chrome namespace to a copy of |policy|.
  // Note: Replaces the PolicyBundle, so any policy that has been set previously
  // will be lost when calling this utility method.
  void UpdateChromePolicy(const PolicyMap& policy);

  // Utility method that invokes UpdatePolicy() with a PolicyBundle that maps
  // the extension's |extension_id| namespace to a copy of |policy|.
  // Note: Replaces the PolicyBundle, so any policy that has been set previously
  // will be lost when calling this utility method.
  void UpdateExtensionPolicy(const PolicyMap& policy,
                             const std::string& extension_id);

  // Convenience method so that tests don't need to create a registry to create
  // this mock.
  using ConfigurationPolicyProvider::Init;
  void Init() {
    ConfigurationPolicyProvider::Init(&registry_);
  }

  // Utility testing method used to set up boilerplate |ON_CALL| defaults.
  void SetDefaultReturns(bool is_initialization_complete_return,
                         bool is_first_policy_load_complete_return) {
    ON_CALL(*this, IsInitializationComplete(testing::_))
        .WillByDefault(testing::Return(is_initialization_complete_return));
    ON_CALL(*this, IsFirstPolicyLoadComplete(testing::_))
        .WillByDefault(testing::Return(is_first_policy_load_complete_return));
  }

  // Convenience method that installs an expectation on RefreshPolicies that
  // just notifies the observers and serves the same policies.
  void SetAutoRefresh();

  void RefreshWithSamePolicies();

  // policy::PolicyService::Observer
  void OnPolicyUpdated(const policy::PolicyNamespace& ns,
                       const policy::PolicyMap& previous,
                       const policy::PolicyMap& current) override;

 private:
  void WaitForPoliciesUpdated(policy::PolicyDomain domain);

  SchemaRegistry registry_;
  raw_ptr<policy::PolicyService> policy_service_ = nullptr;
  base::OnceCallback<void()> extension_policies_updated_callback_;
  base::OnceCallback<void()> chrome_policies_updated_callback_;
};

class MockConfigurationPolicyObserver
    : public ConfigurationPolicyProvider::Observer {
 public:
  MockConfigurationPolicyObserver();
  ~MockConfigurationPolicyObserver() override;

  MOCK_METHOD1(OnUpdatePolicy, void(ConfigurationPolicyProvider*));
};

}  // namespace policy

#endif  // COMPONENTS_POLICY_CORE_COMMON_MOCK_CONFIGURATION_POLICY_PROVIDER_H_