| 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, RunShortcut) { | 165 TEST(WebAppShortcutCreatorTest, RunShortcut) { |
| 111 base::ScopedTempDir temp_app_data_path; | 166 base::ScopedTempDir temp_app_data_path; |
| 112 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 167 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 113 base::ScopedTempDir temp_dst_dir; | 168 base::ScopedTempDir temp_dst_dir; |
| 114 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 169 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 115 | 170 |
| 116 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 171 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 117 | 172 |
| 118 base::FilePath dst_folder = temp_dst_dir.path(); | 173 base::FilePath dst_folder = temp_dst_dir.path(); |
| 119 base::FilePath dst_path = dst_folder.Append( | 174 base::FilePath dst_path = dst_folder.Append( |
| 120 info.profile_path.value() + " " + info.extension_id + ".app"); | 175 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 121 | 176 |
| 122 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( | 177 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 123 temp_app_data_path.path(), info); | 178 temp_app_data_path.path(), info); |
| 124 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 179 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 125 .WillRepeatedly(Return(dst_folder)); | 180 .WillRepeatedly(Return(dst_folder)); |
| 126 EXPECT_CALL(shortcut_creator, RevealGeneratedBundleInFinder(dst_path)); | 181 EXPECT_CALL(shortcut_creator, RevealAppShimInFinder()); |
| 127 | 182 |
| 128 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 183 EXPECT_TRUE(shortcut_creator.CreateShortcuts()); |
| 129 EXPECT_TRUE(file_util::PathExists(dst_path)); | 184 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 130 | 185 |
| 131 ssize_t status = getxattr( | 186 ssize_t status = getxattr( |
| 132 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); | 187 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); |
| 133 EXPECT_EQ(-1, status); | 188 EXPECT_EQ(-1, status); |
| 134 EXPECT_EQ(ENOATTR, errno); | 189 EXPECT_EQ(ENOATTR, errno); |
| 135 } | 190 } |
| 136 | 191 |
| 137 TEST(WebAppShortcutCreatorTest, CreateFailure) { | 192 TEST(WebAppShortcutCreatorTest, CreateFailure) { |
| 138 base::ScopedTempDir temp_app_data_path; | 193 base::ScopedTempDir temp_app_data_path; |
| 139 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 194 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 140 base::ScopedTempDir temp_dst_dir; | 195 base::ScopedTempDir temp_dst_dir; |
| 141 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 196 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 142 | 197 |
| 143 base::FilePath non_existent_path = | 198 base::FilePath non_existent_path = |
| 144 temp_dst_dir.path().Append("not-existent").Append("name.app"); | 199 temp_dst_dir.path().Append("not-existent").Append("name.app"); |
| 145 | 200 |
| 146 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( | 201 NiceMock<WebAppShortcutCreatorMock> shortcut_creator( |
| 147 temp_app_data_path.path(), GetShortcutInfo()); | 202 temp_app_data_path.path(), GetShortcutInfo()); |
| 148 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 203 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 149 .WillRepeatedly(Return(non_existent_path)); | 204 .WillRepeatedly(Return(non_existent_path)); |
| 150 EXPECT_FALSE(shortcut_creator.CreateShortcut()); | 205 EXPECT_FALSE(shortcut_creator.CreateShortcuts()); |
| 151 } | 206 } |
| 152 | 207 |
| 153 TEST(WebAppShortcutCreatorTest, UpdateIcon) { | 208 TEST(WebAppShortcutCreatorTest, UpdateIcon) { |
| 154 base::ScopedTempDir temp_app_data_path; | 209 base::ScopedTempDir temp_app_data_path; |
| 155 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); | 210 EXPECT_TRUE(temp_app_data_path.CreateUniqueTempDir()); |
| 156 base::ScopedTempDir temp_dst_dir; | 211 base::ScopedTempDir temp_dst_dir; |
| 157 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); | 212 EXPECT_TRUE(temp_dst_dir.CreateUniqueTempDir()); |
| 158 base::FilePath dst_path = temp_dst_dir.path(); | 213 base::FilePath dst_path = temp_dst_dir.path(); |
| 159 | 214 |
| 160 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 215 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 161 gfx::Image product_logo = | 216 gfx::Image product_logo = |
| 162 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 217 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 163 IDR_PRODUCT_LOGO_32); | 218 IDR_PRODUCT_LOGO_32); |
| 164 info.favicon.Add(product_logo); | 219 info.favicon.Add(product_logo); |
| 165 WebAppShortcutCreatorMock shortcut_creator(temp_app_data_path.path(), info); | 220 WebAppShortcutCreatorMock shortcut_creator(temp_app_data_path.path(), info); |
| 166 | 221 |
| 167 ASSERT_TRUE(shortcut_creator.UpdateIcon(dst_path)); | 222 ASSERT_TRUE(shortcut_creator.UpdateIcon(dst_path)); |
| 168 base::FilePath icon_path = | 223 base::FilePath icon_path = |
| 169 dst_path.Append("Contents").Append("Resources").Append("app.icns"); | 224 dst_path.Append("Contents").Append("Resources").Append("app.icns"); |
| 170 | 225 |
| 171 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: | 226 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: |
| 172 base::mac::FilePathToNSString(icon_path)]); | 227 base::mac::FilePathToNSString(icon_path)]); |
| 173 EXPECT_TRUE(image); | 228 EXPECT_TRUE(image); |
| 174 EXPECT_EQ(product_logo.Width(), [image size].width); | 229 EXPECT_EQ(product_logo.Width(), [image size].width); |
| 175 EXPECT_EQ(product_logo.Height(), [image size].height); | 230 EXPECT_EQ(product_logo.Height(), [image size].height); |
| 176 } | 231 } |
| 177 | 232 |
| 178 } // namespace web_app | 233 } // namespace web_app |
| OLD | NEW |