summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/sad_tab_controller_unittest.mm
blob: 779739785c9fdc69438716d6d2265f913e257782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/debug/debugger.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#import "chrome/browser/cocoa/sad_tab_controller.h"
#import "chrome/browser/cocoa/sad_tab_view.h"
#include "chrome/browser/renderer_host/test/test_render_view_host.h"
#include "chrome/browser/tab_contents/test_tab_contents.h"
#include "chrome/test/testing_profile.h"

@interface SadTabView (ExposedForTesting)
// Implementation is below.
- (NSButton*)linkButton;
@end

@implementation SadTabView (ExposedForTesting)
- (NSButton*)linkButton {
  return linkButton_;
}
@end

namespace {

class SadTabControllerTest : public RenderViewHostTestHarness {
 public:
  SadTabControllerTest() : test_window_(nil) {
    link_clicked_ = false;
  }

  virtual void SetUp() {
    RenderViewHostTestHarness::SetUp();
    // Inherting from RenderViewHostTestHarness means we can't inherit from
    // from CocoaTest, so do a bootstrap and create test window.
    CocoaTest::BootstrapCocoa();
    test_window_ = [[CocoaTestHelperWindow alloc] init];
    if (base::debug::BeingDebugged()) {
      [test_window_ orderFront:nil];
    } else {
      [test_window_ orderBack:nil];
    }
  }

  virtual void TearDown() {
    [test_window_ close];
    test_window_ = nil;
    RenderViewHostTestHarness::TearDown();
  }

  // Creates the controller and adds its view to contents, caller has ownership.
  SadTabController* CreateController() {
    NSView* contentView = [test_window_ contentView];
    SadTabController* controller =
        [[SadTabController alloc] initWithTabContents:contents()
                                            superview:contentView];
    EXPECT_TRUE(controller);
    NSView* view = [controller view];
    EXPECT_TRUE(view);

    return controller;
  }

  NSButton* GetLinkButton(SadTabController* controller) {
    SadTabView* view = static_cast<SadTabView*>([controller view]);
    return ([view linkButton]);
  }

  static bool link_clicked_;
  CocoaTestHelperWindow* test_window_;
};

// static
bool SadTabControllerTest::link_clicked_;

TEST_F(SadTabControllerTest, WithTabContents) {
  scoped_nsobject<SadTabController> controller(CreateController());
  EXPECT_TRUE(controller);
  NSButton* link = GetLinkButton(controller);
  EXPECT_TRUE(link);
}

TEST_F(SadTabControllerTest, WithoutTabContents) {
  contents_.reset();
  scoped_nsobject<SadTabController> controller(CreateController());
  EXPECT_TRUE(controller);
  NSButton* link = GetLinkButton(controller);
  EXPECT_FALSE(link);
}

TEST_F(SadTabControllerTest, ClickOnLink) {
  scoped_nsobject<SadTabController> controller(CreateController());
  NSButton* link = GetLinkButton(controller);
  EXPECT_TRUE(link);
  EXPECT_FALSE(link_clicked_);
  [link performClick:link];
  EXPECT_TRUE(link_clicked_);
}

}  // namespace

@implementation NSApplication (SadTabControllerUnitTest)
// Add handler for the openLearnMoreAboutCrashLink: action to NSApp for testing
// purposes. Normally this would be sent up the responder tree correctly, but
// since tests run in the background, key window and main window are never set
// on NSApplication. Adding it to NSApplication directly removes the need for
// worrying about what the current window with focus is.
- (void)openLearnMoreAboutCrashLink:(id)sender {
  SadTabControllerTest::link_clicked_ = true;
}

@end