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
20class 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  virtual void SetUpOnMainThread() OVERRIDE {
35  }
36
37  void SetupDialog(bool offerProfileCreation = true) {
38    window_.reset(
39        [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 600)
40                                    styleMask:NSBorderlessWindowMask
41                                      backing:NSBackingStoreBuffered
42                                        defer:NO]);
43    base::Closure close = base::Bind(
44        &ProfileSigninConfirmationViewControllerTest::OnClose, this);
45    controller_.reset([[ProfileSigninConfirmationViewController alloc]
46                        initWithBrowser:browser()
47                               username:username()
48                               delegate:this
49                    closeDialogCallback:close
50                   offerProfileCreation:offerProfileCreation]);
51    [[window_ contentView] addSubview:[controller_ view]];
52    [window_ makeKeyAndOrderFront:NSApp];
53    ASSERT_TRUE([window_ isVisible]);
54  }
55
56  // Test helpers.
57  std::string username() {
58    return "foo@bar.com";
59  }
60  base::string16 learn_more() {
61    return l10n_util::GetStringUTF16(
62        IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
63  }
64
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; }
70
71  // The window containing the dialog.
72  base::scoped_nsobject<NSWindow> window_;
73
74  // Dialog under test.
75  base::scoped_nsobject<ProfileSigninConfirmationViewController> controller_;
76
77  // Visible for testing UI interactions.
78  bool continued_;
79  bool cancelled_;
80  bool created_;
81  bool closed_;
82
83 private:
84  DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationViewControllerTest);
85};
86
87IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
88                       ContinueClicked) {
89  SetupDialog();
90
91  // Click should invoke and clear delegate and close the dialog.
92  [controller_ ok:nil];
93  EXPECT_TRUE(continued_);
94  EXPECT_TRUE(closed_);
95  EXPECT_FALSE([controller_ delegate]);
96
97  // Another click should have no effect.
98  continued_ = false;
99  closed_ = false;
100  [controller_ ok:nil];
101  EXPECT_FALSE(continued_);
102  EXPECT_FALSE(closed_);
103}
104
105IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
106                       CancelClicked) {
107  SetupDialog();
108
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]);
114
115  // Another click should have no effect.
116  cancelled_ = false;
117  closed_ = false;
118  [controller_ cancel:nil];
119  EXPECT_FALSE(cancelled_);
120  EXPECT_FALSE(closed_);
121}
122
123IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
124                       CreateClicked) {
125  SetupDialog();
126
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]);
132
133  // Another click should have no effect.
134  created_ = false;
135  closed_ = false;
136  [controller_ createProfile:nil];
137  EXPECT_FALSE(created_);
138  EXPECT_FALSE(closed_);
139}
140
141IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
142                       CloseClicked) {
143  SetupDialog();
144
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]);
150
151  // Another click should close the dialog but not invoke the delegate.
152  cancelled_ = false;
153  closed_ = false;
154  [controller_ close:nil];
155  EXPECT_FALSE(cancelled_);
156  EXPECT_TRUE(closed_);
157}
158
159IN_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]);
171}
172
173IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
174                       OfferNewProfile) {
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]);
186}
187