diff options
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.h | 7 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 69 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller_unittest.mm | 75 |
3 files changed, 151 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/browser_window_controller.h b/chrome/browser/cocoa/browser_window_controller.h index 49ebe34..de3451d 100644 --- a/chrome/browser/cocoa/browser_window_controller.h +++ b/chrome/browser/cocoa/browser_window_controller.h @@ -19,6 +19,7 @@ #import "chrome/browser/cocoa/bookmark_bubble_controller.h" #import "chrome/browser/cocoa/browser_command_executor.h" #import "chrome/browser/cocoa/view_resizer.h" +#include "chrome/browser/sync/sync_status_ui_helper.h" #import "third_party/GTM/AppKit/GTMTheme.h" class Browser; @@ -184,6 +185,12 @@ class TabStripModelObserverBridge; // Return a point suitable for the topLeft for a bookmark bubble. - (NSPoint)topLeftForBubble; +// Updates a bookmark sync UI item (expected to be a menu item). This is +// called every time the menu containing the sync UI item is displayed. +- (void)updateSyncItem:(id)syncItem + syncEnabled:(BOOL)syncEnabled + status:(SyncStatusUIHelper::MessageType)status; + @end // BrowserWindowController(TestingAPI) #endif // CHROME_BROWSER_COCOA_BROWSER_WINDOW_CONTROLLER_H_ diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index f310c07..73781e4 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -4,6 +4,7 @@ #include <Carbon/Carbon.h> +#include "app/l10n_util_mac.h" #include "base/mac_util.h" #include "base/scoped_nsdisable_screen_updates.h" #import "base/scoped_nsobject.h" @@ -39,8 +40,10 @@ #import "chrome/browser/cocoa/tab_view.h" #import "chrome/browser/cocoa/toolbar_controller.h" #import "chrome/browser/browser_theme_provider.h" +#include "chrome/browser/sync/sync_status_ui_helper.h" #include "chrome/common/pref_service.h" #import "chrome/browser/cocoa/background_gradient_view.h" +#include "grit/generated_resources.h" #include "grit/theme_resources.h" #import "third_party/GTM/AppKit/GTMTheme.h" @@ -579,6 +582,19 @@ willPositionSheet:(NSWindow*)sheet case IDC_FULLSCREEN: enable &= [self supportsFullscreen]; break; + case IDC_SYNC_BOOKMARKS: + { + Profile* profile = browser_->profile()->GetOriginalProfile(); + ProfileSyncService* syncService = profile->GetProfileSyncService(); + // TODO(timsteele): Need a ui helper method to just get the type + // without needing labels. + string16 label, link; + SyncStatusUIHelper::MessageType status = + SyncStatusUIHelper::GetLabels(syncService, &label, &link); + enable &= (syncService != NULL); + [self updateSyncItem:item syncEnabled:enable status:status]; + } + break; default: // Special handling for the contents of the Text Encoding submenu. On // Mac OS, instead of enabling/disabling the top-level menu item, we @@ -599,6 +615,59 @@ willPositionSheet:(NSWindow*)sheet return enable; } +// TODO(akalin): We need to add a menu item to the main Chrome menu (near +// "Clear browsing data..."). However, this is tricky as no browsers may +// actually be open. Refactor the sync UI code so that it doesn't depend +// on a browser being present. + +- (void)updateSyncItem:(id)syncItem + syncEnabled:(BOOL)syncEnabled + status:(SyncStatusUIHelper::MessageType)status { + DCHECK([syncItem isKindOfClass:[NSMenuItem class]]); + NSMenuItem* syncMenuItem = (NSMenuItem*)syncItem; + // Look for a separator immediately after the menu item. + NSMenuItem* followingSeparator = nil; + NSMenu* menu = [syncItem menu]; + if (menu) { + NSInteger syncItemIndex = [menu indexOfItem:syncMenuItem]; + DCHECK_NE(syncItemIndex, -1); + if ((syncItemIndex + 1) < [menu numberOfItems]) { + NSMenuItem* menuItem = [menu itemAtIndex:(syncItemIndex + 1)]; + if ([menuItem isSeparatorItem]) { + followingSeparator = menuItem; + } + } + } + + // TODO(akalin): consolidate this code with the equivalent Windows code in + // chrome/browser/views/toolbar_view.cc. + int titleId; + switch (status) { + case SyncStatusUIHelper::SYNCED: + titleId = IDS_SYNC_MENU_BOOKMARKS_SYNCED_LABEL; + break; + case SyncStatusUIHelper::SYNC_ERROR: + titleId = IDS_SYNC_MENU_BOOKMARK_SYNC_ERROR_LABEL; + break; + case SyncStatusUIHelper::PRE_SYNCED: + titleId = IDS_SYNC_START_SYNC_BUTTON_LABEL; + break; + default: + NOTREACHED(); + // Needed to prevent release-mode warnings. + titleId = IDS_SYNC_START_SYNC_BUTTON_LABEL; + break; + } + NSString* title = l10n_util::GetNSStringWithFixup(titleId); + [syncMenuItem setTitle:title]; + + // If we don't have a sync service, hide any sync-related menu + // items. However, sync_menu_item is enabled/disabled outside of this + // function so we don't touch it here, and separators are always disabled. + [syncMenuItem setHidden:!syncEnabled]; + [followingSeparator setHidden:!syncEnabled]; +} + // Called when the user picks a menu or toolbar item when this window is key. // Calls through to the browser object to execute the command. This assumes that // the command is supported and doesn't check, otherwise it would have been diff --git a/chrome/browser/cocoa/browser_window_controller_unittest.mm b/chrome/browser/cocoa/browser_window_controller_unittest.mm index b5f504e..e2b7451 100644 --- a/chrome/browser/cocoa/browser_window_controller_unittest.mm +++ b/chrome/browser/cocoa/browser_window_controller_unittest.mm @@ -2,19 +2,23 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "app/l10n_util_mac.h" #include "base/scoped_nsobject.h" #include "base/scoped_nsautorelease_pool.h" #include "base/scoped_ptr.h" +#include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/cocoa/browser_test_helper.h" #include "chrome/browser/cocoa/browser_window_controller.h" #include "chrome/browser/cocoa/cocoa_test_helper.h" #include "chrome/browser/cocoa/find_bar_bridge.h" +#include "chrome/browser/sync/sync_status_ui_helper.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "chrome/test/testing_browser_process.h" #include "chrome/test/testing_profile.h" +#include "grit/generated_resources.h" @interface BrowserWindowController (JustForTesting) // Already defined in BWC. @@ -429,6 +433,77 @@ TEST_F(BrowserWindowControllerTest, TestFindBarOnTop) { EXPECT_GT(findBar_index, bookmark_index); } +TEST_F(BrowserWindowControllerTest, TestSyncMenuItem) { + scoped_nsobject<NSMenuItem> syncMenuItem( + [[NSMenuItem alloc] initWithTitle:@"" + action:@selector(commandDispatch) + keyEquivalent:@""]); + [syncMenuItem setTag:IDC_SYNC_BOOKMARKS]; + + NSString* bookmarksSynced = + l10n_util::GetNSStringWithFixup(IDS_SYNC_MENU_BOOKMARKS_SYNCED_LABEL); + NSString* bookmarkSyncError = + l10n_util::GetNSStringWithFixup(IDS_SYNC_MENU_BOOKMARK_SYNC_ERROR_LABEL); + NSString* startSync = + l10n_util::GetNSStringWithFixup(IDS_SYNC_START_SYNC_BUTTON_LABEL); + + [syncMenuItem setTitle:@""]; + [syncMenuItem setHidden:NO]; + [controller_ updateSyncItem:syncMenuItem + syncEnabled:NO + status:SyncStatusUIHelper::PRE_SYNCED]; + EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSync]); + EXPECT_TRUE([syncMenuItem isHidden]); + + [syncMenuItem setTitle:@""]; + [syncMenuItem setHidden:YES]; + [controller_ updateSyncItem:syncMenuItem + syncEnabled:YES + status:SyncStatusUIHelper::SYNC_ERROR]; + EXPECT_TRUE([[syncMenuItem title] isEqualTo:bookmarkSyncError]); + EXPECT_FALSE([syncMenuItem isHidden]); + + [syncMenuItem setTitle:@""]; + [syncMenuItem setHidden:NO]; + [controller_ updateSyncItem:syncMenuItem + syncEnabled:NO + status:SyncStatusUIHelper::SYNCED]; + EXPECT_TRUE([[syncMenuItem title] isEqualTo:bookmarksSynced]); + EXPECT_TRUE([syncMenuItem isHidden]); +} + +TEST_F(BrowserWindowControllerTest, TestSyncMenuItemWithSeparator) { + scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]); + NSMenuItem* syncMenuItem = + [menu addItemWithTitle:@"" + action:@selector(commandDispatch) + keyEquivalent:@""]; + [syncMenuItem setTag:IDC_SYNC_BOOKMARKS]; + NSMenuItem* following_separator = [NSMenuItem separatorItem]; + [menu addItem:following_separator]; + + const SyncStatusUIHelper::MessageType kStatus = + SyncStatusUIHelper::PRE_SYNCED; + + [syncMenuItem setHidden:NO]; + [following_separator setHidden:NO]; + [controller_ updateSyncItem:syncMenuItem + syncEnabled:NO + status:kStatus]; + EXPECT_FALSE([following_separator isEnabled]); + EXPECT_TRUE([syncMenuItem isHidden]); + EXPECT_TRUE([following_separator isHidden]); + + [syncMenuItem setHidden:YES]; + [following_separator setHidden:YES]; + [controller_ updateSyncItem:syncMenuItem + syncEnabled:YES + status:kStatus]; + EXPECT_FALSE([following_separator isEnabled]); + EXPECT_FALSE([syncMenuItem isHidden]); + EXPECT_FALSE([following_separator isHidden]); +} + @interface BrowserWindowControllerFakeFullscreen : BrowserWindowController { @private // We release the window ourselves, so we don't have to rely on the unittest |