File: web_drag_dest_mac_unittest.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (5)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "content/browser/web_contents/web_drag_dest_mac.h"

#include <AppKit/AppKit.h>

#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/stack_allocated.h"
#include "base/strings/sys_string_conversions.h"
#import "content/browser/web_contents/web_contents_view_mac.h"
#include "content/public/common/drop_data.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "ui/base/clipboard/clipboard_util_mac.h"
#import "ui/base/test/cocoa_helper.h"

class WebDragDestTest : public content::RenderViewHostImplTestHarness {
 public:
  void SetUp() override {
    content::RenderViewHostImplTestHarness::SetUp();
    drag_dest_ = [[WebDragDest alloc] initWithWebContentsImpl:contents()];
  }

  STACK_ALLOCATED_IGNORE("https://crbug.com/1424190")
  base::apple::ScopedNSAutoreleasePool pool_;
  WebDragDest* __strong drag_dest_;
};

// Make sure nothing leaks.
TEST_F(WebDragDestTest, Init) {
  EXPECT_TRUE(drag_dest_);
}

TEST_F(WebDragDestTest, EndDragSync) {
  // When there is no pending drop operation, calling `endDrag` should just
  // synchronously call any passed callback. If it didn't, the test would fail
  // from `run_loop` timing out.
  base::RunLoop run_loop;
  [drag_dest_ endDrag:run_loop.QuitClosure()];
  run_loop.Run();
}

TEST_F(WebDragDestTest, EndDragAsync) {
  // When there is a pending drop operation, calling `endDrag` should store the
  // passed callback and call it after `completeDropAsync` resolves.
  [drag_dest_ setDropInProgressForTesting];

  base::RunLoop run_loop;
  [drag_dest_ endDrag:run_loop.QuitClosure()];

  EXPECT_FALSE(run_loop.AnyQuitCalled());
  EXPECT_TRUE([drag_dest_ dropInProgressForTesting]);

  [drag_dest_
      completeDropAsync:std::nullopt
            withContext:content::DropContext(content::DropData(), gfx::PointF(),
                                             gfx::PointF(), 0,
                                             main_test_rfh()
                                                 ->GetRenderWidgetHost()
                                                 ->GetWeakPtr())];

  run_loop.Run();
  EXPECT_FALSE([drag_dest_ dropInProgressForTesting]);
}

TEST_F(WebDragDestTest, Data) {
  scoped_refptr<ui::UniquePasteboard> pboard = new ui::UniquePasteboard;
  NSString* html_string = @"<html><body><b>hi there</b></body></html>";
  NSString* text_string = @"hi there";
  [pboard->get() setString:@"http://www.google.com"
                   forType:NSPasteboardTypeURL];
  [pboard->get() setString:html_string forType:NSPasteboardTypeHTML];
  [pboard->get() setString:text_string forType:NSPasteboardTypeString];

  content::DropData data =
      content::PopulateDropDataFromPasteboard(pboard->get());

  EXPECT_EQ(data.url.spec(), "http://www.google.com/");
  EXPECT_EQ(base::SysNSStringToUTF16(text_string), data.text);
  EXPECT_EQ(base::SysNSStringToUTF16(html_string), data.html);
}

TEST_F(WebDragDestTest, FilePermissions) {
  // Set up a temporary directory
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  // Define the path for the new temporary file
  base::FilePath temp_file_path = temp_dir.GetPath().AppendASCII("test_file");

  // Set a custom umask (disables write permission for group and others) and
  // store the original umask
  const mode_t kCustomUmask = 0022;
  mode_t original_umask = umask(kCustomUmask);

  // Create the temporary file
  base::File file(temp_file_path,
                  base::File::FLAG_CREATE | base::File::FLAG_WRITE);
  ASSERT_TRUE(file.IsValid());

  // Invoke the function to set file permissions
  content::WebContentsViewMac::SetReadWritePermissionsForFileForTests(file);

  // Restore the original umask
  umask(original_umask);

  // Retrieve and verify the file permissions
  int mode = 0;
  ASSERT_TRUE(base::GetPosixFilePermissions(temp_file_path, &mode));

  // Calculate the expected permissions considering the umask
  int expected_permissions = 0666 & ~kCustomUmask;

  EXPECT_EQ(mode, expected_permissions);
}