mock_web_bundle_parser.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_WEB_PACKAGE_TEST_SUPPORT_MOCK_WEB_BUNDLE_PARSER_H_
  5. #define COMPONENTS_WEB_PACKAGE_TEST_SUPPORT_MOCK_WEB_BUNDLE_PARSER_H_
  6. #include "components/web_package/mojom/web_bundle_parser.mojom.h"
  7. #include "mojo/public/cpp/bindings/receiver.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace web_package {
  10. class MockWebBundleParser final : public mojom::WebBundleParser {
  11. public:
  12. MockWebBundleParser(mojo::PendingReceiver<mojom::WebBundleParser> receiver,
  13. bool simulate_parse_integrity_block_crash,
  14. bool simulate_parse_metadata_crash,
  15. bool simulate_parse_response_crash);
  16. // Initializes a `MockWebBundleParser` as a replacement for a disconnected
  17. // `MockWebBundleParser` by moving its `wait_*` callbacks into the new
  18. // instance.
  19. MockWebBundleParser(mojo::PendingReceiver<mojom::WebBundleParser> receiver,
  20. bool simulate_parse_integrity_block_crash,
  21. bool simulate_parse_metadata_crash,
  22. bool simulate_parse_response_crash,
  23. std::unique_ptr<MockWebBundleParser> disconnected_parser);
  24. MockWebBundleParser(const MockWebBundleParser&) = delete;
  25. MockWebBundleParser& operator=(const MockWebBundleParser&) = delete;
  26. ~MockWebBundleParser() override;
  27. void RunIntegrityBlockCallback(
  28. mojom::BundleIntegrityBlockPtr integrity_block,
  29. mojom::BundleIntegrityBlockParseErrorPtr error = nullptr);
  30. void RunMetadataCallback(
  31. mojom::BundleMetadataPtr metadata,
  32. web_package::mojom::BundleMetadataParseErrorPtr error = nullptr);
  33. void RunResponseCallback(mojom::BundleResponsePtr response,
  34. mojom::BundleResponseParseErrorPtr error = nullptr);
  35. void WaitUntilParseIntegrityBlockCalled(base::OnceClosure closure);
  36. void WaitUntilParseMetadataCalled(
  37. base::OnceCallback<void(int64_t offset)> callback);
  38. void WaitUntilParseResponseCalled(
  39. base::OnceCallback<void(mojom::BundleResponseLocationPtr)> callback);
  40. void SetIntegrityBlockParseResult(
  41. mojom::BundleIntegrityBlockPtr integrity_block,
  42. mojom::BundleIntegrityBlockParseErrorPtr error = nullptr);
  43. void SetMetadataParseResult(
  44. mojom::BundleMetadataPtr metadata,
  45. web_package::mojom::BundleMetadataParseErrorPtr error = nullptr);
  46. void SetResponseParseResult(
  47. mojom::BundleResponsePtr response,
  48. mojom::BundleResponseParseErrorPtr error = nullptr);
  49. void SimulateDisconnect() { receiver_.reset(); }
  50. private:
  51. // mojom::WebBundleParser implementation.
  52. void ParseIntegrityBlock(ParseIntegrityBlockCallback callback) override;
  53. void ParseMetadata(int64_t offset, ParseMetadataCallback callback) override;
  54. void ParseResponse(uint64_t response_offset,
  55. uint64_t response_length,
  56. ParseResponseCallback callback) override;
  57. mojo::Receiver<mojom::WebBundleParser> receiver_;
  58. const bool simulate_parse_integrity_block_crash_;
  59. const bool simulate_parse_metadata_crash_;
  60. const bool simulate_parse_response_crash_;
  61. ParseIntegrityBlockCallback integrity_block_callback_;
  62. ParseMetadataCallback metadata_callback_;
  63. ParseResponseCallback response_callback_;
  64. int64_t parse_metadata_args_;
  65. mojom::BundleResponseLocationPtr parse_response_args_;
  66. base::OnceClosure wait_parse_integrity_block_callback_;
  67. base::OnceCallback<void(int64_t offset)> wait_parse_metadata_callback_;
  68. base::OnceCallback<void(mojom::BundleResponseLocationPtr)>
  69. wait_parse_response_callback_;
  70. absl::optional<std::pair<mojom::BundleIntegrityBlockPtr,
  71. mojom::BundleIntegrityBlockParseErrorPtr>>
  72. integrity_block_parse_result_;
  73. absl::optional<std::pair<mojom::BundleMetadataPtr,
  74. web_package::mojom::BundleMetadataParseErrorPtr>>
  75. metadata_parse_result_;
  76. absl::optional<
  77. std::pair<mojom::BundleResponsePtr, mojom::BundleResponseParseErrorPtr>>
  78. response_parse_result_;
  79. };
  80. } // namespace web_package
  81. #endif // COMPONENTS_WEB_PACKAGE_TEST_SUPPORT_MOCK_WEB_BUNDLE_PARSER_H_