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.
5 #import "chrome/browser/ui/cocoa/profiles/profile_signin_confirmation_view_controller.h"
7 #import <Cocoa/Cocoa.h>
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"
20 class ProfileSigninConfirmationViewControllerTest
21 : public InProcessBrowserTest,
22 public ui::ProfileSigninConfirmationDelegate {
25 ProfileSigninConfirmationViewControllerTest()
34 void SetUpOnMainThread() override {}
36 void SetupDialog(bool offerProfileCreation = true) {
38 [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 600)
39 styleMask:NSBorderlessWindowMask
40 backing:NSBackingStoreBuffered
42 base::Closure close = base::Bind(
43 &ProfileSigninConfirmationViewControllerTest::OnClose, this);
44 controller_.reset([[ProfileSigninConfirmationViewController alloc]
45 initWithBrowser:browser()
48 closeDialogCallback:close
49 offerProfileCreation:offerProfileCreation]);
50 [[window_ contentView] addSubview:[controller_ view]];
51 [window_ makeKeyAndOrderFront:NSApp];
52 ASSERT_TRUE([window_ isVisible]);
56 std::string username() {
59 base::string16 learn_more() {
60 return l10n_util::GetStringUTF16(
61 IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
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; }
70 // The window containing the dialog.
71 base::scoped_nsobject<NSWindow> window_;
74 base::scoped_nsobject<ProfileSigninConfirmationViewController> controller_;
76 // Visible for testing UI interactions.
83 DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationViewControllerTest);
86 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
90 // Click should invoke and clear delegate and close the dialog.
92 EXPECT_TRUE(continued_);
94 EXPECT_FALSE([controller_ delegate]);
96 // Another click should have no effect.
100 EXPECT_FALSE(continued_);
101 EXPECT_FALSE(closed_);
104 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
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]);
114 // Another click should have no effect.
117 [controller_ cancel:nil];
118 EXPECT_FALSE(cancelled_);
119 EXPECT_FALSE(closed_);
122 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
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]);
132 // Another click should have no effect.
135 [controller_ createProfile:nil];
136 EXPECT_FALSE(created_);
137 EXPECT_FALSE(closed_);
140 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
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]);
150 // Another click should close the dialog but not invoke the delegate.
153 [controller_ close:nil];
154 EXPECT_FALSE(cancelled_);
155 EXPECT_TRUE(closed_);
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]);
172 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
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]);