1// Copyright 2013 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/screen_capture_notification_ui_cocoa.h"
6
7#include "base/bind.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10
11@interface ScreenCaptureNotificationController (ExposedForTesting)
12- (NSButton*)stopButton;
13- (NSButton*)minimizeButton;
14@end
15
16@implementation ScreenCaptureNotificationController (ExposedForTesting)
17- (NSButton*)stopButton {
18  return stopButton_;
19}
20
21- (NSButton*)minimizeButton {
22  return minimizeButton_;
23}
24@end
25
26class ScreenCaptureNotificationUICocoaTest : public CocoaTest {
27 public:
28  ScreenCaptureNotificationUICocoaTest()
29      : callback_called_(0) {
30  }
31
32  virtual void TearDown() OVERRIDE {
33    callback_called_ = 0;
34    target_.reset();
35    EXPECT_EQ(0, callback_called_);
36
37    CocoaTest::TearDown();
38  }
39
40  void StopCallback() {
41    ++callback_called_;
42  }
43
44 protected:
45  ScreenCaptureNotificationController* controller() {
46    return target_->windowController_.get();
47  }
48
49  scoped_ptr<ScreenCaptureNotificationUICocoa> target_;
50  int callback_called_;
51
52  DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUICocoaTest);
53};
54
55TEST_F(ScreenCaptureNotificationUICocoaTest, CreateAndDestroy) {
56  target_.reset(
57      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
58}
59
60TEST_F(ScreenCaptureNotificationUICocoaTest, CreateAndStart) {
61  target_.reset(
62      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
63  target_->OnStarted(
64      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
65                 base::Unretained(this)));
66}
67
68TEST_F(ScreenCaptureNotificationUICocoaTest, LongTitle) {
69  target_.reset(new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16(
70      "Very long title, with very very very very very very very very "
71      "very very very very very very very very very very very very many "
72      "words")));
73  target_->OnStarted(
74      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
75                 base::Unretained(this)));
76  // The elided label sometimes is a few pixels longer than the max width. So
77  // allow a 5px off from the 1000px maximium.
78  EXPECT_LE(NSWidth([[controller() window] frame]), 1005);
79}
80
81TEST_F(ScreenCaptureNotificationUICocoaTest, ShortTitle) {
82  target_.reset(
83      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
84  target_->OnStarted(
85      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
86                 base::Unretained(this)));
87  EXPECT_EQ(460, NSWidth([[controller() window] frame]));
88}
89
90TEST_F(ScreenCaptureNotificationUICocoaTest, ClickStop) {
91  target_.reset(
92      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
93  target_->OnStarted(
94      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
95                 base::Unretained(this)));
96
97  [[controller() stopButton] performClick:nil];
98  EXPECT_EQ(1, callback_called_);
99}
100
101TEST_F(ScreenCaptureNotificationUICocoaTest, CloseWindow) {
102  target_.reset(
103      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
104  target_->OnStarted(
105      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
106                 base::Unretained(this)));
107
108  [[controller() window] close];
109
110  EXPECT_EQ(1, callback_called_);
111}
112
113TEST_F(ScreenCaptureNotificationUICocoaTest, MinimizeWindow) {
114  target_.reset(
115      new ScreenCaptureNotificationUICocoa(base::UTF8ToUTF16("Title")));
116  target_->OnStarted(
117      base::Bind(&ScreenCaptureNotificationUICocoaTest::StopCallback,
118                 base::Unretained(this)));
119
120  [[controller() minimizeButton] performClick:nil];
121
122  EXPECT_EQ(0, callback_called_);
123  EXPECT_TRUE([[controller() window] isMiniaturized]);
124}
125