1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/clipboard/clipboard_change_event_controller.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_test_utils.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
namespace blink {
class ClipboardChangeEventTest : public ClipboardTestBase {};
TEST_F(ClipboardChangeEventTest, ClipboardChangeEventFiresWhenFocused) {
ExecutionContext* executionContext = GetFrame().DomWindow();
// Clipboardchange event requires a secure origin and page in focus to work.
SetSecureOrigin(executionContext);
SetPageFocus(true);
auto* clipboard_change_event_handler =
MakeGarbageCollected<EventCountingListener>();
GetDocument().addEventListener(event_type_names::kClipboardchange,
clipboard_change_event_handler, false);
auto* clipboard_change_event_controller =
MakeGarbageCollected<ClipboardChangeEventController>(
*GetFrame().DomWindow()->navigator(), &GetDocument());
EXPECT_EQ(clipboard_change_event_handler->Count(), 0);
// Simulate a clipboard change event from the system clipboard.
clipboard_change_event_controller->DidUpdateData();
// Wait for any internal callbacks to run.
test::RunPendingTasks();
// Expect a single clipboardchange event to be fired.
EXPECT_EQ(clipboard_change_event_handler->Count(), 1);
// Clean up the event listener
GetDocument().removeEventListener(event_type_names::kClipboardchange,
clipboard_change_event_handler, false);
}
TEST_F(ClipboardChangeEventTest, ClipboardChangeEventNotFiredWhenNotFocused) {
ExecutionContext* executionContext = GetFrame().DomWindow();
SetSecureOrigin(executionContext);
SetPageFocus(false);
auto* clipboard_change_event_handler =
MakeGarbageCollected<EventCountingListener>();
GetDocument().addEventListener(event_type_names::kClipboardchange,
clipboard_change_event_handler, false);
auto* clipboard_change_event_controller =
MakeGarbageCollected<ClipboardChangeEventController>(
*GetFrame().DomWindow()->navigator(), &GetDocument());
EXPECT_EQ(clipboard_change_event_handler->Count(), 0);
// Simulate a clipboard change event from the system clipboard.
clipboard_change_event_controller->DidUpdateData();
// Wait for any internal callbacks to run.
test::RunPendingTasks();
// Expect no clipboardchange event to be fired since the page is not focused.
EXPECT_EQ(clipboard_change_event_handler->Count(), 0);
// Simulate more clipboard updates
clipboard_change_event_controller->DidUpdateData();
clipboard_change_event_controller->DidUpdateData();
// Focus back and wait for any internal callbacks to run.
SetPageFocus(true);
test::RunPendingTasks();
// Expect a single clipboardchange event to be fired
// now that the page is focused.
EXPECT_EQ(clipboard_change_event_handler->Count(), 1);
// Clean up the event listener
GetDocument().removeEventListener(event_type_names::kClipboardchange,
clipboard_change_event_handler, false);
}
} // namespace blink
|