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_main_container.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h"
9 #import "chrome/browser/ui/cocoa/autofill/autofill_section_view.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 #import "ui/base/test/ui_cocoa_test_helper.h"
16 class AutofillMainContainerTest : public ui::CocoaTest {
18 virtual void SetUp() {
24 container_.reset([[AutofillMainContainer alloc] initWithDelegate:
26 [[test_window() contentView] setSubviews:@[ [container_ view] ]];
30 base::scoped_nsobject<AutofillMainContainer> container_;
31 testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
36 TEST_VIEW(AutofillMainContainerTest, [container_ view])
38 TEST_F(AutofillMainContainerTest, SubViews) {
39 bool hasButtons = false;
40 bool hasButtonStripImage = false;
41 bool hasTextView = false;
42 bool hasDetailsContainer = false;
43 bool hasCheckbox = false;
44 bool hasCheckboxTooltip = false;
45 bool hasNotificationContainer = false;
47 // Should have account chooser, button strip, and details section.
48 EXPECT_EQ(7U, [[[container_ view] subviews] count]);
49 for (NSView* view in [[container_ view] subviews]) {
50 NSArray* subviews = [view subviews];
51 if ([view isKindOfClass:[NSScrollView class]]) {
52 hasDetailsContainer = true;
53 } else if (view == [container_ saveInChromeTooltipForTesting]) {
54 hasCheckboxTooltip = true;
55 } else if ([subviews count] == 2) {
57 [[subviews objectAtIndex:0] isKindOfClass:[NSButton class]]);
59 [[subviews objectAtIndex:1] isKindOfClass:[NSButton class]]);
61 } else if ([view isKindOfClass:[NSImageView class]]) {
62 if (view == [container_ buttonStripImageForTesting])
63 hasButtonStripImage = true;
65 EXPECT_TRUE(false); // Unknown image view; should not be reachable.
66 } else if ([view isKindOfClass:[NSTextView class]]) {
68 } else if ([view isKindOfClass:[NSButton class]] &&
69 view == [container_ saveInChromeCheckboxForTesting]) {
72 // Assume by default this is the notification area view.
73 // There is no way to easily verify that with more detail.
74 hasNotificationContainer = true;
78 EXPECT_TRUE(hasButtons);
79 EXPECT_TRUE(hasButtonStripImage);
80 EXPECT_TRUE(hasTextView);
81 EXPECT_TRUE(hasDetailsContainer);
82 EXPECT_TRUE(hasNotificationContainer);
83 EXPECT_TRUE(hasCheckbox);
84 EXPECT_TRUE(hasCheckboxTooltip);
87 // Ensure the default state of the "Save in Chrome" checkbox is controlled by
89 TEST_F(AutofillMainContainerTest, SaveDetailsDefaultsFromDelegate) {
90 using namespace testing;
91 EXPECT_CALL(delegate_, ShouldOfferToSaveInChrome()).Times(2)
92 .WillRepeatedly(Return(true));
93 EXPECT_CALL(delegate_,ShouldSaveInChrome()).Times(2)
94 .WillOnce(Return(false))
95 .WillOnce(Return(true));
98 EXPECT_FALSE([container_ saveDetailsLocally]);
101 EXPECT_TRUE([container_ saveDetailsLocally]);
104 TEST_F(AutofillMainContainerTest, SaveInChromeCheckboxVisibility) {
105 using namespace testing;
107 // Tests that the checkbox is only visible if the delegate allows it.
108 EXPECT_CALL(delegate_, ShouldOfferToSaveInChrome()).Times(2)
109 .WillOnce(Return(false))
110 .WillOnce(Return(true));
113 NSButton* checkbox = [container_ saveInChromeCheckboxForTesting];
114 NSImageView* tooltip = [container_ saveInChromeTooltipForTesting];
115 ASSERT_TRUE(checkbox);
116 ASSERT_TRUE(tooltip);
118 EXPECT_TRUE([checkbox isHidden]);
119 EXPECT_TRUE([tooltip isHidden]);
120 [container_ modelChanged];
121 EXPECT_FALSE([checkbox isHidden]);
122 EXPECT_FALSE([tooltip isHidden]);