| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/web_applications/web_app_mac.h" | 5 #import "chrome/browser/web_applications/web_app_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include <sys/xattr.h> | 9 #include <sys/xattr.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 26 | 26 |
| 27 using ::testing::_; | 27 using ::testing::_; |
| 28 using ::testing::Return; | 28 using ::testing::Return; |
| 29 using ::testing::NiceMock; | 29 using ::testing::NiceMock; |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const char kFakeChromeBundleId[] = "fake.cfbundleidentifier"; |
| 34 |
| 33 class WebAppShortcutCreatorMock : public web_app::WebAppShortcutCreator { | 35 class WebAppShortcutCreatorMock : public web_app::WebAppShortcutCreator { |
| 34 public: | 36 public: |
| 35 explicit WebAppShortcutCreatorMock( | 37 explicit WebAppShortcutCreatorMock( |
| 36 const base::FilePath& app_data_path, | 38 const base::FilePath& app_data_path, |
| 37 const ShellIntegration::ShortcutInfo& shortcut_info) | 39 const ShellIntegration::ShortcutInfo& shortcut_info) |
| 38 : WebAppShortcutCreator(app_data_path, shortcut_info, | 40 : WebAppShortcutCreator(app_data_path, |
| 39 UTF8ToUTF16("fake.cfbundleidentifier")) { | 41 shortcut_info, |
| 42 kFakeChromeBundleId) { |
| 40 } | 43 } |
| 41 | 44 |
| 42 MOCK_CONST_METHOD0(GetDestinationPath, base::FilePath()); | 45 MOCK_CONST_METHOD0(GetDestinationPath, base::FilePath()); |
| 43 MOCK_CONST_METHOD1(RevealGeneratedBundleInFinder, | 46 MOCK_CONST_METHOD1(GetAppBundleById, |
| 44 void (const base::FilePath&)); | 47 base::FilePath(const std::string& bundle_id)); |
| 48 MOCK_CONST_METHOD0(RevealAppShimInFinder, void()); |
| 45 }; | 49 }; |
| 46 | 50 |
| 47 ShellIntegration::ShortcutInfo GetShortcutInfo() { | 51 ShellIntegration::ShortcutInfo GetShortcutInfo() { |
| 48 ShellIntegration::ShortcutInfo info; | 52 ShellIntegration::ShortcutInfo info; |
| 49 info.extension_id = "extension_id"; | 53 info.extension_id = "extensionid"; |
| 50 info.extension_path = base::FilePath("/fake/extension/path"); | 54 info.extension_path = base::FilePath("/fake/extension/path"); |
| 51 info.title = ASCIIToUTF16("Shortcut Title"); | 55 info.title = ASCIIToUTF16("Shortcut Title"); |
| 52 info.url = GURL("http://example.com/"); | 56 info.url = GURL("http://example.com/"); |
| 53 info.profile_path = base::FilePath("Default"); | 57 info.profile_path = base::FilePath("Profile 1"); |
| 54 info.profile_name = "profile name"; | 58 info.profile_name = "profile name"; |
| 55 return info; | 59 return info; |
| 56 } | 60 } |
| 57 | 61 |
| 58 } // namespace | 62 } // namespace |
| 59 | 63 |
| 60 namespace web_app { | 64 namespace web_app { |
| 61 | 65 |
| 62 TEST(WebAppShortcutCreatorTest, CreateShortcut) { | 66 TEST(WebAppShortcutCreatorTest, CreateShortcuts) { |
| 63 base::ScopedTempDir temp_app_data_path; | 67 base::ScopedTempDir temp_app_data_path; |
| 64 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 68 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 65 base::ScopedTempDir temp_dst_dir; | 69 base::ScopedTempDir temp_dst_dir; |
| 66 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 70 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 67 | 71 |
| 68 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 72 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 69 | 73 |
| 70 | |
| 71 base::FilePath app_name( | 74 base::FilePath app_name( |
| 72 info.profile_path.value() + " " + info.extension_id + ".app"); | 75 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 73 base::FilePath app_in_app_data_path_path = | 76 base::FilePath app_in_app_data_path_path = |
| 74 temp_app_data_path.path().Append(app_name); | 77 temp_app_data_path.path().Append(app_name); |
| 75 base::FilePath dst_folder = temp_dst_dir.path(); | 78 base::FilePath dst_folder = temp_dst_dir.path(); |
| 76 base::FilePath dst_path = dst_folder.Append(app_name); | 79 base::FilePath dst_path = dst_folder.Append(app_name); |
| 77 | 80 |
| 78 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( | 81 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 79 temp_app_data_path.path(), info); | 82 temp_app_data_path.path(), info); |
| 80 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 83 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 81 .WillRepeatedly(Return(dst_folder)); | 84 .WillRepeatedly(Return(dst_folder)); |
| 82 EXPECT_CALL(shortcut_creator, RevealGeneratedBundleInFinder(dst_path)); | 85 EXPECT_CALL(shortcut_creator, RevealAppShimInFinder()); |
| 83 | 86 |
| 84 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 87 EXPECT_TRUE(shortcut_creator.CreateShortcuts()); |
| 85 EXPECT_TRUE(file_util::PathExists(app_in_app_data_path_path)); | 88 EXPECT_TRUE(file_util::PathExists(app_in_app_data_path_path)); |
| 86 EXPECT_TRUE(file_util::PathExists(dst_path)); | 89 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 87 EXPECT_EQ(dst_path.value(), shortcut_creator.GetShortcutPath().value()); | 90 EXPECT_EQ(dst_path.BaseName(), shortcut_creator.GetShortcutName()); |
| 88 | 91 |
| 89 base::FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); | 92 base::FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); |
| 90 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: | 93 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: |
| 91 base::mac::FilePathToNSString(plist_path)]; | 94 base::mac::FilePathToNSString(plist_path)]; |
| 92 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), | 95 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), |
| 93 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); | 96 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); |
| 94 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), | 97 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), |
| 95 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); | 98 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); |
| 96 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), | 99 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), |
| 97 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); | 100 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); |
| 98 | 101 |
| 99 // Make sure all values in the plist are actually filled in. | 102 // Make sure all values in the plist are actually filled in. |
| 100 for (id key in plist) { | 103 for (id key in plist) { |
| 101 id value = [plist valueForKey:key]; | 104 id value = [plist valueForKey:key]; |
| 102 if (!base::mac::ObjCCast<NSString>(value)) | 105 if (!base::mac::ObjCCast<NSString>(value)) |
| 103 continue; | 106 continue; |
| 104 | 107 |
| 105 EXPECT_EQ([value rangeOfString:@"@APP_"].location, NSNotFound) | 108 EXPECT_EQ([value rangeOfString:@"@APP_"].location, NSNotFound) |
| 106 << [key UTF8String] << ":" << [value UTF8String]; | 109 << [key UTF8String] << ":" << [value UTF8String]; |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 | 112 |
| 113 TEST(WebAppShortcutCreatorTest, UpdateShortcuts) { |
| 114 base::ScopedTempDir temp_app_data_path; |
| 115 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 116 base::ScopedTempDir temp_dst_dir; |
| 117 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 118 base::ScopedTempDir temp_dst_dir_other; |
| 119 EXPECT_TRUE(temp_dst_dir_other.CreateUniqueTempDir()); |
| 120 |
| 121 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 122 |
| 123 base::FilePath app_name( |
| 124 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 125 base::FilePath app_in_app_data_path_path = |
| 126 temp_app_data_path.path().Append(app_name); |
| 127 base::FilePath dst_folder = temp_dst_dir.path(); |
| 128 base::FilePath other_folder = temp_dst_dir_other.path(); |
| 129 |
| 130 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 131 temp_app_data_path.path(), info); |
| 132 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 133 .WillRepeatedly(Return(dst_folder)); |
| 134 |
| 135 std::string expected_bundle_id = kFakeChromeBundleId; |
| 136 expected_bundle_id += ".app.Profile-1-" + info.extension_id; |
| 137 EXPECT_CALL(shortcut_creator, GetAppBundleById(expected_bundle_id)) |
| 138 .WillOnce(Return(other_folder.Append(app_name))); |
| 139 |
| 140 shortcut_creator.BuildShortcut(other_folder.Append(app_name)); |
| 141 |
| 142 EXPECT_TRUE(file_util::Delete( |
| 143 other_folder.Append(app_name).Append("Contents"), true)); |
| 144 |
| 145 EXPECT_TRUE(shortcut_creator.UpdateShortcuts()); |
| 146 EXPECT_FALSE(file_util::PathExists(dst_folder.Append(app_name))); |
| 147 EXPECT_TRUE(file_util::PathExists( |
| 148 other_folder.Append(app_name).Append("Contents"))); |
| 149 |
| 150 // Also test case where GetAppBundleById fails. |
| 151 EXPECT_CALL(shortcut_creator, GetAppBundleById(expected_bundle_id)) |
| 152 .WillOnce(Return(base::FilePath())); |
| 153 |
| 154 shortcut_creator.BuildShortcut(other_folder.Append(app_name)); |
| 155 |
| 156 EXPECT_TRUE(file_util::Delete( |
| 157 other_folder.Append(app_name).Append("Contents"), true)); |
| 158 |
| 159 EXPECT_FALSE(shortcut_creator.UpdateShortcuts()); |
| 160 EXPECT_FALSE(file_util::PathExists(dst_folder.Append(app_name))); |
| 161 EXPECT_FALSE(file_util::PathExists( |
| 162 other_folder.Append(app_name).Append("Contents"))); |
| 163 } |
| 164 |
| 110 TEST(WebAppShortcutCreatorTest, CreateAppListShortcut) { | 165 TEST(WebAppShortcutCreatorTest, CreateAppListShortcut) { |
| 111 base::ScopedTempDir scoped_temp_dir; | 166 base::ScopedTempDir temp_dst_dir; |
| 112 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 167 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 113 base::FilePath dst_folder = scoped_temp_dir.path(); | 168 |
| 114 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 169 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 115 | 170 |
| 171 base::FilePath dst_folder = temp_dst_dir.path(); |
| 172 |
| 116 // With an empty |profile_name|, the shortcut path should not have the profile | 173 // With an empty |profile_name|, the shortcut path should not have the profile |
| 117 // directory prepended to the extension id on the app bundle name. | 174 // directory prepended to the extension id on the app bundle name. |
| 118 info.profile_name.clear(); | 175 info.profile_name.clear(); |
| 119 base::FilePath dst_path = dst_folder.Append(info.extension_id + ".app"); | 176 base::FilePath dst_path = dst_folder.Append(info.extension_id + ".app"); |
| 120 | 177 |
| 121 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(base::FilePath(), info); | 178 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(base::FilePath(), info); |
| 122 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 179 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 123 .WillRepeatedly(Return(dst_folder)); | 180 .WillRepeatedly(Return(dst_folder)); |
| 124 EXPECT_EQ(dst_path.value(), shortcut_creator.GetShortcutPath().value()); | 181 EXPECT_EQ(dst_path.BaseName(), shortcut_creator.GetShortcutName()); |
| 125 } | 182 } |
| 126 | 183 |
| 127 TEST(WebAppShortcutCreatorTest, RunShortcut) { | 184 TEST(WebAppShortcutCreatorTest, RunShortcut) { |
| 128 base::ScopedTempDir temp_app_data_path; | 185 base::ScopedTempDir temp_app_data_path; |
| 129 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 186 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 130 base::ScopedTempDir temp_dst_dir; | 187 base::ScopedTempDir temp_dst_dir; |
| 131 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 188 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 132 | 189 |
| 133 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 190 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 134 | 191 |
| 135 base::FilePath dst_folder = temp_dst_dir.path(); | 192 base::FilePath dst_folder = temp_dst_dir.path(); |
| 136 base::FilePath dst_path = dst_folder.Append( | 193 base::FilePath dst_path = dst_folder.Append( |
| 137 info.profile_path.value() + " " + info.extension_id + ".app"); | 194 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 138 | 195 |
| 139 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( | 196 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 140 temp_app_data_path.path(), info); | 197 temp_app_data_path.path(), info); |
| 141 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 198 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 142 .WillRepeatedly(Return(dst_folder)); | 199 .WillRepeatedly(Return(dst_folder)); |
| 143 EXPECT_CALL(shortcut_creator, RevealGeneratedBundleInFinder(dst_path)); | 200 EXPECT_CALL(shortcut_creator, RevealAppShimInFinder()); |
| 144 | 201 |
| 145 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 202 EXPECT_TRUE(shortcut_creator.CreateShortcuts()); |
| 146 EXPECT_TRUE(file_util::PathExists(dst_path)); | 203 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 147 | 204 |
| 148 ssize_t status = getxattr( | 205 ssize_t status = getxattr( |
| 149 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); | 206 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); |
| 150 EXPECT_EQ(-1, status); | 207 EXPECT_EQ(-1, status); |
| 151 EXPECT_EQ(ENOATTR, errno); | 208 EXPECT_EQ(ENOATTR, errno); |
| 152 } | 209 } |
| 153 | 210 |
| 154 TEST(WebAppShortcutCreatorTest, CreateFailure) { | 211 TEST(WebAppShortcutCreatorTest, CreateFailure) { |
| 155 base::ScopedTempDir temp_app_data_path; | 212 base::ScopedTempDir temp_app_data_path; |
| 156 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 213 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 157 base::ScopedTempDir temp_dst_dir; | 214 base::ScopedTempDir temp_dst_dir; |
| 158 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 215 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 159 | 216 |
| 160 base::FilePath non_existent_path = | 217 base::FilePath non_existent_path = |
| 161 temp_dst_dir.path().Append("not-existent").Append("name.app"); | 218 temp_dst_dir.path().Append("not-existent").Append("name.app"); |
| 162 | 219 |
| 163 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( | 220 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 164 temp_app_data_path.path(), GetShortcutInfo()); | 221 temp_app_data_path.path(), GetShortcutInfo()); |
| 165 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 222 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 166 .WillRepeatedly(Return(non_existent_path)); | 223 .WillRepeatedly(Return(non_existent_path)); |
| 167 EXPECT_FALSE(shortcut_creator.CreateShortcut()); | 224 EXPECT_FALSE(shortcut_creator.CreateShortcuts()); |
| 168 } | 225 } |
| 169 | 226 |
| 170 TEST(WebAppShortcutCreatorTest, UpdateIcon) { | 227 TEST(WebAppShortcutCreatorTest, UpdateIcon) { |
| 171 base::ScopedTempDir temp_app_data_path; | 228 base::ScopedTempDir temp_app_data_path; |
| 172 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 229 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 173 base::ScopedTempDir temp_dst_dir; | 230 base::ScopedTempDir temp_dst_dir; |
| 174 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 231 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 175 base::FilePath dst_path = temp_dst_dir.path(); | 232 base::FilePath dst_path = temp_dst_dir.path(); |
| 176 | 233 |
| 177 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 234 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 178 gfx::Image product_logo = | 235 gfx::Image product_logo = |
| 179 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 236 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 180 IDR_PRODUCT_LOGO_32); | 237 IDR_PRODUCT_LOGO_32); |
| 181 info.favicon.Add(product_logo); | 238 info.favicon.Add(product_logo); |
| 182 WebAppShortcutCreatorMock shortcut_creator(temp_app_data_path.path(), info); | 239 WebAppShortcutCreatorMock shortcut_creator(temp_app_data_path.path(), info); |
| 183 | 240 |
| 184 ASSERT_TRUE(shortcut_creator.UpdateIcon(dst_path)); | 241 ASSERT_TRUE(shortcut_creator.UpdateIcon(dst_path)); |
| 185 base::FilePath icon_path = | 242 base::FilePath icon_path = |
| 186 dst_path.Append("Contents").Append("Resources").Append("app.icns"); | 243 dst_path.Append("Contents").Append("Resources").Append("app.icns"); |
| 187 | 244 |
| 188 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: | 245 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: |
| 189 base::mac::FilePathToNSString(icon_path)]); | 246 base::mac::FilePathToNSString(icon_path)]); |
| 190 EXPECT_TRUE(image); | 247 EXPECT_TRUE(image); |
| 191 EXPECT_EQ(product_logo.Width(), [image size].width); | 248 EXPECT_EQ(product_logo.Width(), [image size].width); |
| 192 EXPECT_EQ(product_logo.Height(), [image size].height); | 249 EXPECT_EQ(product_logo.Height(), [image size].height); |
| 193 } | 250 } |
| 194 | 251 |
| 195 } // namespace web_app | 252 } // namespace web_app |
| OLD | NEW |