1 // Copyright 2013 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 "ui/app_list/cocoa/signin_view_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/utf_string_conversions.h"
10 #import "testing/gtest_mac.h"
11 #include "ui/app_list/signin_delegate.h"
12 #include "ui/app_list/test/app_list_test_view_delegate.h"
13 #import "ui/base/test/ui_cocoa_test_helper.h"
15 @class TestSigninViewDelegate;
19 // Helper function to cycle through the responder chain created implicitly
20 // from subviews, without having to interact with the event system.
21 NSControl* NextControl(NSControl* control) {
22 NSArray* siblings = [[control superview] subviews];
23 for (NSUInteger index = [siblings indexOfObject:control] + 1;
24 index < [siblings count] ; ++index) {
25 if ([[siblings objectAtIndex:index] acceptsFirstResponder])
26 return [siblings objectAtIndex:index];
36 class SigninViewControllerTest : public ui::CocoaTest,
37 public SigninDelegate {
39 SigninViewControllerTest()
40 : test_text_(ASCIIToUTF16("Sign in")),
42 show_signin_count_(0),
43 open_learn_more_count_(0),
44 open_settings_count_(0) {}
46 // ui::CocoaTest override:
47 virtual void SetUp() OVERRIDE;
49 // SigninDelegate overrides:
50 virtual bool NeedSignin() OVERRIDE { return needs_signin_; }
51 virtual void ShowSignin() OVERRIDE { ++show_signin_count_; }
52 virtual void OpenLearnMore() OVERRIDE { ++open_learn_more_count_; }
53 virtual void OpenSettings() OVERRIDE { ++open_settings_count_; }
55 virtual base::string16 GetSigninHeading() OVERRIDE { return test_text_; }
56 virtual base::string16 GetSigninText() OVERRIDE { return test_text_; }
57 virtual base::string16 GetSigninButtonText() OVERRIDE { return test_text_; }
58 virtual base::string16 GetLearnMoreLinkText() OVERRIDE { return test_text_; }
59 virtual base::string16 GetSettingsLinkText() OVERRIDE { return test_text_; }
62 const base::string16 test_text_;
63 base::scoped_nsobject<SigninViewController> signin_view_controller_;
65 int show_signin_count_;
66 int open_learn_more_count_;
67 int open_settings_count_;
70 DISALLOW_COPY_AND_ASSIGN(SigninViewControllerTest);
73 void SigninViewControllerTest::SetUp() {
74 NSRect frame = NSMakeRect(0, 0, 400, 500);
75 signin_view_controller_.reset(
76 [[SigninViewController alloc] initWithFrame:frame
80 ui::CocoaTest::SetUp();
81 [[test_window() contentView] addSubview:[signin_view_controller_ view]];
84 TEST_VIEW(SigninViewControllerTest, [signin_view_controller_ view]);
86 TEST_F(SigninViewControllerTest, NotSignedIn) {
87 NSArray* content_subviews = [[test_window() contentView] subviews];
88 EXPECT_EQ(1u, [content_subviews count]);
89 NSArray* subviews = [[content_subviews objectAtIndex:0] subviews];
90 EXPECT_LT(0u, [subviews count]);
92 // The first subview that acceptFirstResponder should be the signin button,
93 // and performing its action should show the signin dialog. Then "Learn more",
94 // followed by "Settings".
95 NSControl* control = NextControl([subviews objectAtIndex:0]);
96 EXPECT_EQ(0, show_signin_count_);
97 EXPECT_TRUE([[control target] performSelector:[control action]
99 EXPECT_EQ(1, show_signin_count_);
101 control = NextControl(control);
102 EXPECT_EQ(0, open_learn_more_count_);
103 EXPECT_TRUE([[control target] performSelector:[control action]
104 withObject:control]);
105 EXPECT_EQ(1, open_learn_more_count_);
107 control = NextControl(control);
108 EXPECT_EQ(0, open_settings_count_);
109 EXPECT_TRUE([[control target] performSelector:[control action]
110 withObject:control]);
111 EXPECT_EQ(1, open_settings_count_);
115 } // namespace app_list