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