1 // Copyright (c) 2013 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.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_account_chooser.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h"
11 #import "chrome/browser/ui/cocoa/menu_button.h"
12 #include "testing/gtest_mac.h"
13 #include "testing/platform_test.h"
14 #import "ui/base/cocoa/controls/hyperlink_button_cell.h"
15 #include "ui/base/models/simple_menu_model.h"
16 #import "ui/gfx/test/ui_cocoa_test_helper.h"
20 class AutofillAccountChooserTest : public ui::CocoaTest {
22 AutofillAccountChooserTest() {
23 NSRect frame = NSMakeRect(0, 0, 500, 200);
24 view_.reset([[AutofillAccountChooser alloc] initWithFrame:frame
25 delegate:&delegate_]);
26 [[test_window() contentView] addSubview:view_];
30 base::scoped_nsobject<AutofillAccountChooser> view_;
31 testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
34 DISALLOW_COPY_AND_ASSIGN(AutofillAccountChooserTest);
37 class MenuDelegate : public ui::SimpleMenuModel::Delegate {
39 bool IsCommandIdChecked(int command_id) const override { return false; }
41 bool IsCommandIdEnabled(int command_id) const override { return true; }
43 bool GetAcceleratorForCommandId(int command_id,
44 ui::Accelerator* accelerator) override {
48 void ExecuteCommand(int command_id, int event_flags) override {}
53 TEST_VIEW(AutofillAccountChooserTest, view_);
55 // Make sure account chooser has all required subviews
56 TEST_F(AutofillAccountChooserTest, SubViews) {
59 bool hasPopup = false;
61 for (id view in [view_ subviews]) {
62 if ([view isKindOfClass:[NSImageView class]]) {
64 } else if ([view isKindOfClass:[MenuButton class]]) {
66 } else if ([view isKindOfClass:[NSButton class]]) {
67 if ([[view cell] isKindOfClass:[HyperlinkButtonCell class]])
74 EXPECT_TRUE(hasPopup);
77 // Validate that the account menu is properly populated.
78 TEST_F(AutofillAccountChooserTest, PopulatesMenu) {
79 using namespace testing;
80 MenuDelegate delegate;
81 ui::SimpleMenuModel model(&delegate);
82 model.AddItem(1, base::ASCIIToUTF16("one"));
83 model.AddItem(2, base::ASCIIToUTF16("two"));
85 EXPECT_CALL(delegate_, MenuModelForAccountChooser())
86 .WillOnce(Return(&model));
89 MenuButton* button = nil;
90 for (id view in [view_ subviews]) {
91 if ([view isKindOfClass:[MenuButton class]]) {
97 NSArray* items = [[button attachedMenu] itemArray];
98 EXPECT_EQ(3U, [items count]);
99 EXPECT_NSEQ(@"", [[items objectAtIndex:0] title]);
100 EXPECT_NSEQ(@"one", [[items objectAtIndex:1] title]);
101 EXPECT_NSEQ(@"two", [[items objectAtIndex:2] title]);