Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_manage_view_controller_unittest.mm
1 // Copyright 2014 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/passwords/manage_passwords_bubble_manage_view_controller.h"
6
7 #include "base/mac/foundation_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #import "chrome/browser/ui/cocoa/passwords/manage_password_item_view_controller.h"
10 #include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h"
11 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
12 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/gtest_mac.h"
15
16 @interface ManagePasswordsBubbleManageViewTestDelegate
17     : NSObject<ManagePasswordsBubbleContentViewDelegate> {
18   BOOL dismissed_;
19 }
20 @property(readonly) BOOL dismissed;
21 @end
22
23 @implementation ManagePasswordsBubbleManageViewTestDelegate
24
25 @synthesize dismissed = dismissed_;
26
27 - (void)viewShouldDismiss {
28   dismissed_ = YES;
29 }
30
31 @end
32
33 namespace {
34
35 class ManagePasswordsBubbleManageViewControllerTest
36     : public ManagePasswordsControllerTest {
37  public:
38   ManagePasswordsBubbleManageViewControllerTest() : controller_(nil) {}
39
40   virtual void SetUp() OVERRIDE {
41     ManagePasswordsControllerTest::SetUp();
42     delegate_.reset(
43         [[ManagePasswordsBubbleManageViewTestDelegate alloc] init]);
44     ui_controller()->SetState(password_manager::ui::MANAGE_STATE);
45   }
46
47   ManagePasswordsBubbleManageViewTestDelegate* delegate() {
48     return delegate_.get();
49   }
50
51   ManagePasswordsBubbleManageViewController* controller() {
52     if (!controller_) {
53       controller_.reset([[ManagePasswordsBubbleManageViewController alloc]
54           initWithModel:model()
55                delegate:delegate()]);
56       [controller_ loadView];
57     }
58     return controller_.get();
59   }
60
61  private:
62   base::scoped_nsobject<ManagePasswordsBubbleManageViewController> controller_;
63   base::scoped_nsobject<ManagePasswordsBubbleManageViewTestDelegate> delegate_;
64   DISALLOW_COPY_AND_ASSIGN(ManagePasswordsBubbleManageViewControllerTest);
65 };
66
67 TEST_F(ManagePasswordsBubbleManageViewControllerTest,
68        ShouldDismissWhenDoneClicked) {
69   [controller().doneButton performClick:nil];
70   EXPECT_TRUE([delegate() dismissed]);
71 }
72
73 TEST_F(ManagePasswordsBubbleManageViewControllerTest,
74        ShouldOpenPasswordsWhenManageClicked) {
75   [controller().manageButton performClick:nil];
76   EXPECT_TRUE([delegate() dismissed]);
77   EXPECT_TRUE(ui_controller()->navigated_to_settings_page());
78 }
79
80 TEST_F(ManagePasswordsBubbleManageViewControllerTest,
81        ShouldShowNoPasswordsWhenNoPasswordsExistForSite) {
82   EXPECT_TRUE(model()->best_matches().empty());
83   EXPECT_EQ([NoPasswordsView class], [controller().contentView class]);
84 }
85
86 TEST_F(ManagePasswordsBubbleManageViewControllerTest,
87        ShouldShowAllPasswordItemsWhenPasswordsExistForSite) {
88   // Add a few password entries.
89   autofill::ConstPasswordFormMap map;
90   autofill::PasswordForm form1;
91   form1.username_value = base::ASCIIToUTF16("username1");
92   form1.password_value = base::ASCIIToUTF16("password1");
93   map[base::ASCIIToUTF16("username1")] = &form1;
94
95   autofill::PasswordForm form2;
96   form2.username_value = base::ASCIIToUTF16("username2");
97   form2.password_value = base::ASCIIToUTF16("password2");
98   map[base::ASCIIToUTF16("username2")] = &form2;
99
100   // Add the entries to the model.
101   ui_controller()->SetPasswordFormMap(map);
102   model()->set_state(password_manager::ui::MANAGE_STATE);
103
104   // Check the view state.
105   EXPECT_FALSE(model()->best_matches().empty());
106   EXPECT_EQ([PasswordItemListView class], [controller().contentView class]);
107   NSArray* items = base::mac::ObjCCastStrict<PasswordItemListView>(
108       controller().contentView).itemViews;
109   EXPECT_EQ(2U, [items count]);
110
111   // Check the entry items.
112   for (ManagePasswordItemViewController* item in items) {
113     ManagePasswordItemManageView* itemContent =
114         base::mac::ObjCCastStrict<ManagePasswordItemManageView>(
115             item.contentView);
116     NSString* username = [itemContent.usernameField stringValue];
117     if ([username isEqualToString:@"username1"]) {
118       EXPECT_NSEQ(@"password1", [itemContent.passwordField stringValue]);
119     } else if ([username isEqualToString:@"username2"]) {
120       EXPECT_NSEQ(@"password2", [itemContent.passwordField stringValue]);
121     } else {
122       NOTREACHED();
123     }
124   }
125 }
126
127 }  // namespace