url_bar_browsertest.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2020 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/memory/raw_ptr.h"
  5. #include "base/run_loop.h"
  6. #include "net/test/embedded_test_server/embedded_test_server.h"
  7. #include "weblayer/browser/browser_impl.h"
  8. #include "weblayer/browser/profile_impl.h"
  9. #include "weblayer/browser/tab_impl.h"
  10. #include "weblayer/shell/browser/shell.h"
  11. #include "weblayer/test/weblayer_browser_test.h"
  12. #include "weblayer/test/weblayer_browser_test_utils.h"
  13. namespace weblayer {
  14. class UrlBarBrowserTest : public WebLayerBrowserTest {
  15. public:
  16. UrlBarBrowserTest() = default;
  17. ~UrlBarBrowserTest() override = default;
  18. // WebLayerBrowserTest
  19. void SetUpOnMainThread() override {
  20. WebLayerBrowserTest::SetUpOnMainThread();
  21. ASSERT_TRUE(embedded_test_server()->Start());
  22. tab_ = static_cast<TabImpl*>(shell()->browser()->CreateTab());
  23. another_tab_ = static_cast<TabImpl*>(shell()->browser()->CreateTab());
  24. SetActiveTab(tab_);
  25. }
  26. void PostRunTestOnMainThread() override {
  27. tab_ = nullptr;
  28. another_tab_ = nullptr;
  29. WebLayerBrowserTest::PostRunTestOnMainThread();
  30. }
  31. GURL real_url() {
  32. return embedded_test_server()->GetURL("/simple_page.html");
  33. }
  34. GURL abort_url() { return embedded_test_server()->GetURL("/nocontent"); }
  35. void SetVisibleSecurityStateChangedCallback(base::OnceClosure closure) {
  36. browser_impl()->set_visible_security_state_callback_for_tests(
  37. std::move(closure));
  38. }
  39. void SetActiveTab(TabImpl* tab) { shell()->browser()->SetActiveTab(tab); }
  40. protected:
  41. raw_ptr<TabImpl> tab_ = nullptr;
  42. raw_ptr<TabImpl> another_tab_ = nullptr;
  43. private:
  44. BrowserImpl* browser_impl() {
  45. return static_cast<BrowserImpl*>(shell()->browser());
  46. }
  47. };
  48. IN_PROC_BROWSER_TEST_F(UrlBarBrowserTest, CanceledNavigationsUpdateUrl) {
  49. NavigateAndWaitForCompletion(real_url(), tab_);
  50. {
  51. base::RunLoop run_loop;
  52. SetVisibleSecurityStateChangedCallback(run_loop.QuitClosure());
  53. // Navigating to the /nocontent url cancels the navigation with a 204 error.
  54. NavigateAndWaitForStart(abort_url(), tab_);
  55. // The test won't finish until WebLayer acts on the resulting
  56. // WebContentsObserver::DidChangeVisibleSecurityState() notification, or the
  57. // test times out.
  58. run_loop.Run();
  59. }
  60. }
  61. IN_PROC_BROWSER_TEST_F(UrlBarBrowserTest, ChangingActiveTabUpdatesUrlBarView) {
  62. NavigateAndWaitForCompletion(real_url(), tab_);
  63. NavigateAndWaitForCompletion(real_url(), another_tab_);
  64. {
  65. base::RunLoop run_loop;
  66. SetVisibleSecurityStateChangedCallback(run_loop.QuitClosure());
  67. SetActiveTab(another_tab_);
  68. // The test won't finish until
  69. // BrowserImpl::VisibleSecurityStateOfActiveTabChanged() gets called.
  70. run_loop.Run();
  71. }
  72. }
  73. } // namespace weblayer