File: web_app_test.h

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 (99 lines) | stat: -rw-r--r-- 3,957 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
// 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.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_TEST_WEB_APP_TEST_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_WEB_APP_TEST_H_

#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/test_renderer_host.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"

namespace web_app {
class FakeWebAppProvider;
class WebAppProvider;
}

// Consider to implement web app specific test harness independent of
// RenderViewHost.
// When inheriting from this class, the FakeWebAppProvider doesn't automatically
// start the WebAppProvider system. To do so, try using a helper method like
// `test::AwaitStartWebAppProviderAndSubsystems`.
class WebAppTest : public content::RenderViewHostTestHarness {
 public:
  struct WithTestUrlLoaderFactory {};
  struct ValidTraits {
    explicit ValidTraits(base::test::TaskEnvironment::ValidTraits);
    explicit ValidTraits(WithTestUrlLoaderFactory);
  };

  template <typename... WebAppTestTraits>
    requires base::trait_helpers::AreValidTraits<ValidTraits,
                                                 WebAppTestTraits...>
  explicit WebAppTest(WebAppTestTraits&&... traits)
      : content::RenderViewHostTestHarness(
            base::trait_helpers::Exclude<WithTestUrlLoaderFactory>::Filter(
                traits)...) {
    shared_url_loader_factory_ =
        base::trait_helpers::HasTrait<WithTestUrlLoaderFactory,
                                      WebAppTestTraits...>()
            ? test_url_loader_factory_.GetSafeWeakWrapper()
            : nullptr;
  }

  ~WebAppTest() override;

  void SetUp() override;
  void TearDown() override;

  TestingProfile* profile() const { return profile_.get(); }
  TestingProfileManager& profile_manager() { return testing_profile_manager_; }

  network::TestURLLoaderFactory& profile_url_loader_factory() {
    CHECK(shared_url_loader_factory_)
        << "If you want the testing profile to use a "
           "`network::TestURLLoaderFactory`, make sure to pass the "
           "`WebAppTest::WithTestUrlLoaderFactory` trait to the constructor of "
           "`WebAppTest`. By default, the testing profile's `URLLoaderFactory` "
           "will be `nullptr`.";
    return test_url_loader_factory_;
  }

  web_app::WebAppProvider& provider() const;

  web_app::FakeWebAppProvider& fake_provider() const;

  web_app::OsIntegrationTestOverrideImpl& fake_os_integration() const;

 protected:
  // content::RenderViewHostTestHarness.
  content::BrowserContext* GetBrowserContext() final;

 private:
  base::TimeTicks start_time_ = base::TimeTicks::Now();
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_ =
      nullptr;

  // std::unique_ptr so it can be reset in the TearDown method for the safest
  // 'waiting' for os integration to complete, while the task environment is
  // still around.
  std::unique_ptr<web_app::OsIntegrationTestOverrideBlockingRegistration>
      os_integration_test_override_{std::make_unique<
          web_app::OsIntegrationTestOverrideBlockingRegistration>()};

  TestingProfileManager testing_profile_manager_{
      TestingBrowserProcess::GetGlobal()};
  raw_ptr<TestingProfile, DanglingUntriaged> profile_ = nullptr;
};

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_TEST_WEB_APP_TEST_H_