summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/editor_test.cc
blob: 42dc372e1075ca48c3b38d95bf5bfa51987effaa (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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/core/editing/editor.h"

#include "third_party/blink/renderer/core/clipboard/system_clipboard.h"
#include "third_party/blink/renderer/core/editing/commands/editor_command.h"
#include "third_party/blink/renderer/core/editing/testing/editing_test_base.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"

namespace blink {

class EditorTest : public EditingTestBase {
 public:
  void TearDown() override {
    SystemClipboard::GetInstance().WritePlainText(String(""));
    EditingTestBase::TearDown();
  }

  void ExecuteCopy() {
    Editor& editor = GetDocument().GetFrame()->GetEditor();
    editor.CreateCommand("Copy").Execute();
    test::RunPendingTasks();
  }
};

TEST_F(EditorTest, copyGeneratedPassword) {
  // Checks that if the password field has the value generated by Chrome
  // (HTMLInputElement::shouldRevealPassword will be true), copying the field
  // should be available.
  const char* body_content = "<input type='password' id='password'></input>";
  SetBodyContent(body_content);

  HTMLInputElement& element =
      ToHTMLInputElement(*GetDocument().getElementById("password"));

  const String kPasswordValue = "secret";
  element.focus();
  element.setValue(kPasswordValue);
  element.SetSelectionRange(0, kPasswordValue.length());

  Editor& editor = GetDocument().GetFrame()->GetEditor();
  EXPECT_FALSE(editor.CanCopy());

  element.SetShouldRevealPassword(true);
  EXPECT_TRUE(editor.CanCopy());
}

TEST_F(EditorTest, CopyVisibleSelection) {
  const char* body_content = "<input id=hiding value=HEY>";
  SetBodyContent(body_content);

  HTMLInputElement& text_control =
      ToHTMLInputElement(*GetDocument().getElementById("hiding"));
  text_control.select();

  ExecuteCopy();

  const String copied = SystemClipboard::GetInstance().ReadPlainText();
  EXPECT_EQ("HEY", copied);
}

TEST_F(EditorTest, DontCopyHiddenSelections) {
  const char* body_content =
      "<input type=checkbox id=checkbox>"
      "<input id=hiding value=HEY>";
  SetBodyContent(body_content);

  HTMLInputElement& text_control =
      ToHTMLInputElement(*GetDocument().getElementById("hiding"));
  text_control.select();

  HTMLInputElement& checkbox =
      ToHTMLInputElement(*GetDocument().getElementById("checkbox"));
  checkbox.focus();

  ExecuteCopy();

  const String copied = SystemClipboard::GetInstance().ReadPlainText();
  EXPECT_TRUE(copied.IsEmpty()) << copied << " was copied.";
}

}  // namespace blink