new_tab_delegate_browsertest.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "weblayer/public/new_tab_delegate.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "net/test/embedded_test_server/embedded_test_server.h"
  7. #include "weblayer/browser/tab_impl.h"
  8. #include "weblayer/public/browser.h"
  9. #include "weblayer/public/new_tab_delegate.h"
  10. #include "weblayer/public/tab.h"
  11. #include "weblayer/shell/browser/shell.h"
  12. #include "weblayer/test/weblayer_browser_test.h"
  13. #include "weblayer/test/weblayer_browser_test_utils.h"
  14. namespace weblayer {
  15. namespace {
  16. class DestroyingNewTabDelegate : public NewTabDelegate {
  17. public:
  18. void WaitForOnNewTab() { run_loop_.Run(); }
  19. bool was_on_new_tab_called() const { return was_on_new_tab_called_; }
  20. // NewTabDelegate:
  21. void OnNewTab(Tab* new_tab, NewTabType type) override {
  22. was_on_new_tab_called_ = true;
  23. new_tab->GetBrowser()->DestroyTab(new_tab);
  24. run_loop_.Quit();
  25. }
  26. private:
  27. base::RunLoop run_loop_;
  28. bool was_on_new_tab_called_ = false;
  29. };
  30. } // namespace
  31. using NewTabDelegateTest = WebLayerBrowserTest;
  32. IN_PROC_BROWSER_TEST_F(NewTabDelegateTest, DestroyTabOnNewTab) {
  33. ASSERT_TRUE(embedded_test_server()->Start());
  34. NavigateAndWaitForCompletion(embedded_test_server()->GetURL("/echo"),
  35. shell()->tab());
  36. DestroyingNewTabDelegate new_tab_delegate;
  37. shell()->tab()->SetNewTabDelegate(&new_tab_delegate);
  38. GURL popup_url = embedded_test_server()->GetURL("/echo?popup");
  39. ExecuteScriptWithUserGesture(
  40. shell()->tab(),
  41. base::StringPrintf("window.open('%s')", popup_url.spec().c_str()));
  42. new_tab_delegate.WaitForOnNewTab();
  43. EXPECT_TRUE(new_tab_delegate.was_on_new_tab_called());
  44. EXPECT_EQ(1u, shell()->tab()->GetBrowser()->GetTabs().size());
  45. }
  46. } // namespace weblayer