1 // Copyright (c) 2012 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/constrained_window/constrained_window_alert.h"
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
8 #import "testing/gtest_mac.h"
10 class ConstrainedWindowAlertTest : public CocoaTest {
13 @interface ConstrainedWindowAlertTestTarget : NSObject {
15 int linkClickedCount_;
17 @property(nonatomic, readonly) int linkClickedCount;
19 - (void)onLinkClicked:(id)sender;
22 @implementation ConstrainedWindowAlertTestTarget
24 @synthesize linkClickedCount = linkClickedCount_;
26 - (void)onLinkClicked:(id)sender {
32 // Test showing the alert.
33 TEST_F(ConstrainedWindowAlertTest, Show) {
34 base::scoped_nsobject<ConstrainedWindowAlert> alert(
35 [[ConstrainedWindowAlert alloc] init]);
36 EXPECT_TRUE([alert window]);
37 EXPECT_TRUE([alert closeButton]);
39 [alert setMessageText:@"Message text"];
40 [alert setInformativeText:@"Informative text"];
41 [alert addButtonWithTitle:@"OK" keyEquivalent:@"" target:nil action:NULL];
42 [alert addButtonWithTitle:@"Cancel" keyEquivalent:@"" target:nil action:NULL];
45 [[alert window] makeKeyAndOrderFront:nil];
48 // Test showing the alert with no buttons.
49 TEST_F(ConstrainedWindowAlertTest, NoButtons) {
50 base::scoped_nsobject<ConstrainedWindowAlert> alert(
51 [[ConstrainedWindowAlert alloc] init]);
53 [[alert window] makeKeyAndOrderFront:nil];
56 // Test adding an accessory view to an alert.
57 TEST_F(ConstrainedWindowAlertTest, AccessoryView) {
58 base::scoped_nsobject<ConstrainedWindowAlert> alert(
59 [[ConstrainedWindowAlert alloc] init]);
60 [alert addButtonWithTitle:@"OK" keyEquivalent:@"" target:nil action:NULL];
61 [alert addButtonWithTitle:@"Cancel" keyEquivalent:@"" target:nil action:NULL];
63 NSRect view_rect = NSMakeRect(0, 0, 700, 300);
64 base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:view_rect]);
65 EXPECT_FALSE([alert accessoryView]);
66 [alert setAccessoryView:view];
67 EXPECT_NSEQ([alert accessoryView], view);
70 NSRect window_rect = [[alert window] frame];
71 EXPECT_GT(NSWidth(window_rect), NSWidth(view_rect));
72 EXPECT_GT(NSHeight(window_rect), NSHeight(view_rect));
74 [[alert window] makeKeyAndOrderFront:nil];
77 // Test adding a link to an alert.
78 TEST_F(ConstrainedWindowAlertTest, LinkView) {
79 base::scoped_nsobject<ConstrainedWindowAlert> alert(
80 [[ConstrainedWindowAlert alloc] init]);
81 base::scoped_nsobject<ConstrainedWindowAlertTestTarget> target(
82 [[ConstrainedWindowAlertTestTarget alloc] init]);
85 NSRect initial_window_rect = [[alert window] frame];
87 EXPECT_EQ(nil, [alert linkView]);
88 NSString* linkText = @"Text of the link";
89 [alert setLinkText:linkText
91 action:@selector(onLinkClicked:)];
92 EXPECT_EQ([linkText length], [[[alert linkView] title] length]);
95 NSRect window_rect = [[alert window] frame];
97 EXPECT_GT(NSHeight(window_rect), NSHeight(initial_window_rect));
99 [[alert window] makeKeyAndOrderFront:nil];
101 [[alert linkView] performClick:nil];
102 EXPECT_EQ(1, [target linkClickedCount]);