Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / constrained_window / constrained_window_alert_unittest.mm
blob6bb9ffd025ce5406c1f6f8d4d809a925c0687718
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 {
14  @private
15   int linkClickedCount_;
17 @property(nonatomic, readonly) int linkClickedCount;
19 - (void)onLinkClicked:(id)sender;
20 @end
22 @implementation ConstrainedWindowAlertTestTarget
24 @synthesize linkClickedCount = linkClickedCount_;
26 - (void)onLinkClicked:(id)sender {
27   ++linkClickedCount_;
30 @end
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];
44   [alert layout];
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]);
52   [alert layout];
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);
69   [alert layout];
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]);
84   [alert layout];
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
90               target:target.get()
91               action:@selector(onLinkClicked:)];
92   EXPECT_EQ([linkText length], [[[alert linkView] title] length]);
94   [alert layout];
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]);