Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / profiles / profile_signin_confirmation_view_controller_browsertest.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/profiles/profile_signin_confirmation_view_controller.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #import "testing/gtest_mac.h"
18 #include "ui/base/l10n/l10n_util.h"
19
20 class ProfileSigninConfirmationViewControllerTest
21   : public InProcessBrowserTest,
22     public ui::ProfileSigninConfirmationDelegate {
23
24  public:
25   ProfileSigninConfirmationViewControllerTest()
26     : window_(nil),
27       continued_(false),
28       cancelled_(false),
29       created_(false),
30       closed_(false) {
31   }
32
33  protected:
34   void SetUpOnMainThread() override {}
35
36   void SetupDialog(bool offerProfileCreation = true) {
37     window_.reset(
38         [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 600)
39                                     styleMask:NSBorderlessWindowMask
40                                       backing:NSBackingStoreBuffered
41                                         defer:NO]);
42     base::Closure close = base::Bind(
43         &ProfileSigninConfirmationViewControllerTest::OnClose, this);
44     controller_.reset([[ProfileSigninConfirmationViewController alloc]
45                         initWithBrowser:browser()
46                                username:username()
47                                delegate:this
48                     closeDialogCallback:close
49                    offerProfileCreation:offerProfileCreation]);
50     [[window_ contentView] addSubview:[controller_ view]];
51     [window_ makeKeyAndOrderFront:NSApp];
52     ASSERT_TRUE([window_ isVisible]);
53   }
54
55   // Test helpers.
56   std::string username() {
57     return "foo@bar.com";
58   }
59   base::string16 learn_more() {
60     return l10n_util::GetStringUTF16(
61         IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
62   }
63
64   // ui::ProfileSigninConfirmationDelegate:
65   void OnContinueSignin() override { continued_ = true; }
66   void OnCancelSignin() override { cancelled_ = true; }
67   void OnSigninWithNewProfile() override { created_ = true; }
68   void OnClose() { closed_ = true; }
69
70   // The window containing the dialog.
71   base::scoped_nsobject<NSWindow> window_;
72
73   // Dialog under test.
74   base::scoped_nsobject<ProfileSigninConfirmationViewController> controller_;
75
76   // Visible for testing UI interactions.
77   bool continued_;
78   bool cancelled_;
79   bool created_;
80   bool closed_;
81
82  private:
83   DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationViewControllerTest);
84 };
85
86 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
87                        ContinueClicked) {
88   SetupDialog();
89
90   // Click should invoke and clear delegate and close the dialog.
91   [controller_ ok:nil];
92   EXPECT_TRUE(continued_);
93   EXPECT_TRUE(closed_);
94   EXPECT_FALSE([controller_ delegate]);
95
96   // Another click should have no effect.
97   continued_ = false;
98   closed_ = false;
99   [controller_ ok:nil];
100   EXPECT_FALSE(continued_);
101   EXPECT_FALSE(closed_);
102 }
103
104 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
105                        CancelClicked) {
106   SetupDialog();
107
108   // Click should invoke and clear delegate and close the dialog.
109   [controller_ cancel:nil];
110   EXPECT_TRUE(cancelled_);
111   EXPECT_TRUE(closed_);
112   EXPECT_FALSE([controller_ delegate]);
113
114   // Another click should have no effect.
115   cancelled_ = false;
116   closed_ = false;
117   [controller_ cancel:nil];
118   EXPECT_FALSE(cancelled_);
119   EXPECT_FALSE(closed_);
120 }
121
122 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
123                        CreateClicked) {
124   SetupDialog();
125
126   // Click should invoke and clear delegate and close the dialog.
127   [controller_ createProfile:nil];
128   EXPECT_TRUE(created_);
129   EXPECT_TRUE(closed_);
130   EXPECT_FALSE([controller_ delegate]);
131
132   // Another click should have no effect.
133   created_ = false;
134   closed_ = false;
135   [controller_ createProfile:nil];
136   EXPECT_FALSE(created_);
137   EXPECT_FALSE(closed_);
138 }
139
140 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
141                        CloseClicked) {
142   SetupDialog();
143
144   // Click should invoke and clear delegate and close the dialog.
145   [controller_ cancel:nil];
146   EXPECT_TRUE(cancelled_);
147   EXPECT_TRUE(closed_);
148   EXPECT_FALSE([controller_ delegate]);
149
150   // Another click should close the dialog but not invoke the delegate.
151   cancelled_ = false;
152   closed_ = false;
153   [controller_ close:nil];
154   EXPECT_FALSE(cancelled_);
155   EXPECT_TRUE(closed_);
156 }
157
158 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
159                        DoNotOfferNewProfile) {
160   SetupDialog(/*offerProfileCreation = */ false);
161   // Create Profile button shouldn't exist.
162   EXPECT_NSEQ(nil, [controller_ createProfileButton]);
163   // Explanation shouldn't mention creating a new profile.
164   NSString* explanationWithoutCreateProfile = base::SysUTF16ToNSString(
165       l10n_util::GetStringFUTF16(
166           IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITHOUT_PROFILE_CREATION_NEW_STYLE,
167           base::UTF8ToUTF16(username()), learn_more()));
168   EXPECT_NSEQ(explanationWithoutCreateProfile,
169               [[[controller_ explanationField] textStorage] string]);
170 }
171
172 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
173                        OfferNewProfile) {
174   SetupDialog(/*offerProfileCreation = */ true);
175   // Create Profile button should exist and be visible.
176   EXPECT_NSNE(nil, [controller_ createProfileButton]);
177   EXPECT_TRUE([[[controller_ view] subviews]
178                 containsObject:[controller_ createProfileButton]]);
179   NSString* explanationWithCreateProfile = base::SysUTF16ToNSString(
180       l10n_util::GetStringFUTF16(
181           IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITH_PROFILE_CREATION_NEW_STYLE,
182           base::UTF8ToUTF16(username()), learn_more()));
183   EXPECT_NSEQ(explanationWithCreateProfile,
184               [[[controller_ explanationField] textStorage] string]);
185 }