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

content / public / test / simple_url_loader_test_helper.h [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_TEST_SIMPLE_URL_LOADER_TEST_HELPER_H_
#define CONTENT_PUBLIC_TEST_SIMPLE_URL_LOADER_TEST_HELPER_H_

#include <memory>
#include <optional>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "services/network/public/cpp/simple_url_loader.h"

namespace content {

// Test utility class for waiting until a SimpleURLLoader has received a
// response body.
class SimpleURLLoaderTestHelper {
 public:
  SimpleURLLoaderTestHelper();

  SimpleURLLoaderTestHelper(const SimpleURLLoaderTestHelper&) = delete;
  SimpleURLLoaderTestHelper& operator=(const SimpleURLLoaderTestHelper&) =
      delete;

  ~SimpleURLLoaderTestHelper();

  // Returns a BodyAsStringCallbackDeprecated for use with a SimpleURLLoader.
  // May be called only once.
  network::SimpleURLLoader::BodyAsStringCallback GetCallback();
  network::SimpleURLLoader::BodyAsStringCallbackDeprecated
  GetCallbackDeprecated();

  // Waits until the callback returned by GetCallback() is invoked.
  void WaitForCallback();

  // Response body passed to the callback returned by GetCallback, if there was
  // one.
  const std::optional<std::string>& response_body() const {
    return response_body_;
  }

 private:
  // Called back GetCallback().  Stores the response body and quits the message
  // loop.
  void OnCompleteCallback(std::optional<std::string> response_body);
  void OnCompleteCallbackDeprecated(std::unique_ptr<std::string> response_body);

  // Used to ensure GetCallback() is called only once.
  bool callback_created_ = false;
  // Used to ensure WaitForCallback() is called only once.
  bool wait_started_ = false;

  base::RunLoop run_loop_;

  std::optional<std::string> response_body_;

  base::WeakPtrFactory<SimpleURLLoaderTestHelper> weak_ptr_factory_{this};
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_SIMPLE_URL_LOADER_TEST_HELPER_H_