Chromium Code Reviews| 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 ShellIntegration::ShortcutInfo& shortcut_info) | 38 const ShellIntegration::ShortcutInfo& shortcut_info) |
| 37 : WebAppShortcutCreator(base::FilePath("/fake/path"), shortcut_info, | 39 : WebAppShortcutCreator(base::FilePath("/fake/path"), |
| 38 UTF8ToUTF16("fake.cfbundleidentifier")) { | 40 shortcut_info, |
| 41 kFakeChromeBundleId) { | |
| 39 } | 42 } |
| 40 | 43 |
| 41 MOCK_CONST_METHOD0(GetDestinationPath, base::FilePath()); | 44 MOCK_CONST_METHOD0(GetDestinationPath, base::FilePath()); |
| 42 MOCK_CONST_METHOD1(RevealGeneratedBundleInFinder, | 45 MOCK_CONST_METHOD0(RevealAppShimInFinder, void()); |
| 43 void (const base::FilePath&)); | 46 MOCK_CONST_METHOD1(GetAppBundleById, base::FilePath(const std::string&)); |
| 44 }; | 47 }; |
| 45 | 48 |
| 46 ShellIntegration::ShortcutInfo GetShortcutInfo() { | 49 ShellIntegration::ShortcutInfo GetShortcutInfo() { |
| 47 ShellIntegration::ShortcutInfo info; | 50 ShellIntegration::ShortcutInfo info; |
| 48 info.extension_id = "extension_id"; | 51 info.extension_id = "extensionid"; |
| 49 info.extension_path = base::FilePath("/fake/extension/path"); | 52 info.extension_path = base::FilePath("/fake/extension/path"); |
| 50 info.title = ASCIIToUTF16("Shortcut Title"); | 53 info.title = ASCIIToUTF16("Shortcut Title"); |
| 51 info.url = GURL("http://example.com/"); | 54 info.url = GURL("http://example.com/"); |
| 52 info.profile_path = base::FilePath("Default"); | 55 info.profile_path = base::FilePath("Profile 1"); |
| 53 info.profile_name = "profile name"; | 56 info.profile_name = "profile name"; |
| 54 return info; | 57 return info; |
| 55 } | 58 } |
| 56 | 59 |
| 57 } // namespace | 60 } // namespace |
| 58 | 61 |
| 59 namespace web_app { | 62 namespace web_app { |
| 60 | 63 |
| 61 TEST(WebAppShortcutCreatorTest, CreateShortcut) { | 64 TEST(WebAppShortcutCreatorTest, CreateShortcut) { |
| 62 base::ScopedTempDir scoped_temp_dir; | 65 base::ScopedTempDir scoped_temp_dir; |
| 63 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 66 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 64 | 67 |
| 65 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 68 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 66 | 69 |
| 67 base::FilePath dst_folder = scoped_temp_dir.path(); | 70 base::FilePath dst_folder = scoped_temp_dir.path(); |
| 68 base::FilePath dst_path = dst_folder.Append( | 71 base::FilePath dst_path = dst_folder.Append( |
| 69 info.profile_path.value() + " " + info.extension_id + ".app"); | 72 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 70 | 73 |
| 71 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); | 74 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); |
| 72 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 75 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 73 .WillRepeatedly(Return(dst_folder)); | 76 .WillRepeatedly(Return(dst_folder)); |
| 74 EXPECT_CALL(shortcut_creator, RevealGeneratedBundleInFinder(dst_path)); | 77 EXPECT_CALL(shortcut_creator, RevealAppShimInFinder()); |
| 75 | 78 |
| 76 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 79 EXPECT_TRUE(shortcut_creator.CreateShortcut()); |
| 77 EXPECT_TRUE(file_util::PathExists(dst_path)); | 80 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 78 EXPECT_EQ(dst_path.value(), shortcut_creator.GetShortcutPath().value()); | 81 EXPECT_EQ(dst_path.BaseName(), shortcut_creator.GetShortcutName()); |
| 79 | 82 |
| 80 base::FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); | 83 base::FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); |
| 81 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: | 84 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: |
| 82 base::mac::FilePathToNSString(plist_path)]; | 85 base::mac::FilePathToNSString(plist_path)]; |
| 83 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), | 86 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), |
| 84 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); | 87 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); |
| 85 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), | 88 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), |
| 86 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); | 89 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); |
| 87 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), | 90 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), |
| 88 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); | 91 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); |
| 89 | 92 |
| 90 // Make sure all values in the plist are actually filled in. | 93 // Make sure all values in the plist are actually filled in. |
| 91 for (id key in plist) { | 94 for (id key in plist) { |
| 92 id value = [plist valueForKey:key]; | 95 id value = [plist valueForKey:key]; |
| 93 if (!base::mac::ObjCCast<NSString>(value)) | 96 if (!base::mac::ObjCCast<NSString>(value)) |
| 94 continue; | 97 continue; |
| 95 | 98 |
| 96 EXPECT_EQ([value rangeOfString:@"@APP_"].location, NSNotFound) | 99 EXPECT_EQ([value rangeOfString:@"@APP_"].location, NSNotFound) |
| 97 << [key UTF8String] << ":" << [value UTF8String]; | 100 << [key UTF8String] << ":" << [value UTF8String]; |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 104 TEST(WebAppShortcutCreatorTest, UpdateShortcuts) { | |
| 105 base::ScopedTempDir scoped_temp_dir; | |
| 106 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | |
| 107 base::ScopedTempDir scoped_temp_dir_other; | |
| 108 EXPECT_TRUE(scoped_temp_dir_other.CreateUniqueTempDir()); | |
| 109 | |
| 110 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | |
| 111 | |
| 112 base::FilePath app_name( | |
| 113 info.profile_path.value() + " " + info.extension_id + ".app"); | |
| 114 base::FilePath dst_folder = scoped_temp_dir.path(); | |
| 115 base::FilePath other_folder = scoped_temp_dir_other.path(); | |
| 116 | |
| 117 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); | |
| 118 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | |
| 119 .WillRepeatedly(Return(dst_folder)); | |
| 120 | |
| 121 std::string expected_bundle_id = kFakeChromeBundleId; | |
| 122 expected_bundle_id += ".app.Profile-1." + info.extension_id; | |
| 123 EXPECT_CALL(shortcut_creator, GetAppBundleById(expected_bundle_id)) | |
|
tapted
2013/06/18 06:54:59
Can you test the failure case, with GetAppBundleBy
jackhou1
2013/06/18 08:32:40
Done.
| |
| 124 .WillRepeatedly(Return(other_folder.Append(app_name))); | |
| 125 | |
| 126 shortcut_creator.BuildShortcut(other_folder.Append(app_name)); | |
| 127 | |
| 128 EXPECT_TRUE(file_util::Delete( | |
| 129 other_folder.Append(app_name).Append("Contents"), true)); | |
| 130 | |
| 131 EXPECT_TRUE(shortcut_creator.UpdateShortcuts()); | |
| 132 EXPECT_FALSE(file_util::PathExists(dst_folder.Append(app_name))); | |
| 133 EXPECT_TRUE(file_util::PathExists( | |
| 134 other_folder.Append(app_name).Append("Contents"))); | |
| 135 } | |
| 136 | |
| 101 TEST(WebAppShortcutCreatorTest, RunShortcut) { | 137 TEST(WebAppShortcutCreatorTest, RunShortcut) { |
| 102 base::ScopedTempDir scoped_temp_dir; | 138 base::ScopedTempDir scoped_temp_dir; |
| 103 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 139 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 104 | 140 |
| 105 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 141 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 106 | 142 |
| 107 base::FilePath dst_folder = scoped_temp_dir.path(); | 143 base::FilePath dst_folder = scoped_temp_dir.path(); |
| 108 base::FilePath dst_path = dst_folder.Append( | 144 base::FilePath dst_path = dst_folder.Append( |
| 109 info.profile_path.value() + " " + info.extension_id + ".app"); | 145 info.profile_path.value() + " " + info.extension_id + ".app"); |
| 110 | 146 |
| 111 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); | 147 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); |
| 112 EXPECT_CALL(shortcut_creator, GetDestinationPath()) | 148 EXPECT_CALL(shortcut_creator, GetDestinationPath()) |
| 113 .WillRepeatedly(Return(dst_folder)); | 149 .WillRepeatedly(Return(dst_folder)); |
| 114 EXPECT_CALL(shortcut_creator, RevealGeneratedBundleInFinder(dst_path)); | |
| 115 | 150 |
| 116 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 151 EXPECT_TRUE(shortcut_creator.CreateShortcut()); |
| 117 EXPECT_TRUE(file_util::PathExists(dst_path)); | 152 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 118 | 153 |
| 119 ssize_t status = getxattr( | 154 ssize_t status = getxattr( |
| 120 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); | 155 dst_path.value().c_str(), "com.apple.quarantine", NULL, 0, 0, 0); |
| 121 EXPECT_EQ(-1, status); | 156 EXPECT_EQ(-1, status); |
| 122 EXPECT_EQ(ENOATTR, errno); | 157 EXPECT_EQ(ENOATTR, errno); |
| 123 } | 158 } |
| 124 | 159 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 152 dst_path.Append("Contents").Append("Resources").Append("app.icns"); | 187 dst_path.Append("Contents").Append("Resources").Append("app.icns"); |
| 153 | 188 |
| 154 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: | 189 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: |
| 155 base::mac::FilePathToNSString(icon_path)]); | 190 base::mac::FilePathToNSString(icon_path)]); |
| 156 EXPECT_TRUE(image); | 191 EXPECT_TRUE(image); |
| 157 EXPECT_EQ(product_logo.Width(), [image size].width); | 192 EXPECT_EQ(product_logo.Width(), [image size].width); |
| 158 EXPECT_EQ(product_logo.Height(), [image size].height); | 193 EXPECT_EQ(product_logo.Height(), [image size].height); |
| 159 } | 194 } |
| 160 | 195 |
| 161 } // namespace web_app | 196 } // namespace web_app |
| OLD | NEW |