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. 4 5 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" 6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 7 #import "testing/gtest_mac.h" 8 9 class ConstrainedWindowAlertTest : public CocoaTest { 10 }; 11 12 @interface ConstrainedWindowAlertTestTarget : NSObject { 13 @private 14 int linkClickedCount_; 15 } 16 @property(nonatomic, readonly) int linkClickedCount; 17 18 - (void)onLinkClicked:(id)sender; 19 @end 20 21 @implementation ConstrainedWindowAlertTestTarget 22 23 @synthesize linkClickedCount = linkClickedCount_; 24 25 - (void)onLinkClicked:(id)sender { 26 ++linkClickedCount_; 27 } 28 29 @end 30 31 // Test showing the alert. 32 TEST_F(ConstrainedWindowAlertTest, Show) { 33 base::scoped_nsobject<ConstrainedWindowAlert> alert( 34 [[ConstrainedWindowAlert alloc] init]); 35 EXPECT_TRUE([alert window]); 36 EXPECT_TRUE([alert closeButton]); 37 38 [alert setMessageText:@"Message text"]; 39 [alert setInformativeText:@"Informative text"]; 40 [alert addButtonWithTitle:@"OK" keyEquivalent:@"" target:nil action:NULL]; 41 [alert addButtonWithTitle:@"Cancel" keyEquivalent:@"" target:nil action:NULL]; 42 43 [alert layout]; 44 [[alert window] makeKeyAndOrderFront:nil]; 45 } 46 47 // Test showing the alert with no buttons. 48 TEST_F(ConstrainedWindowAlertTest, NoButtons) { 49 base::scoped_nsobject<ConstrainedWindowAlert> alert( 50 [[ConstrainedWindowAlert alloc] init]); 51 [alert layout]; 52 [[alert window] makeKeyAndOrderFront:nil]; 53 } 54 55 // Test adding an accessory view to an alert. 56 TEST_F(ConstrainedWindowAlertTest, AccessoryView) { 57 base::scoped_nsobject<ConstrainedWindowAlert> alert( 58 [[ConstrainedWindowAlert alloc] init]); 59 [alert addButtonWithTitle:@"OK" keyEquivalent:@"" target:nil action:NULL]; 60 [alert addButtonWithTitle:@"Cancel" keyEquivalent:@"" target:nil action:NULL]; 61 62 NSRect view_rect = NSMakeRect(0, 0, 700, 300); 63 base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:view_rect]); 64 EXPECT_FALSE([alert accessoryView]); 65 [alert setAccessoryView:view]; 66 EXPECT_NSEQ([alert accessoryView], view); 67 68 [alert layout]; 69 NSRect window_rect = [[alert window] frame]; 70 EXPECT_GT(NSWidth(window_rect), NSWidth(view_rect)); 71 EXPECT_GT(NSHeight(window_rect), NSHeight(view_rect)); 72 73 [[alert window] makeKeyAndOrderFront:nil]; 74 } 75 76 // Test adding a link to an alert. 77 TEST_F(ConstrainedWindowAlertTest, LinkView) { 78 base::scoped_nsobject<ConstrainedWindowAlert> alert( 79 [[ConstrainedWindowAlert alloc] init]); 80 base::scoped_nsobject<ConstrainedWindowAlertTestTarget> target( 81 [[ConstrainedWindowAlertTestTarget alloc] init]); 82 83 [alert layout]; 84 NSRect initial_window_rect = [[alert window] frame]; 85 86 EXPECT_EQ(nil, [alert linkView]); 87 NSString* linkText = @"Text of the link"; 88 [alert setLinkText:linkText 89 target:target.get() 90 action:@selector(onLinkClicked:)]; 91 EXPECT_EQ([linkText length], [[[alert linkView] title] length]); 92 93 [alert layout]; 94 NSRect window_rect = [[alert window] frame]; 95 96 EXPECT_GT(NSHeight(window_rect), NSHeight(initial_window_rect)); 97 98 [[alert window] makeKeyAndOrderFront:nil]; 99 100 [[alert linkView] performClick:nil]; 101 EXPECT_EQ(1, [target linkClickedCount]); 102 } 103