File: shortcut_creator_win_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 5,726 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/shortcuts/shortcut_creator.h"

#include <algorithm>
#include <functional>

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/hash/hash.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_path_override.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/test/test_shortcut_win.h"
#include "base/win/scoped_com_initializer.h"
#include "chrome/browser/profiles/profile_shortcut_manager_win.h"
#include "chrome/browser/shortcuts/platform_util_win.h"
#include "chrome/browser/shortcuts/shortcut_creation_test_support.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "url/gurl.h"

namespace shortcuts {

namespace {

constexpr char kShortcutFileName[] = "Test Name.lnk";
constexpr char16_t kShortcutName[] = u"Test Name";
constexpr char16_t kShortcutWithInvalidCharsName[] = u"Test|Name";

struct ImageDesc {
  int size;
  SkColor color;
};

gfx::ImageFamily CreateImageFamily(std::vector<ImageDesc> images) {
  gfx::ImageFamily image_family;
  for (const ImageDesc& image_desc : images) {
    image_family.Add(gfx::test::CreateImage(image_desc.size, image_desc.color));
  }
  return image_family;
}

base::FilePath GetUserDesktopPath() {
  return base::PathService::CheckedGet(base::DIR_USER_DESKTOP);
}

class ShortcutCreatorWinTest : public testing::Test {
 protected:
  const GURL kUrl = GURL("https://example.com/test?query=1s&basdf");
  void SetUp() override {
    ASSERT_TRUE(default_profile_path_.CreateUniqueTempDir());
  }
  void VerifyShortcut(const base::FilePath& current_shortcut_path) const;
  const base::FilePath& default_profile_path() const {
    return default_profile_path_.GetPath();
  }

 private:
  base::ScopedTempDir default_profile_path_;
  base::ScopedPathOverride desktop_override_{base::DIR_USER_DESKTOP};
  base::win::ScopedCOMInitializer com_initializer_;
  base::test::SingleThreadTaskEnvironment task_environment_;
};

void ShortcutCreatorWinTest::VerifyShortcut(
    const base::FilePath& current_shortcut_path) const {
  base::win::ShortcutProperties properties;
  properties.set_target(GetChromeProxyPath());
  std::string url_hash =
      base::NumberToString(base::PersistentHash(kUrl.spec()));
  base::FilePath target_path = default_profile_path()
                                   .Append(kWebShortcutsIconDirName)
                                   .AppendASCII(url_hash)
                                   .Append(L"shortcut.ico");
  properties.set_icon(target_path, 0);
  properties.set_arguments(base::StrCat(
      {profiles::internal::CreateProfileShortcutFlags(default_profile_path(),
                                                      /*incognito=*/false),
       L" --", base::ASCIIToWide(switches::kIgnoreProfileDirectoryIfNotExists),
       L" ", base::ASCIIToWide(kUrl.spec())}));
  const base::FilePath expected_shortcut_path =
      GetUserDesktopPath().AppendASCII(kShortcutFileName);
  ASSERT_EQ(expected_shortcut_path, current_shortcut_path);
  ASSERT_TRUE(base::PathExists(current_shortcut_path));
  base::win::ValidateShortcut(current_shortcut_path, properties);
  // TODO(b/333024272): Verify images in .ico file are correct.

  // Verify that the shortcut matchers work correctly as well.
  EXPECT_THAT(expected_shortcut_path, IsShortcutForUrl(kUrl));
  EXPECT_THAT(expected_shortcut_path,
              IsShortcutForProfile(default_profile_path()));
  EXPECT_THAT(expected_shortcut_path, IsShortcutWithTitle(kShortcutName));
}

TEST_F(ShortcutCreatorWinTest, ShortcutCreated) {
  gfx::ImageFamily images =
      CreateImageFamily({{.size = 16, .color = SK_ColorCYAN},
                         {.size = 32, .color = SK_ColorBLUE},
                         {.size = 256, .color = SK_ColorGREEN}});

  ShortcutMetadata metadata(default_profile_path(), kUrl, kShortcutName,
                            std::move(images));
  base::test::TestFuture<const base::FilePath&, ShortcutCreatorResult> future;

  CreateShortcutOnUserDesktop(std::move(metadata), future.GetCallback());
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(ShortcutCreatorResult::kSuccess,
            future.Get<ShortcutCreatorResult>());

  VerifyShortcut(future.Get<base::FilePath>());
}

TEST_F(ShortcutCreatorWinTest, ShortcutWithInvalidCharsInNameCreated) {
  gfx::ImageFamily images =
      CreateImageFamily({{.size = 16, .color = SK_ColorCYAN},
                         {.size = 32, .color = SK_ColorBLUE},
                         {.size = 256, .color = SK_ColorGREEN}});

  // The '|' in kShortcutWithInvalidCharsName should be replaced with ' ' and
  // match kShortcutFileName.
  ShortcutMetadata metadata(default_profile_path(), kUrl,
                            kShortcutWithInvalidCharsName, std::move(images));
  base::test::TestFuture<const base::FilePath&, ShortcutCreatorResult> future;

  CreateShortcutOnUserDesktop(std::move(metadata), future.GetCallback());
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(ShortcutCreatorResult::kSuccess,
            future.Get<ShortcutCreatorResult>());

  VerifyShortcut(future.Get<base::FilePath>());
}

}  // namespace

}  // namespace shortcuts