Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / profiles / profile_signin_confirmation_view_controller_browsertest.mm
blob9fe87623646cc64fcacef6bdd9ac3f3d81e496d2
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 {
24  public:
25   ProfileSigninConfirmationViewControllerTest()
26     : window_(nil),
27       continued_(false),
28       cancelled_(false),
29       created_(false),
30       closed_(false) {
31   }
33  protected:
34   void SetUpOnMainThread() override {}
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   }
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   }
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_;
73   // Dialog under test.
74   base::scoped_nsobject<ProfileSigninConfirmationViewController> controller_;
76   // Visible for testing UI interactions.
77   bool continued_;
78   bool cancelled_;
79   bool created_;
80   bool closed_;
82  private:
83   DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationViewControllerTest);
86 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
87                        ContinueClicked) {
88   SetupDialog();
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]);
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_);
104 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
105                        CancelClicked) {
106   SetupDialog();
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.
115   cancelled_ = false;
116   closed_ = false;
117   [controller_ cancel:nil];
118   EXPECT_FALSE(cancelled_);
119   EXPECT_FALSE(closed_);
122 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
123                        CreateClicked) {
124   SetupDialog();
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.
133   created_ = false;
134   closed_ = false;
135   [controller_ createProfile:nil];
136   EXPECT_FALSE(created_);
137   EXPECT_FALSE(closed_);
140 IN_PROC_BROWSER_TEST_F(ProfileSigninConfirmationViewControllerTest,
141                        CloseClicked) {
142   SetupDialog();
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.
151   cancelled_ = false;
152   closed_ = false;
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,
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]);