clipboard_browsertest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021 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 <string>
  5. #include "components/permissions/permission_request_manager.h"
  6. #include "components/permissions/test/mock_permission_prompt_factory.h"
  7. #include "content/public/test/browser_test_utils.h"
  8. #include "content/public/test/scoped_page_focus_override.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "url/gurl.h"
  11. #include "weblayer/browser/tab_impl.h"
  12. #include "weblayer/shell/browser/shell.h"
  13. #include "weblayer/test/weblayer_browser_test.h"
  14. #include "weblayer/test/weblayer_browser_test_utils.h"
  15. namespace weblayer {
  16. class ClipboardBrowserTest : public WebLayerBrowserTest {
  17. public:
  18. ClipboardBrowserTest() = default;
  19. ~ClipboardBrowserTest() override = default;
  20. // WebLayerBrowserTest:
  21. void SetUpOnMainThread() override {
  22. WebLayerBrowserTest::SetUpOnMainThread();
  23. permissions::PermissionRequestManager* manager =
  24. permissions::PermissionRequestManager::FromWebContents(
  25. GetWebContents());
  26. prompt_factory_ =
  27. std::make_unique<permissions::MockPermissionPromptFactory>(manager);
  28. title_watcher_ =
  29. std::make_unique<content::TitleWatcher>(GetWebContents(), u"success");
  30. title_watcher_->AlsoWaitForTitle(u"fail");
  31. EXPECT_TRUE(embedded_test_server()->Start());
  32. NavigateAndWaitForCompletion(
  33. embedded_test_server()->GetURL("/clipboard.html"), shell());
  34. // The Clipboard API requires the page to have focus.
  35. scoped_focus_ =
  36. std::make_unique<content::ScopedPageFocusOverride>(GetWebContents());
  37. }
  38. void TearDownOnMainThread() override {
  39. scoped_focus_.reset();
  40. title_watcher_.reset();
  41. prompt_factory_.reset();
  42. }
  43. protected:
  44. content::WebContents* GetWebContents() {
  45. return static_cast<TabImpl*>(shell()->tab())->web_contents();
  46. }
  47. GURL GetBaseOrigin() {
  48. return embedded_test_server()->base_url().DeprecatedGetOriginAsURL();
  49. }
  50. permissions::MockPermissionPromptFactory* prompt_factory() {
  51. return prompt_factory_.get();
  52. }
  53. content::TitleWatcher* title_watcher() { return title_watcher_.get(); }
  54. private:
  55. std::unique_ptr<permissions::MockPermissionPromptFactory> prompt_factory_;
  56. std::unique_ptr<content::TitleWatcher> title_watcher_;
  57. std::unique_ptr<content::ScopedPageFocusOverride> scoped_focus_;
  58. };
  59. IN_PROC_BROWSER_TEST_F(ClipboardBrowserTest, ReadTextSuccess) {
  60. prompt_factory()->set_response_type(
  61. permissions::PermissionRequestManager::ACCEPT_ALL);
  62. ExecuteScriptWithUserGesture(shell()->tab(), "tryClipboardReadText()");
  63. EXPECT_EQ(u"success", title_watcher()->WaitAndGetTitle());
  64. EXPECT_EQ(1, prompt_factory()->TotalRequestCount());
  65. EXPECT_TRUE(prompt_factory()->RequestOriginSeen(GetBaseOrigin()));
  66. }
  67. IN_PROC_BROWSER_TEST_F(ClipboardBrowserTest, WriteSanitizedTextSuccess) {
  68. prompt_factory()->set_response_type(
  69. permissions::PermissionRequestManager::ACCEPT_ALL);
  70. ExecuteScriptWithUserGesture(shell()->tab(), "tryClipboardWriteText()");
  71. EXPECT_EQ(u"success", title_watcher()->WaitAndGetTitle());
  72. // Writing sanitized data to the clipboard does not require a permission.
  73. EXPECT_EQ(0, prompt_factory()->TotalRequestCount());
  74. }
  75. IN_PROC_BROWSER_TEST_F(ClipboardBrowserTest, ReadTextWithoutPermission) {
  76. prompt_factory()->set_response_type(
  77. permissions::PermissionRequestManager::DENY_ALL);
  78. ExecuteScriptWithUserGesture(shell()->tab(), "tryClipboardReadText()");
  79. EXPECT_EQ(u"fail", title_watcher()->WaitAndGetTitle());
  80. EXPECT_EQ(1, prompt_factory()->TotalRequestCount());
  81. EXPECT_TRUE(prompt_factory()->RequestOriginSeen(GetBaseOrigin()));
  82. }
  83. } // namespace weblayer