summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/testing/code_cache_loader_mock.h
blob: fb06db03e65abbcfc7cdf19e412f4d7b615a7efc (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_CODE_CACHE_LOADER_MOCK_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_CODE_CACHE_LOADER_MOCK_H_

#include "base/memory/weak_ptr.h"
#include "third_party/blink/public/platform/web_code_cache_loader.h"
#include "url/gurl.h"

namespace blink {

// A simple class for mocking WebCodeCacheLoader.
class CodeCacheLoaderMock : public WebCodeCacheLoader {
 public:
  // A class which can be owned by both this mock loader and the creator of this
  // mock loader, which lets the creator control the behavior of the mock loader
  // without having to retain a reference to the mock loader itself.
  class Controller : public base::RefCounted<Controller> {
   public:
    void DelayResponse();
    void Respond(base::Time time, mojo_base::BigBuffer data);

   private:
    friend class CodeCacheLoaderMock;
    friend class base::RefCounted<Controller>;
    ~Controller() = default;

    // Whether to delay responses until Respond is called.
    // Otherwise responses are immediate and empty.
    bool delayed_ = false;

    // Callback saved by fetch call, if delayed_ was true.
    WebCodeCacheLoader::FetchCodeCacheCallback callback_;
  };

  explicit CodeCacheLoaderMock(scoped_refptr<Controller> controller = nullptr)
      : controller_(std::move(controller)) {}
  CodeCacheLoaderMock(const CodeCacheLoaderMock&) = delete;
  CodeCacheLoaderMock& operator=(const CodeCacheLoaderMock&) = delete;
  ~CodeCacheLoaderMock() override = default;

  // CodeCacheLoader methods:
  void FetchFromCodeCache(
      blink::mojom::CodeCacheType cache_type,
      const WebURL& url,
      WebCodeCacheLoader::FetchCodeCacheCallback callback) override;

 private:
  scoped_refptr<Controller> controller_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_CODE_CACHE_LOADER_MOCK_H_