1// Copyright (c) 2011 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/html_dialog_window_controller.h"
6
7#include <string>
8#include <vector>
9
10#import <Cocoa/Cocoa.h>
11
12#import "base/mac/scoped_nsautorelease_pool.h"
13#include "base/sys_string_conversions.h"
14#include "chrome/browser/ui/browser.h"
15#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
16#include "chrome/browser/ui/webui/html_dialog_ui.h"
17#include "chrome/test/browser_with_test_window_test.h"
18#include "chrome/test/testing_profile.h"
19#include "content/browser/webui/web_ui.h"
20#include "googleurl/src/gurl.h"
21#include "testing/gmock/include/gmock/gmock.h"
22#include "testing/gtest/include/gtest/gtest.h"
23#include "ui/gfx/size.h"
24
25namespace {
26
27class MockDelegate : public HtmlDialogUIDelegate {
28public:
29  MOCK_CONST_METHOD0(IsDialogModal, bool());
30  MOCK_CONST_METHOD0(GetDialogTitle, std::wstring());
31  MOCK_CONST_METHOD0(GetDialogContentURL, GURL());
32  MOCK_CONST_METHOD1(GetWebUIMessageHandlers,
33                     void(std::vector<WebUIMessageHandler*>*));
34  MOCK_CONST_METHOD1(GetDialogSize, void(gfx::Size*));
35  MOCK_CONST_METHOD0(GetDialogArgs, std::string());
36  MOCK_METHOD1(OnDialogClosed, void(const std::string& json_retval));
37  MOCK_METHOD2(OnCloseContents,
38               void(TabContents* source, bool* out_close_dialog));
39  MOCK_CONST_METHOD0(ShouldShowDialogTitle, bool());
40};
41
42class HtmlDialogWindowControllerTest : public BrowserWithTestWindowTest {
43 public:
44  virtual void SetUp() {
45    BrowserWithTestWindowTest::SetUp();
46    CocoaTest::BootstrapCocoa();
47    title_ = L"Mock Title";
48    size_ = gfx::Size(50, 100);
49    gurl_ = GURL("");
50  }
51
52 protected:
53  std::wstring title_;
54  gfx::Size size_;
55  GURL gurl_;
56
57  // Order here is important.
58  MockDelegate delegate_;
59};
60
61using ::testing::_;
62using ::testing::Return;
63using ::testing::SetArgumentPointee;
64
65// TODO(akalin): We can't test much more than the below without a real browser.
66// In particular, GetWebUIMessageHandlers() and GetDialogArgs() are never
67// called. This should be fixed.
68
69TEST_F(HtmlDialogWindowControllerTest, showDialog) {
70  // We want to make sure html_dialog_window_controller below gets
71  // destroyed before delegate_, so we specify our own autorelease pool.
72  //
73  // TODO(dmaclach): Remove this once
74  // http://code.google.com/p/chromium/issues/detail?id=26133 is fixed.
75  base::mac::ScopedNSAutoreleasePool release_pool;
76
77  EXPECT_CALL(delegate_, GetDialogTitle())
78    .WillOnce(Return(title_));
79  EXPECT_CALL(delegate_, GetDialogSize(_))
80    .WillOnce(SetArgumentPointee<0>(size_));
81  EXPECT_CALL(delegate_, GetDialogContentURL())
82    .WillOnce(Return(gurl_));
83  EXPECT_CALL(delegate_, OnDialogClosed(_))
84    .Times(1);
85
86  HtmlDialogWindowController* html_dialog_window_controller =
87    [[HtmlDialogWindowController alloc] initWithDelegate:&delegate_
88                                                 profile:profile()];
89
90  [html_dialog_window_controller loadDialogContents];
91  [html_dialog_window_controller showWindow:nil];
92  [html_dialog_window_controller close];
93}
94
95}  // namespace
96