File: promotional_tabs_enabled_policy_browsertest.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 (266 lines) | stat: -rw-r--r-- 11,010 bytes parent folder | download | duplicates (6)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/base_switches.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_ui.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_switches.h"
#include "content/public/test/browser_test.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "url/gurl.h"

namespace policy {

// Base class for testing the policy.
class PromotionalTabsEnabledPolicyTest
    : public PolicyTest,
      public testing::WithParamInterface<
          std::pair</*PromotionalTabsEnabledPolicy*/ PolicyTest::BooleanPolicy,
                    /*PromotionsEnabledPolicy*/ PolicyTest::BooleanPolicy>> {
 public:
  PromotionalTabsEnabledPolicyTest(const PromotionalTabsEnabledPolicyTest&) =
      delete;
  PromotionalTabsEnabledPolicyTest& operator=(
      const PromotionalTabsEnabledPolicyTest&) = delete;

 protected:
  PromotionalTabsEnabledPolicyTest() {
    const std::vector<base::test::FeatureRef> kEnabledFeatures = {
      whats_new::kForceEnabled,
    };
    scoped_feature_list_.InitWithFeatures(kEnabledFeatures, {});
  }
  ~PromotionalTabsEnabledPolicyTest() override = default;

  void SetUp() override {
    // Ordinarily, browser tests include chrome://blank on the command line to
    // suppress any onboarding or promotional tabs. This test, on the other
    // hand, must evaluate startup with nothing on the command line so that a
    // default launch takes place.
    set_open_about_blank_on_browser_launch(false);
    PolicyTest::SetUp();
  }

  void CreatedBrowserMainParts(
      content::BrowserMainParts* browser_main_parts) override {
    // Set policies before the browser starts up.
    PolicyMap policies;

    // Suppress the first-run dialog by disabling metrics reporting.
    policies.Set(key::kMetricsReportingEnabled, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_MACHINE, POLICY_SOURCE_CLOUD, base::Value(false),
                 nullptr);

    // Apply the policy setting under test.
    if (GetParam().first != BooleanPolicy::kNotConfigured) {
      policies.Set(key::kPromotionalTabsEnabled, POLICY_LEVEL_MANDATORY,
                   POLICY_SCOPE_MACHINE, POLICY_SOURCE_CLOUD,
                   base::Value(GetParam().first == BooleanPolicy::kTrue),
                   nullptr);
    }
    if (GetParam().second != BooleanPolicy::kNotConfigured) {
      policies.Set(key::kPromotionsEnabled, POLICY_LEVEL_MANDATORY,
                   POLICY_SCOPE_MACHINE, POLICY_SOURCE_CLOUD,
                   base::Value(GetParam().second == BooleanPolicy::kTrue),
                   nullptr);
    }

    UpdateProviderPolicy(policies);
    PolicyTest::CreatedBrowserMainParts(browser_main_parts);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests that the PromotionalTabsEnabled policy properly suppresses the What's
// New page.
class PromotionalTabsEnabledPolicyWhatsNewTest
    : public PromotionalTabsEnabledPolicyTest {
 public:
  PromotionalTabsEnabledPolicyWhatsNewTest(
      const PromotionalTabsEnabledPolicyWhatsNewTest&) = delete;
  PromotionalTabsEnabledPolicyWhatsNewTest& operator=(
      const PromotionalTabsEnabledPolicyWhatsNewTest&) = delete;

 protected:
  PromotionalTabsEnabledPolicyWhatsNewTest() = default;
  ~PromotionalTabsEnabledPolicyWhatsNewTest() override = default;

  virtual int WhatsNewVersionForPref() { return CHROME_VERSION_MAJOR - 1; }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->RemoveSwitch(switches::kForceFirstRun);
    command_line->AppendSwitch(switches::kForceWhatsNew);
  }

  bool SetUpUserDataDirectory() override {
    base::FilePath user_data_dir;
    base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);

    std::string json;
    base::Value::Dict prefs;
    // Set the session startup pref to NewTab. This enables consistent test
    // expectations across platforms - we should always expect to see the NTP.
    // Without this line, on ChromeOS only, the default type is LAST, which
    // tries to restore the last session and suppresses the NTP.
    prefs.SetByDottedPath(prefs::kRestoreOnStartup,
                          SessionStartupPref::kPrefValueNewTab);
    base::JSONWriter::Write(prefs, &json);

    base::FilePath default_dir =
        user_data_dir.AppendASCII(chrome::kInitialProfile);
    if (!base::CreateDirectory(default_dir)) {
      ADD_FAILURE() << "base::CreateDirectory() failed, " << default_dir;
      return false;
    }

    base::FilePath preferences_path =
        default_dir.Append(chrome::kPreferencesFilename);
    if (!base::WriteFile(preferences_path, json)) {
      ADD_FAILURE() << "base::WriteFile() failed, " << preferences_path;
      return false;
    }

    // Also set the version for What's New in the local state.
    base::Value::Dict local_state;
    local_state.SetByDottedPath(prefs::kLastWhatsNewVersion,
                                WhatsNewVersionForPref());
    std::string local_state_string;
    base::JSONWriter::Write(local_state, &local_state_string);
    base::FilePath local_state_path =
        user_data_dir.Append(chrome::kLocalStateFilename);
    if (!base::WriteFile(local_state_path, local_state_string)) {
      ADD_FAILURE() << "base::WriteFile() failed, " << local_state_path;
      return false;
    }

    return true;
  }
};

// This is disabled due to flakiness: https://crbug.com/1362518
#define MAYBE_RunTest DISABLED_RunTest
IN_PROC_BROWSER_TEST_P(PromotionalTabsEnabledPolicyWhatsNewTest,
                       MAYBE_RunTest) {
  // Delay to allow the network request simulation to finish.
  base::RunLoop run_loop;
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, run_loop.QuitClosure(), TestTimeouts::action_timeout());
  run_loop.Run();
  TabStripModel* tab_strip = browser()->tab_strip_model();
  ASSERT_GE(tab_strip->count(), 1);
  const auto& url = tab_strip->GetWebContentsAt(0)->GetLastCommittedURL();
  bool promotions_disabled = (GetParam().first == BooleanPolicy::kFalse) ||
                             (GetParam().first == BooleanPolicy::kFalse);
  if (promotions_disabled) {
    // Only the NTP should show.
    EXPECT_EQ(tab_strip->count(), 1);
    if (url.possibly_invalid_spec() != chrome::kChromeUINewTabURL) {
      EXPECT_PRED2(search::IsNTPOrRelatedURL, url, browser()->profile());
    }
  } else {
    EXPECT_EQ(tab_strip->count(), 2);
    // Whats's New should show and be the active tab.
    EXPECT_EQ(url.possibly_invalid_spec(), chrome::kChromeUIWhatsNewURL);
    EXPECT_EQ(0, tab_strip->active_index());
    // The second tab should be the NTP.
    const auto& url_tab1 =
        tab_strip->GetWebContentsAt(1)->GetLastCommittedURL();
    EXPECT_EQ(url_tab1.possibly_invalid_spec(), chrome::kChromeUINewTabURL);
  }
}

