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/test/base/in_process_browser_test.h"
15 #include "grit/chromium_strings.h"
16 #include "grit/generated_resources.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 virtual void SetUpOnMainThread() OVERRIDE {
37 void SetupDialog(bool offerProfileCreation = true) {
39 [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 600)
40 styleMask:NSBorderlessWindowMask
41 backing:NSBackingStoreBuffered
43 base::Closure close = base::Bind(
44 &ProfileSigninConfirmationViewControllerTest::OnClose, this);
45 controller_.reset([[ProfileSigninConfirmationViewController alloc]
46 initWithBrowser:browser()
49 closeDialogCallback:close
50 offerProfileCreation:offerProfileCreation]);
51 [[window_ contentView] addSubview:[controller_ view]];
52 [window_ makeKeyAndOrderFront:NSApp];
53 ASSERT_TRUE([window_ isVisible]);
57 std::string username() {
60 base::string16 learn_more() {
61 return l10n_util::GetStringUTF16(
62 IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
65 // ui::ProfileSigninConfirmationDelegate:
66 virtual void OnContinueSignin() OVERRIDE { continued_ = true; }
67 virtual void OnCancelSignin() OVERRIDE { cancelled_ = true; }
68 virtual void OnSigninWithNewProfile() OVERRIDE { created_ = true; }
69 void OnClose() { closed_ = true; }
71 // The window containing the dialog.
72 base::scoped_nsobject<NSWindow> window_;
75 base::scoped_nsobject<ProfileSigninConfirmationViewController> controller_;
77 // Visible for testing UI interactions.
84 DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationViewControllerTest);
87 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
91 // Click should invoke and clear delegate and close the dialog.
93 EXPECT_TRUE(continued_);
95 EXPECT_FALSE([controller_ delegate]);
97 // Another click should have no effect.
100 [controller_ ok:nil];
101 EXPECT_FALSE(continued_);
102 EXPECT_FALSE(closed_);
105 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
109 // Click should invoke and clear delegate and close the dialog.
110 [controller_ cancel:nil];
111 EXPECT_TRUE(cancelled_);
112 EXPECT_TRUE(closed_);
113 EXPECT_FALSE([controller_ delegate]);
115 // Another click should have no effect.
118 [controller_ cancel:nil];
119 EXPECT_FALSE(cancelled_);
120 EXPECT_FALSE(closed_);
123 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
127 // Click should invoke and clear delegate and close the dialog.
128 [controller_ createProfile:nil];
129 EXPECT_TRUE(created_);
130 EXPECT_TRUE(closed_);
131 EXPECT_FALSE([controller_ delegate]);
133 // Another click should have no effect.
136 [controller_ createProfile:nil];
137 EXPECT_FALSE(created_);
138 EXPECT_FALSE(closed_);
141 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
145 // Click should invoke and clear delegate and close the dialog.
146 [controller_ cancel:nil];
147 EXPECT_TRUE(cancelled_);
148 EXPECT_TRUE(closed_);
149 EXPECT_FALSE([controller_ delegate]);
151 // Another click should close the dialog but not invoke the delegate.
154 [controller_ close:nil];
155 EXPECT_FALSE(cancelled_);
156 EXPECT_TRUE(closed_);
159 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
160 DoNotOfferNewProfile) {
161 SetupDialog(/*offerProfileCreation = */ false);
162 // Create Profile button shouldn't exist.
163 EXPECT_NSEQ(nil, [controller_ createProfileButton]);
164 // Explanation shouldn't mention creating a new profile.
165 NSString* explanationWithoutCreateProfile = base::SysUTF16ToNSString(
166 l10n_util::GetStringFUTF16(
167 IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITHOUT_PROFILE_CREATION_NEW_STYLE,
168 base::UTF8ToUTF16(username()), learn_more()));
169 EXPECT_NSEQ(explanationWithoutCreateProfile,
170 [[[controller_ explanationField] textStorage] string]);
173 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
175 SetupDialog(/*offerProfileCreation = */ true);
176 // Create Profile button should exist and be visible.
177 EXPECT_NSNE(nil, [controller_ createProfileButton]);
178 EXPECT_TRUE([[[controller_ view] subviews]
179 containsObject:[controller_ createProfileButton]]);
180 NSString* explanationWithCreateProfile = base::SysUTF16ToNSString(
181 l10n_util::GetStringFUTF16(
182 IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITH_PROFILE_CREATION_NEW_STYLE,
183 base::UTF8ToUTF16(username()), learn_more()));
184 EXPECT_NSEQ(explanationWithCreateProfile,
185 [[[controller_ explanationField] textStorage] string]);