File: glic_test_environment.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 (110 lines) | stat: -rw-r--r-- 4,150 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_GLIC_TEST_SUPPORT_GLIC_TEST_ENVIRONMENT_H_
#define CHROME_BROWSER_GLIC_TEST_SUPPORT_GLIC_TEST_ENVIRONMENT_H_

#include <optional>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"

class Profile;
namespace glic {
class GlicKeyedService;
namespace internal {
class TestCookieSynchronizer;
}  // namespace internal
namespace prefs {
enum class FreStatus;
}  // namespace prefs

class GlicTestEnvironmentService;

// Configuration of GlicTestEnvironment.
struct GlicTestEnvironmentConfig {
  // If enabled, forces sign-in and enables model execution capability, which
  // are prerequisites for using Glic.
  bool force_signin_and_model_execution_capability = true;
  // The default FRE status saved to prefs after profile creation.
  std::optional<prefs::FreStatus> fre_status = prefs::FreStatus::kCompleted;
};

// Overrides some glic functionality to allow tests that depend on glic to run.
// This should be created on the main thread.
// If possible, use InteractiveGlicTest instead of this directly!
// This class is used by tests in browser_tests and interactive_ui_tests that
// cannot use InteractiveGlicTest.
//
// Ensures a GlicTestEnvironmentService is created for each browser context, and
// sets the default configuration.
class GlicTestEnvironment {
 public:
  explicit GlicTestEnvironment(
      const GlicTestEnvironmentConfig& config = {},
      std::vector<base::test::FeatureRef> enabled_features =
          {features::kGlic, features::kTabstripComboButton,
           features::kGlicRollout},
      std::vector<base::test::FeatureRef> disabled_features = {
          features::kGlicWarming, features::kGlicFreWarming});

  ~GlicTestEnvironment();

  // Functions to override configuration after creation. These affect only
  // subsequently created profiles.

  // Updates `force_signin_and_model_execution_capability`.
  void SetForceSigninAndModelExecutionCapability(bool force);

  // Sets the `prefs::FreStatus`. If nullopt, keeps the
  // default pref state (`FreStatus::kNotStarted`).
  void SetFreStatusForNewProfiles(std::optional<prefs::FreStatus> fre_status);

  static GlicTestEnvironmentService* GetService(Profile* profile,
                                                bool create = true);

 private:
  void OnWillCreateBrowserContextKeyedServices(
      content::BrowserContext* context);
  base::CallbackListSubscription create_services_subscription_;

  base::test::ScopedFeatureList scoped_feature_list_;
};

// Note: This constructs the GlicKeyedService, if it's not already created,
// which will also construct dependencies like IdentityManager. You likely want
// to create GlicTestEnvironmentService only after other test environment
// classes, like IdentityTestEnvironmentProfileAdaptor.
class GlicTestEnvironmentService : public KeyedService {
 public:
  explicit GlicTestEnvironmentService(Profile* profile);
  ~GlicTestEnvironmentService() override;

  // Convenience functions.
  void SetFRECompletion(prefs::FreStatus fre_status);
  GlicKeyedService* GetService();

  // Glic syncs sign-in cookies to the webview before showing the window. By
  // default, this class replaces this step with an immediately fake success.
  // Change the result of this operation here.
  void SetResultForFutureCookieSync(bool result);
  void SetResultForFutureCookieSyncInFre(bool result);

 private:
  raw_ptr<Profile> profile_;
  // Null during teardown.
  base::WeakPtr<internal::TestCookieSynchronizer> cookie_synchronizer_;
  base::WeakPtr<internal::TestCookieSynchronizer> fre_cookie_synchronizer_;
};

}  // namespace glic

#endif  // CHROME_BROWSER_GLIC_TEST_SUPPORT_GLIC_TEST_ENVIRONMENT_H_