INSTANTIATE_TEST_SUITE_P(
    All,
    PromotionalTabsEnabledPolicyWhatsNewTest,
    testing::ValuesIn(std::vector<std::pair<PolicyTest::BooleanPolicy,
                                            PolicyTest::BooleanPolicy>>{
        {PolicyTest::BooleanPolicy::kNotConfigured,
         PolicyTest::BooleanPolicy::kNotConfigured},
        {PolicyTest::BooleanPolicy::kFalse, PolicyTest::BooleanPolicy::kFalse},
        {PolicyTest::BooleanPolicy::kTrue, PolicyTest::BooleanPolicy::kTrue},
        {PolicyTest::BooleanPolicy::kFalse,
         PolicyTest::BooleanPolicy::kTrue}}));

// Tests that What's New doesn't show up regardless of the policy if the version
// is not greater than the one in |prefs::kLastWhatsNewVersion|.
class PromotionalTabsEnabledPolicyWhatsNewInvalidTest
    : public PromotionalTabsEnabledPolicyWhatsNewTest {
 public:
  PromotionalTabsEnabledPolicyWhatsNewInvalidTest(
      const PromotionalTabsEnabledPolicyWhatsNewInvalidTest&) = delete;
  PromotionalTabsEnabledPolicyWhatsNewTest& operator=(
      const PromotionalTabsEnabledPolicyWhatsNewInvalidTest&) = delete;

 protected:
  PromotionalTabsEnabledPolicyWhatsNewInvalidTest() = default;
  ~PromotionalTabsEnabledPolicyWhatsNewInvalidTest() override = default;

  int WhatsNewVersionForPref() override { return CHROME_VERSION_MAJOR; }
};

IN_PROC_BROWSER_TEST_P(PromotionalTabsEnabledPolicyWhatsNewInvalidTest,
                       RunTest) {
  // Delay to allow the network request simulation to finish.
  base::RunLoop run_loop;
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, run_loop.QuitClosure(), TestTimeouts::action_timeout());
  run_loop.Run();
  TabStripModel* tab_strip = browser()->tab_strip_model();
  ASSERT_GE(tab_strip->count(), 1);
  const auto& url = tab_strip->GetWebContentsAt(0)->GetLastCommittedURL();

  // Only the NTP should show. There are no other relevant tabs since
  // What's New has already been shown or promotional tabs are disabled.
  EXPECT_EQ(tab_strip->count(), 1);
  if (url.possibly_invalid_spec() != chrome::kChromeUINewTabURL) {
    EXPECT_PRED2(search::IsNTPOrRelatedURL, url, browser()->profile());
  }
}

INSTANTIATE_TEST_SUITE_P(
    All,
    PromotionalTabsEnabledPolicyWhatsNewInvalidTest,
    testing::ValuesIn(std::vector<std::pair<PolicyTest::BooleanPolicy,
                                            PolicyTest::BooleanPolicy>>{
        {PolicyTest::BooleanPolicy::kNotConfigured,
         PolicyTest::BooleanPolicy::kNotConfigured},
        {PolicyTest::BooleanPolicy::kFalse, PolicyTest::BooleanPolicy::kFalse},
        {PolicyTest::BooleanPolicy::kTrue, PolicyTest::BooleanPolicy::kTrue},
        {PolicyTest::BooleanPolicy::kFalse,
         PolicyTest::BooleanPolicy::kTrue}}));
}  // namespace policy