Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_main_container_unittest.mm
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.
4
5 #import "chrome/browser/ui/cocoa/autofill/autofill_main_container.h"
6
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/gfx/test/ui_cocoa_test_helper.h"
13
14 namespace {
15
16 class AutofillMainContainerTest : public ui::CocoaTest {
17  public:
18   virtual void SetUp() {
19     CocoaTest::SetUp();
20     RebuildView();
21   }
22
23   void RebuildView() {
24     container_.reset([[AutofillMainContainer alloc] initWithDelegate:
25                          &delegate_]);
26     [[test_window() contentView] setSubviews:@[ [container_ view] ]];
27   }
28
29  protected:
30   base::scoped_nsobject<AutofillMainContainer> container_;
31   testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
32 };
33
34 }  // namespace
35
36 TEST_VIEW(AutofillMainContainerTest, [container_ view])
37
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;
46
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) {
56       EXPECT_TRUE(
57           [[subviews objectAtIndex:0] isKindOfClass:[NSButton class]]);
58       EXPECT_TRUE(
59           [[subviews objectAtIndex:1] isKindOfClass:[NSButton class]]);
60       hasButtons = true;
61     } else if ([view isKindOfClass:[NSImageView class]]) {
62       if (view == [container_ buttonStripImageForTesting])
63         hasButtonStripImage = true;
64       else
65         EXPECT_TRUE(false);  // Unknown image view; should not be reachable.
66     } else if ([view isKindOfClass:[NSTextView class]]) {
67       hasTextView = true;
68     } else if ([view isKindOfClass:[NSButton class]] &&
69                view == [container_ saveInChromeCheckboxForTesting]) {
70       hasCheckbox = true;
71     } else {
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;
75     }
76   }
77
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);
85 }
86
87 // Ensure the default state of the "Save in Chrome" checkbox is controlled by
88 // the delegate.
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));
96
97   RebuildView();
98   EXPECT_FALSE([container_ saveDetailsLocally]);
99
100   RebuildView();
101   EXPECT_TRUE([container_ saveDetailsLocally]);
102 }
103
104 TEST_F(AutofillMainContainerTest, SaveInChromeCheckboxVisibility) {
105   using namespace testing;
106
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));
111
112   RebuildView();
113   NSButton* checkbox = [container_ saveInChromeCheckboxForTesting];
114   NSImageView* tooltip = [container_ saveInChromeTooltipForTesting];
115   ASSERT_TRUE(checkbox);
116   ASSERT_TRUE(tooltip);
117
118   EXPECT_TRUE([checkbox isHidden]);
119   EXPECT_TRUE([tooltip isHidden]);
120   [container_ modelChanged];
121   EXPECT_FALSE([checkbox isHidden]);
122   EXPECT_FALSE([tooltip isHidden]);
123 }