File: browser_switcher_prefs_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,181 bytes parent folder | download | duplicates (4)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "chrome/browser/browser_switcher/browser_switcher_sitelist.h"

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

#include "base/run_loop.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_switcher/browser_switcher_prefs.h"
#include "chrome/browser/browser_switcher/ieem_sitelist_parser.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

using testing::_;
using testing::Invoke;
using testing::Return;

namespace browser_switcher {

namespace {

class TestBrowserSwitcherPrefs : public BrowserSwitcherPrefs {
 public:
  TestBrowserSwitcherPrefs(PrefService* prefs,
                           policy::PolicyService* policy_service)
      : BrowserSwitcherPrefs(prefs, policy_service) {}
};

base::Value StringArrayToValue(const std::vector<const char*>& strings) {
  base::Value::List list;
  for (const auto* string : strings)
    list.Append(string);
  return base::Value(std::move(list));
}

}  // namespace

class BrowserSwitcherPrefsTest : public testing::Test {
 public:
  void SetUp() override {
    BrowserSwitcherPrefs::RegisterProfilePrefs(prefs_backend_.registry());
    policy_provider_ = std::make_unique<
        testing::NiceMock<policy::MockConfigurationPolicyProvider>>();
    policy_provider_->SetDefaultReturns(
        /*is_initialization_complete_return=*/true,
        /*is_first_policy_load_complete_return=*/true);
    std::vector<
        raw_ptr<policy::ConfigurationPolicyProvider, VectorExperimental>>
        providers = {policy_provider_.get()};
    policy_service_ = std::make_unique<policy::PolicyServiceImpl>(providers);
    prefs_ = std::make_unique<TestBrowserSwitcherPrefs>(&prefs_backend_,
                                                        policy_service_.get());
  }

  void TearDown() override { prefs_->Shutdown(); }

  policy::MockConfigurationPolicyProvider* policy_provider() {
    return policy_provider_.get();
  }
  sync_preferences::TestingPrefServiceSyncable* prefs_backend() {
    return &prefs_backend_;
  }
  BrowserSwitcherPrefs* prefs() { return prefs_.get(); }

 private:
  content::BrowserTaskEnvironment task_environment_;

  sync_preferences::TestingPrefServiceSyncable prefs_backend_;

  std::unique_ptr<policy::MockConfigurationPolicyProvider> policy_provider_;
  std::unique_ptr<policy::PolicyService> policy_service_;
  std::unique_ptr<BrowserSwitcherPrefs> prefs_;
};

TEST_F(BrowserSwitcherPrefsTest, ListensForPrefChanges) {
  prefs_backend()->SetManagedPref(prefs::kEnabled, base::Value(true));
  prefs_backend()->SetManagedPref(prefs::kAlternativeBrowserPath,
                                  base::Value("notepad.exe"));
  prefs_backend()->SetManagedPref(prefs::kAlternativeBrowserParameters,
                                  StringArrayToValue({"a", "b", "c"}));
#if BUILDFLAG(IS_WIN)
  prefs_backend()->SetManagedPref(prefs::kChromePath, base::Value("cmd.exe"));
  prefs_backend()->SetManagedPref(prefs::kChromeParameters,
                                  StringArrayToValue({"d", "e", "f"}));
#endif
  prefs_backend()->SetManagedPref(prefs::kUrlList,
                                  StringArrayToValue({"example.com"}));
  prefs_backend()->SetManagedPref(prefs::kUrlGreylist,
                                  StringArrayToValue({"foo.example.com"}));

  EXPECT_EQ(true, prefs()->IsEnabled());

  EXPECT_EQ("notepad.exe", prefs()->GetAlternativeBrowserPath());

  EXPECT_EQ(3u, prefs()->GetAlternativeBrowserParameters().size());
  EXPECT_EQ("a", prefs()->GetAlternativeBrowserParameters()[0]);
  EXPECT_EQ("b", prefs()->GetAlternativeBrowserParameters()[1]);
  EXPECT_EQ("c", prefs()->GetAlternativeBrowserParameters()[2]);

#if BUILDFLAG(IS_WIN)
  EXPECT_EQ("cmd.exe", prefs()->GetChromePath().MaybeAsASCII());

  EXPECT_EQ(3u, prefs()->GetChromeParameters().size());
  EXPECT_EQ("d", prefs()->GetChromeParameters()[0]);
  EXPECT_EQ("e", prefs()->GetChromeParameters()[1]);
  EXPECT_EQ("f", prefs()->GetChromeParameters()[2]);
#endif

  EXPECT_EQ(1u, prefs()->GetRules().sitelist.size());
  EXPECT_EQ("example.com", prefs()->GetRules().sitelist[0]->ToString());

  EXPECT_EQ(1u, prefs()->GetRules().greylist.size());
  EXPECT_EQ("foo.example.com", prefs()->GetRules().greylist[0]->ToString());
}

TEST_F(BrowserSwitcherPrefsTest, TriggersObserversOnPolicyChange) {
  policy::PolicyMap policy_map;
  policy_map.Set(policy::key::kAlternativeBrowserPath,
                 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
                 policy::POLICY_SOURCE_PLATFORM, base::Value("notepad.exe"),
                 nullptr);

  base::RunLoop run_loop;
  auto subscription = prefs()->RegisterPrefsChangedCallback(base::BindRepeating(
      [](base::OnceClosure quit, BrowserSwitcherPrefs* prefs,
         const std::vector<std::string>& changed_prefs) {
        EXPECT_EQ("notepad.exe", prefs->GetAlternativeBrowserPath());
        std::vector<std::string> expected_changed_prefs{
            prefs::kAlternativeBrowserPath,
        };
        EXPECT_EQ(expected_changed_prefs, changed_prefs);
        std::move(quit).Run();
      },
      run_loop.QuitClosure()));

  prefs_backend()->SetManagedPref(prefs::kAlternativeBrowserPath,
                                  base::Value("notepad.exe"));
  policy_provider()->UpdateChromePolicy(policy_map);

  run_loop.Run();

  // If this code is reached, the callback has run as expected. Now just clean
  // up.
}

}  // namespace browser_switcher