context_impl_browsertest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "base/command_line.h"
  5. #include "base/no_destructor.h"
  6. #include "content/public/test/browser_test.h"
  7. #include "fuchsia_web/common/test/frame_test_util.h"
  8. #include "fuchsia_web/common/test/test_navigation_listener.h"
  9. #include "fuchsia_web/webengine/switches.h"
  10. #include "fuchsia_web/webengine/test/frame_for_test.h"
  11. #include "fuchsia_web/webengine/test/web_engine_browser_test.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "url/url_constants.h"
  14. namespace {
  15. // Defines a suite of tests that exercise browser-level configuration and
  16. // functionality.
  17. class ContextImplTest : public WebEngineBrowserTest {
  18. public:
  19. ContextImplTest() = default;
  20. ~ContextImplTest() override = default;
  21. ContextImplTest(const ContextImplTest&) = delete;
  22. ContextImplTest& operator=(const ContextImplTest&) = delete;
  23. protected:
  24. // Synchronously gets the list of all cookies from the fuchsia.web.Context.
  25. std::vector<fuchsia::web::Cookie> GetCookies() {
  26. base::RunLoop get_cookies_loop;
  27. // Connect to the Context's CookieManager and request all the cookies.
  28. fuchsia::web::CookieManagerPtr cookie_manager;
  29. context()->GetCookieManager(cookie_manager.NewRequest());
  30. fuchsia::web::CookiesIteratorPtr cookies_iterator;
  31. cookie_manager->GetCookieList(nullptr, nullptr,
  32. cookies_iterator.NewRequest());
  33. // |cookies_iterator| will disconnect once after the last cookies have been
  34. // returned by GetNext().
  35. cookies_iterator.set_error_handler([&](zx_status_t status) {
  36. EXPECT_EQ(ZX_ERR_PEER_CLOSED, status);
  37. get_cookies_loop.Quit();
  38. });
  39. std::vector<fuchsia::web::Cookie> cookies;
  40. // std::function<> must be used here because fit::function<> is move-only
  41. // and this callback will be used both for the initial GetNext() call, and
  42. // for the follow-up calls made each time GetNext() results are received.
  43. std::function<void(std::vector<fuchsia::web::Cookie>)> get_next_callback =
  44. [&](std::vector<fuchsia::web::Cookie> new_cookies) {
  45. cookies.insert(cookies.end(),
  46. std::make_move_iterator(new_cookies.begin()),
  47. std::make_move_iterator(new_cookies.end()));
  48. cookies_iterator->GetNext(get_next_callback);
  49. };
  50. cookies_iterator->GetNext(get_next_callback);
  51. get_cookies_loop.Run();
  52. return cookies;
  53. }
  54. };
  55. fuchsia::web::Cookie CreateExpectedCookie() {
  56. fuchsia::web::Cookie cookie;
  57. fuchsia::web::CookieId id;
  58. id.set_name("foo");
  59. id.set_path("/");
  60. id.set_domain("127.0.0.1");
  61. cookie.set_id(std::move(id));
  62. cookie.set_value("bar");
  63. return cookie;
  64. }
  65. const fuchsia::web::Cookie& ExpectedCookie() {
  66. static const base::NoDestructor<fuchsia::web::Cookie> expected_cookie(
  67. CreateExpectedCookie());
  68. return *expected_cookie;
  69. }
  70. } // namespace
  71. // BrowserContext with persistent storage stores cookies such that they can
  72. // be retrieved via the CookieManager API.
  73. IN_PROC_BROWSER_TEST_F(ContextImplTest, PersistentCookieStore) {
  74. ASSERT_TRUE(embedded_test_server()->Start());
  75. auto frame =
  76. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  77. const GURL kSetCookieUrl(
  78. embedded_test_server()->GetURL("/set-cookie?foo=bar"));
  79. LoadUrlAndExpectResponse(frame.GetNavigationController(),
  80. fuchsia::web::LoadUrlParams(), kSetCookieUrl.spec());
  81. frame.navigation_listener().RunUntilUrlEquals(kSetCookieUrl);
  82. std::vector<fuchsia::web::Cookie> cookies = GetCookies();
  83. ASSERT_EQ(cookies.size(), 1u);
  84. ASSERT_TRUE(cookies[0].has_id());
  85. EXPECT_TRUE(fidl::Equals(cookies[0], ExpectedCookie()));
  86. // Check that the cookie persists beyond the lifetime of the Frame by
  87. // releasing the Frame and re-querying the CookieStore.
  88. frame = FrameForTest();
  89. base::RunLoop().RunUntilIdle();
  90. cookies = GetCookies();
  91. ASSERT_EQ(cookies.size(), 1u);
  92. ASSERT_TRUE(cookies[0].has_id());
  93. EXPECT_TRUE(fidl::Equals(cookies[0], ExpectedCookie()));
  94. }
  95. // Suite for tests which run the BrowserContext in incognito mode (no data
  96. // directory).
  97. class IncognitoContextImplTest : public ContextImplTest {
  98. public:
  99. IncognitoContextImplTest() = default;
  100. ~IncognitoContextImplTest() override = default;
  101. IncognitoContextImplTest(const IncognitoContextImplTest&) = delete;
  102. IncognitoContextImplTest& operator=(const IncognitoContextImplTest&) = delete;
  103. void SetUp() override {
  104. base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kIncognito);
  105. ContextImplTest::SetUp();
  106. }
  107. };
  108. // Verify that the browser can be initialized without a persistent data
  109. // directory.
  110. IN_PROC_BROWSER_TEST_F(IncognitoContextImplTest, NavigateFrame) {
  111. auto frame =
  112. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  113. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  114. fuchsia::web::LoadUrlParams(),
  115. url::kAboutBlankURL));
  116. frame.navigation_listener().RunUntilUrlEquals(GURL(url::kAboutBlankURL));
  117. }
  118. // In-memory cookie store stores cookies, and is accessible via CookieManager.
  119. IN_PROC_BROWSER_TEST_F(IncognitoContextImplTest, InMemoryCookieStore) {
  120. ASSERT_TRUE(embedded_test_server()->Start());
  121. auto frame =
  122. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  123. const GURL kSetCookieUrl(
  124. embedded_test_server()->GetURL("/set-cookie?foo=bar"));
  125. LoadUrlAndExpectResponse(frame.GetNavigationController(),
  126. fuchsia::web::LoadUrlParams(), kSetCookieUrl.spec());
  127. frame.navigation_listener().RunUntilUrlEquals(kSetCookieUrl);
  128. std::vector<fuchsia::web::Cookie> cookies = GetCookies();
  129. ASSERT_EQ(cookies.size(), 1u);
  130. EXPECT_TRUE(fidl::Equals(cookies[0], ExpectedCookie()));
  131. }