network_context_owner_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 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. #include "ios/web/public/init/network_context_owner.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/bind.h"
  8. #include "base/run_loop.h"
  9. #include "ios/web/public/test/web_task_environment.h"
  10. #include "ios/web/public/thread/web_task_traits.h"
  11. #include "ios/web/public/thread/web_thread.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "net/url_request/url_request_context.h"
  14. #include "net/url_request/url_request_context_getter.h"
  15. #include "net/url_request/url_request_test_util.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #include "testing/platform_test.h"
  18. namespace web {
  19. class NetworkContextOwnerTest : public PlatformTest {
  20. protected:
  21. NetworkContextOwnerTest()
  22. : saw_connection_error_(false),
  23. context_getter_(base::MakeRefCounted<net::TestURLRequestContextGetter>(
  24. GetIOThreadTaskRunner({}))) {}
  25. ~NetworkContextOwnerTest() override {
  26. // Tests should cleanup after themselves.
  27. EXPECT_EQ(network_context_owner_.get(), nullptr);
  28. }
  29. void WatchForErrors() {
  30. ASSERT_TRUE(network_context_.is_bound());
  31. network_context_.set_disconnect_handler(base::BindOnce(
  32. &NetworkContextOwnerTest::SawError, base::Unretained(this)));
  33. }
  34. void SawError() { saw_connection_error_ = true; }
  35. bool saw_connection_error_;
  36. WebTaskEnvironment task_environment_;
  37. scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
  38. mojo::Remote<network::mojom::NetworkContext> network_context_;
  39. std::unique_ptr<NetworkContextOwner> network_context_owner_;
  40. };
  41. // Test that NetworkContextOwner actually creates a NetworkContext owner and
  42. // connects a pipe to it, and destroys its end of the pipe when it's gone.
  43. TEST_F(NetworkContextOwnerTest, Basic) {
  44. EXPECT_FALSE(network_context_.is_bound());
  45. network_context_owner_ = std::make_unique<NetworkContextOwner>(
  46. context_getter_.get(),
  47. /*cors_exempt_header_list=*/std::vector<std::string>(),
  48. &network_context_);
  49. EXPECT_TRUE(network_context_.is_bound());
  50. WatchForErrors();
  51. base::RunLoop().RunUntilIdle();
  52. EXPECT_FALSE(saw_connection_error_);
  53. web::GetIOThreadTaskRunner({})->DeleteSoon(FROM_HERE,
  54. network_context_owner_.release());
  55. base::RunLoop().RunUntilIdle();
  56. EXPECT_TRUE(saw_connection_error_); // other end gone
  57. }
  58. // Test to make sure that explicit shutdown of URLRequestContextGetter destroys
  59. // the NetworkContext object as expected.
  60. TEST_F(NetworkContextOwnerTest, ShutdownHandling) {
  61. EXPECT_FALSE(network_context_.is_bound());
  62. network_context_owner_ = std::make_unique<NetworkContextOwner>(
  63. context_getter_.get(),
  64. /*cors_exempt_header_list=*/std::vector<std::string>(),
  65. &network_context_);
  66. EXPECT_TRUE(network_context_.is_bound());
  67. WatchForErrors();
  68. base::RunLoop().RunUntilIdle();
  69. EXPECT_FALSE(saw_connection_error_);
  70. web::GetIOThreadTaskRunner({})->PostTask(
  71. FROM_HERE,
  72. base::BindOnce(
  73. &net::TestURLRequestContextGetter::NotifyContextShuttingDown,
  74. context_getter_));
  75. base::RunLoop().RunUntilIdle();
  76. EXPECT_TRUE(saw_connection_error_); // other end gone post-shutdown.
  77. web::GetIOThreadTaskRunner({})->DeleteSoon(FROM_HERE,
  78. network_context_owner_.release());
  79. }
  80. } // namespace web