Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / constrained_window / constrained_window_alert_unittest.mm
blobb5fe7a710d6a412d52cb63e0f5e78f83e0489300
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"
6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
7 #import "testing/gtest_mac.h"
9 class ConstrainedWindowAlertTest : public CocoaTest {
12 @interface ConstrainedWindowAlertTestTarget : NSObject {
13  @private
14   int linkClickedCount_;
16 @property(nonatomic, readonly) int linkClickedCount;
18 - (void)onLinkClicked:(id)sender;
19 @end
21 @implementation ConstrainedWindowAlertTestTarget
23 @synthesize linkClickedCount = linkClickedCount_;
25 - (void)onLinkClicked:(id)sender {
26   ++linkClickedCount_;
29 @end
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]);
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];
43   [alert layout];
44   [[alert window] makeKeyAndOrderFront:nil];
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];
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];
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);
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));
73   [[alert window] makeKeyAndOrderFront:nil];
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]);
83   [alert layout];
84   NSRect initial_window_rect = [[alert window] frame];
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]);
93   [alert layout];
94   NSRect window_rect = [[alert window] frame];
96   EXPECT_GT(NSHeight(window_rect), NSHeight(initial_window_rect));
98   [[alert window] makeKeyAndOrderFront:nil];
100   [[alert linkView] performClick:nil];
101   EXPECT_EQ(1, [target linkClickedCount]);