| 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 #include "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 "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/memory/scoped_nsobject.h" | 11 #include "base/memory/scoped_nsobject.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/common/mac/app_mode_common.h" | 15 #include "chrome/common/mac/app_mode_common.h" |
| 16 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #import "testing/gtest_mac.h" | 19 #import "testing/gtest_mac.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 22 | 22 |
| 23 using ::testing::_; | 23 using ::testing::_; |
| 24 using ::testing::Return; | 24 using ::testing::Return; |
| 25 using ::testing::NiceMock; | 25 using ::testing::NiceMock; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 class WebAppShortcutCreatorMock : public web_app::WebAppShortcutCreator { | 29 class WebAppShortcutCreatorMock : public web_app::WebAppShortcutCreator { |
| 30 public: | 30 public: |
| 31 explicit WebAppShortcutCreatorMock( | 31 explicit WebAppShortcutCreatorMock( |
| 32 const ShellIntegration::ShortcutInfo& shortcut_info) | 32 const ShellIntegration::ShortcutInfo& shortcut_info) |
| 33 : WebAppShortcutCreator(FilePath(), shortcut_info) { | 33 : WebAppShortcutCreator(FilePath(), shortcut_info, |
| 34 UTF8ToUTF16("fake.cfbundleidentifier")) { |
| 34 } | 35 } |
| 35 | 36 |
| 36 MOCK_CONST_METHOD1(GetDestinationPath, FilePath(const FilePath&)); | 37 MOCK_CONST_METHOD1(GetDestinationPath, FilePath(const FilePath&)); |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 ShellIntegration::ShortcutInfo GetShortcutInfo() { | 40 ShellIntegration::ShortcutInfo GetShortcutInfo() { |
| 40 ShellIntegration::ShortcutInfo info; | 41 ShellIntegration::ShortcutInfo info; |
| 41 info.extension_id = "extension_id"; | 42 info.extension_id = "extension_id"; |
| 42 info.title = ASCIIToUTF16("Shortcut Title"); | 43 info.title = ASCIIToUTF16("Shortcut Title"); |
| 43 info.url = GURL("http://example.com/"); | 44 info.url = GURL("http://example.com/"); |
| 44 return info; | 45 return info; |
| 45 } | 46 } |
| 46 | 47 |
| 47 } // namespace | 48 } // namespace |
| 48 | 49 |
| 49 namespace web_app { | 50 namespace web_app { |
| 50 | 51 |
| 51 // This test currently fails because the Mac app loader isn't built yet. | 52 // This test is disabled for the following reasons: |
| 52 TEST(WebAppShortcutCreatorTest, FAILS_CreateShortcut) { | 53 // * The plist still isn't filled in correctly. |
| 54 // * WebAppShortcutCreator::CreateShortcut() opens a Finder window which it |
| 55 // shouldn't be doing when run from a unit test. |
| 56 TEST(WebAppShortcutCreatorTest, DISABLED_CreateShortcut) { |
| 53 ScopedTempDir scoped_temp_dir; | 57 ScopedTempDir scoped_temp_dir; |
| 54 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 58 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 55 FilePath dst_path = scoped_temp_dir.path().Append("a.app"); | 59 FilePath dst_path = scoped_temp_dir.path().Append("a.app"); |
| 56 | 60 |
| 57 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 61 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 58 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); | 62 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(info); |
| 59 EXPECT_CALL(shortcut_creator, GetDestinationPath(_)) | 63 EXPECT_CALL(shortcut_creator, GetDestinationPath(_)) |
| 60 .WillRepeatedly(Return(dst_path)); | 64 .WillRepeatedly(Return(dst_path)); |
| 61 EXPECT_TRUE(shortcut_creator.CreateShortcut()); | 65 EXPECT_TRUE(shortcut_creator.CreateShortcut()); |
| 62 EXPECT_TRUE(file_util::PathExists(dst_path)); | 66 EXPECT_TRUE(file_util::PathExists(dst_path)); |
| 63 | 67 |
| 64 FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); | 68 FilePath plist_path = dst_path.Append("Contents").Append("Info.plist"); |
| 65 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: | 69 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: |
| 66 base::mac::FilePathToNSString(plist_path)]; | 70 base::mac::FilePathToNSString(plist_path)]; |
| 67 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), | 71 EXPECT_NSEQ(base::SysUTF8ToNSString(info.extension_id), |
| 68 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); | 72 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); |
| 69 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), | 73 EXPECT_NSEQ(base::SysUTF16ToNSString(info.title), |
| 70 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); | 74 [plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); |
| 71 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), | 75 EXPECT_NSEQ(base::SysUTF8ToNSString(info.url.spec()), |
| 72 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); | 76 [plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); |
| 73 | 77 |
| 74 // Make sure all values in the plist are actually filled in. | 78 // Make sure all values in the plist are actually filled in. |
| 75 for (NSString* value in [plist allValues]) | 79 for (id key in plist) { |
| 76 EXPECT_FALSE([value hasPrefix:@"@APP_"]); | 80 id value = [plist valueForKey:key]; |
| 81 if (!base::mac::ObjCCast<NSString>(value)) |
| 82 continue; |
| 83 |
| 84 EXPECT_EQ([value rangeOfString:@"@APP_"].location, NSNotFound) |
| 85 << [key UTF8String] << ":" << [value UTF8String]; |
| 86 } |
| 77 } | 87 } |
| 78 | 88 |
| 79 TEST(WebAppShortcutCreatorTest, CreateFailure) { | 89 TEST(WebAppShortcutCreatorTest, DISABLED_CreateFailure) { |
| 80 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(GetShortcutInfo()); | 90 NiceMock<WebAppShortcutCreatorMock> shortcut_creator(GetShortcutInfo()); |
| 81 EXPECT_CALL(shortcut_creator, GetDestinationPath(_)) | 91 EXPECT_CALL(shortcut_creator, GetDestinationPath(_)) |
| 82 .WillRepeatedly(Return(FilePath("/non-existant/path/"))); | 92 .WillRepeatedly(Return(FilePath("/non-existant/path/"))); |
| 83 EXPECT_FALSE(shortcut_creator.CreateShortcut()); | 93 EXPECT_FALSE(shortcut_creator.CreateShortcut()); |
| 84 } | 94 } |
| 85 | 95 |
| 86 TEST(WebAppShortcutCreatorTest, UpdateIcon) { | 96 TEST(WebAppShortcutCreatorTest, UpdateIcon) { |
| 87 ScopedTempDir scoped_temp_dir; | 97 ScopedTempDir scoped_temp_dir; |
| 88 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 98 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 89 FilePath dst_path = scoped_temp_dir.path(); | 99 FilePath dst_path = scoped_temp_dir.path(); |
| 90 | 100 |
| 91 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); | 101 ShellIntegration::ShortcutInfo info = GetShortcutInfo(); |
| 92 info.favicon = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 102 info.favicon = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 93 IDR_PRODUCT_LOGO_32).ToSkBitmap(); | 103 IDR_PRODUCT_LOGO_32).ToSkBitmap(); |
| 94 WebAppShortcutCreatorMock shortcut_creator(info); | 104 WebAppShortcutCreatorMock shortcut_creator(info); |
| 95 | 105 |
| 96 shortcut_creator.UpdateIcon(dst_path); | 106 shortcut_creator.UpdateIcon(dst_path); |
| 97 FilePath icon_path = | 107 FilePath icon_path = |
| 98 dst_path.Append("Contents").Append("Resources").Append("app.icns"); | 108 dst_path.Append("Contents").Append("Resources").Append("app.icns"); |
| 99 | 109 |
| 100 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: | 110 scoped_nsobject<NSImage> image([[NSImage alloc] initWithContentsOfFile: |
| 101 base::mac::FilePathToNSString(icon_path)]); | 111 base::mac::FilePathToNSString(icon_path)]); |
| 102 EXPECT_TRUE(image); | 112 EXPECT_TRUE(image); |
| 103 EXPECT_EQ(info.favicon.width(), [image size].width); | 113 EXPECT_EQ(info.favicon.width(), [image size].width); |
| 104 EXPECT_EQ(info.favicon.height(), [image size].height); | 114 EXPECT_EQ(info.favicon.height(), [image size].height); |
| 105 } | 115 } |
| 106 | 116 |
| 107 } // namespace web_app | 117 } // namespace web_app |
| OLD | NEW |