1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/sync/sync_ui_util_mac.h"
6
7#import <Cocoa/Cocoa.h>
8
9#include "base/memory/scoped_nsobject.h"
10#include "chrome/app/chrome_command_ids.h"
11#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12#include "grit/generated_resources.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "ui/base/l10n/l10n_util_mac.h"
15
16namespace {
17
18class SyncStatusUIHelperMacTest : public CocoaTest {
19};
20
21TEST_F(SyncStatusUIHelperMacTest, UpdateSyncItem) {
22  scoped_nsobject<NSMenuItem> syncMenuItem(
23      [[NSMenuItem alloc] initWithTitle:@""
24                                 action:@selector(commandDispatch)
25                          keyEquivalent:@""]);
26  [syncMenuItem setTag:IDC_SYNC_BOOKMARKS];
27
28  NSString* bookmarksSynced =
29    l10n_util::GetNSStringWithFixup(IDS_SYNC_MENU_SYNCED_LABEL);
30  NSString* bookmarkSyncError =
31    l10n_util::GetNSStringWithFixup(IDS_SYNC_MENU_SYNC_ERROR_LABEL);
32  NSString* startSync =
33    l10n_util::GetNSStringWithFixup(IDS_SYNC_START_SYNC_BUTTON_LABEL);
34
35  [syncMenuItem setTitle:@""];
36  [syncMenuItem setHidden:NO];
37
38  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, NO,
39                                        sync_ui_util::PRE_SYNCED);
40  EXPECT_TRUE([[syncMenuItem title] isEqualTo:startSync]);
41  EXPECT_TRUE([syncMenuItem isHidden]);
42
43  [syncMenuItem setTitle:@""];
44  [syncMenuItem setHidden:YES];
45  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, YES,
46                                        sync_ui_util::SYNC_ERROR);
47  EXPECT_TRUE([[syncMenuItem title] isEqualTo:bookmarkSyncError]);
48  EXPECT_FALSE([syncMenuItem isHidden]);
49
50  [syncMenuItem setTitle:@""];
51  [syncMenuItem setHidden:NO];
52  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, NO,
53                                        sync_ui_util::SYNCED);
54  EXPECT_TRUE([[syncMenuItem title] isEqualTo:bookmarksSynced]);
55  EXPECT_TRUE([syncMenuItem isHidden]);
56}
57
58TEST_F(SyncStatusUIHelperMacTest, UpdateSyncItemWithSeparator) {
59  scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
60  NSMenuItem* syncMenuItem =
61      [menu addItemWithTitle:@""
62                      action:@selector(commandDispatch)
63               keyEquivalent:@""];
64  [syncMenuItem setTag:IDC_SYNC_BOOKMARKS];
65  NSMenuItem* followingSeparator = [NSMenuItem separatorItem];
66  [menu addItem:followingSeparator];
67
68  const sync_ui_util::MessageType kStatus = sync_ui_util::PRE_SYNCED;
69
70  [syncMenuItem setHidden:NO];
71  [followingSeparator setHidden:NO];
72  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, NO, kStatus);
73  EXPECT_FALSE([followingSeparator isEnabled]);
74  EXPECT_TRUE([syncMenuItem isHidden]);
75  EXPECT_TRUE([followingSeparator isHidden]);
76
77  [syncMenuItem setHidden:YES];
78  [followingSeparator setHidden:YES];
79  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, YES, kStatus);
80  EXPECT_FALSE([followingSeparator isEnabled]);
81  EXPECT_FALSE([syncMenuItem isHidden]);
82  EXPECT_FALSE([followingSeparator isHidden]);
83}
84
85TEST_F(SyncStatusUIHelperMacTest, UpdateSyncItemWithNonSeparator) {
86  scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
87  NSMenuItem* syncMenuItem =
88      [menu addItemWithTitle:@""
89                      action:@selector(commandDispatch)
90               keyEquivalent:@""];
91  [syncMenuItem setTag:IDC_SYNC_BOOKMARKS];
92  NSMenuItem* followingNonSeparator =
93      [menu addItemWithTitle:@""
94                      action:@selector(commandDispatch)
95               keyEquivalent:@""];
96
97  const sync_ui_util::MessageType kStatus = sync_ui_util::PRE_SYNCED;
98
99  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, NO, kStatus);
100  EXPECT_TRUE([followingNonSeparator isEnabled]);
101  EXPECT_FALSE([followingNonSeparator isHidden]);
102
103  sync_ui_util::UpdateSyncItemForStatus(syncMenuItem, YES, kStatus);
104  EXPECT_TRUE([followingNonSeparator isEnabled]);
105  EXPECT_FALSE([followingNonSeparator isHidden]);
106}
107
108}  // namespace
109