summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/ready_mode_unittest.cc
blob: 6022d26aef4f6c0568756ee8cd6b95424fc905a2 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2011 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 <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
#include <atlwin.h>

#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/registry.h"
#include "chrome_frame/infobars/infobar_content.h"
#include "chrome_frame/ready_mode/internal/ready_mode_state.h"
#include "chrome_frame/ready_mode/internal/ready_prompt_content.h"
#include "chrome_frame/ready_mode/internal/ready_prompt_window.h"
#include "chrome_frame/ready_mode/internal/registry_ready_mode_state.h"
#include "chrome_frame/ready_mode/internal/url_launcher.h"
#include "chrome_frame/simple_resource_loader.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"

namespace {

class SetResourceInstance {
 public:
  SetResourceInstance() : res_dll_(NULL), old_res_dll_(NULL) {
    SimpleResourceLoader* loader_instance = SimpleResourceLoader::GetInstance();
    EXPECT_TRUE(loader_instance != NULL);
    if (loader_instance != NULL) {
      res_dll_ = loader_instance->GetResourceModuleHandle();
      EXPECT_TRUE(res_dll_ != NULL);
      if (res_dll_ != NULL) {
        old_res_dll_ = ATL::_AtlBaseModule.SetResourceInstance(res_dll_);
      }
    }
  }

  ~SetResourceInstance() {
    if (old_res_dll_ != NULL) {
      CHECK_EQ(res_dll_, ATL::_AtlBaseModule.SetResourceInstance(old_res_dll_));
    }
  }

 private:
  HMODULE res_dll_;
  HMODULE old_res_dll_;
};  // class SetResourceInstance

class SimpleWindow : public CWindowImpl<SimpleWindow,
                                        CWindow,
                                        CFrameWinTraits> {
 public:
  virtual ~SimpleWindow() {
    if (IsWindow())
      DestroyWindow();
  }

  static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM l_param) {
    HWND* out = reinterpret_cast<HWND*>(l_param);
    EXPECT_TRUE(out != NULL);

    if (out == NULL)
      return FALSE;

    EXPECT_TRUE(*out == NULL || ::IsChild(*out, hwnd));

    if (*out == NULL)
      *out = hwnd;

    return TRUE;
  }

  HWND GetZeroOrOneChildWindows() {
    HWND child = NULL;
    EnumChildWindows(m_hWnd, EnumChildProc, reinterpret_cast<LPARAM>(&child));
    return child;
  }

  BEGIN_MSG_MAP(SimpleWindow)
  END_MSG_MAP()
};  // class SimpleWindow

class MockInfobarContentFrame : public InfobarContent::Frame {
 public:
  // InfobarContent::Frame implementation
  MOCK_METHOD0(GetFrameWindow, HWND(void));
  MOCK_METHOD0(CloseInfobar, void(void));
};  // class Frame

class MockReadyModeState : public ReadyModeState {
 public:
  // ReadyModeState implementation
  MOCK_METHOD0(TemporarilyDeclineChromeFrame, void(void));
  MOCK_METHOD0(PermanentlyDeclineChromeFrame, void(void));
  MOCK_METHOD0(AcceptChromeFrame, void(void));
};  // class MockReadyModeState

class MockUrlLauncher : public UrlLauncher {
 public:
  // UrlLauncher implementation
  MOCK_METHOD1(LaunchUrl, void(const std::wstring& url));
};  // class MockUrlLauncher

}  // namespace

class ReadyPromptTest : public testing::Test {
 public:
  ReadyPromptTest() : hwnd_(NULL) {}

  void SetUp() {
    hwnd_ = window_.Create(NULL);
    EXPECT_TRUE(hwnd_ != NULL);
    window_.ShowWindow(SW_SHOW);
    EXPECT_TRUE(window_.IsWindowVisible());
    EXPECT_CALL(frame_, GetFrameWindow()).Times(testing::AnyNumber())
        .WillRepeatedly(testing::Return(hwnd_));
  }

 protected:
  SimpleWindow window_;
  HWND hwnd_;
  MockInfobarContentFrame frame_;
  SetResourceInstance set_resource_instance_;
};  // class ReadyPromptTest

class ReadyPromptWindowTest : public ReadyPromptTest {
 public:
  void SetUp() {
    ReadyPromptTest::SetUp();

    // owned by ReadyPromptWindow
    state_ = new MockReadyModeState();
    url_launcher_ = new MockUrlLauncher();

    ready_prompt_window_ = ReadyPromptWindow::CreateInstance(
        &frame_, state_, url_launcher_);

    ASSERT_TRUE(ready_prompt_window_ != NULL);
    RECT position = {0, 0, 800, 39};
    ASSERT_TRUE(ready_prompt_window_->SetWindowPos(HWND_TOP, &position,
                                                   SWP_SHOWWINDOW));
  }

 protected:
  MockReadyModeState* state_;
  MockUrlLauncher* url_launcher_;
  base::WeakPtr<ReadyPromptWindow> ready_prompt_window_;
};  // class ReadyPromptWindowTest

class ReadyPromptWindowButtonTest : public ReadyPromptWindowTest {
 public:
  void TearDown() {
    ASSERT_TRUE(ready_prompt_window_ != NULL);
    ASSERT_TRUE(ready_prompt_window_->DestroyWindow());
    ASSERT_TRUE(ready_prompt_window_ == NULL);
    ASSERT_FALSE(message_loop_.WasTimedOut());

    ReadyPromptWindowTest::TearDown();
  }

 protected:
  struct ClickOnCaptionData {
    const wchar_t* target_caption;
    bool found;
  };  // struct ClickOnCaptionData

  static BOOL CALLBACK ClickOnCaptionProc(HWND hwnd, LPARAM l_param) {
    wchar_t window_caption[256] = {0};
    size_t buffer_length = arraysize(window_caption);

    ClickOnCaptionData* data = reinterpret_cast<ClickOnCaptionData*>(l_param);
    EXPECT_TRUE(data->target_caption != NULL);

    if (data->target_caption == NULL)
      return FALSE;

    if (wcsnlen(data->target_caption, buffer_length + 1) == buffer_length + 1)
      return FALSE;

    if (::GetWindowText(hwnd, window_caption, buffer_length) ==
        static_cast<int>(buffer_length)) {
      return TRUE;
    }

    if (wcscmp(data->target_caption, window_caption) == 0) {
      EXPECT_FALSE(data->found);

      CRect client_rect;
      EXPECT_TRUE(::GetClientRect(hwnd, client_rect));

      CPoint center_point(client_rect.CenterPoint());
      LPARAM coordinates = (center_point.y << 16) | center_point.x;

      ::PostMessage(hwnd, WM_LBUTTONDOWN, 0, coordinates);
      ::PostMessage(hwnd, WM_LBUTTONUP, 0, coordinates);

      data->found = true;
    }

    return TRUE;
  }

  bool ClickOnCaption(const std::wstring& caption) {
    ClickOnCaptionData data = {caption.c_str(), false};

    ::EnumChildWindows(hwnd_, ClickOnCaptionProc,
                       reinterpret_cast<LPARAM>(&data));
    return data.found;
  }

  void RunUntilCloseInfobar() {
    EXPECT_CALL(frame_, CloseInfobar()).WillOnce(QUIT_LOOP(message_loop_));
    ASSERT_NO_FATAL_FAILURE(message_loop_.RunFor(5));  // seconds
  }

  chrome_frame_test::TimedMsgLoop message_loop_;
};  // class ReadyPromptWindowButtonTest

TEST_F(ReadyPromptTest, ReadyPromptContentTest) {
  // owned by ReadyPromptContent
  MockReadyModeState* state = new MockReadyModeState();
  MockUrlLauncher* url_launcher = new MockUrlLauncher();

  scoped_ptr<ReadyPromptContent> content_(new ReadyPromptContent(state,
                                                                 url_launcher));

  content_->InstallInFrame(&frame_);

  // Ensure that, if a child is created, it is not visible yet.
  HWND child_hwnd = window_.GetZeroOrOneChildWindows();
  if (child_hwnd != NULL) {
    CWindow child(child_hwnd);
    RECT child_dimensions;
    EXPECT_TRUE(child.GetClientRect(&child_dimensions));
    EXPECT_FALSE(child.IsWindowVisible() && !::IsRectEmpty(&child_dimensions));
  }

  int desired_height = content_->GetDesiredSize(400, 0);
  EXPECT_GT(desired_height, 0);
  RECT dimensions = {10, 15, 410, 20};
  content_->SetDimensions(dimensions);

  child_hwnd = window_.GetZeroOrOneChildWindows();
  EXPECT_TRUE(child_hwnd != NULL);

  if (child_hwnd != NULL) {
    CWindow child(child_hwnd);
    EXPECT_TRUE(child.IsWindowVisible());
    RECT child_dimensions;
    EXPECT_TRUE(child.GetWindowRect(&child_dimensions));
    EXPECT_TRUE(window_.ScreenToClient(&child_dimensions));
    EXPECT_TRUE(::EqualRect(&child_dimensions, &dimensions));
  }

  // Being visible doesn't change the desired height
  EXPECT_EQ(desired_height, content_->GetDesiredSize(400, 0));

  content_.reset();

  EXPECT_TRUE(window_.GetZeroOrOneChildWindows() == NULL);
}

TEST_F(ReadyPromptWindowTest, Destroy) {
  // Should delete associated mocks, not invoke on ReadyModeState
  ready_prompt_window_->DestroyWindow();
}

TEST_F(ReadyPromptWindowButtonTest, ClickEnable) {
  EXPECT_CALL(*state_, AcceptChromeFrame());
  ASSERT_TRUE(ClickOnCaption(L"Enable"));
  RunUntilCloseInfobar();
}

TEST_F(ReadyPromptWindowButtonTest, ClickIgnore) {
  EXPECT_CALL(*state_, PermanentlyDeclineChromeFrame());
  ASSERT_TRUE(ClickOnCaption(L"Ignore"));
  RunUntilCloseInfobar();
}

// TODO(erikwright): test WebBrowserAdapter
// TODO(erikwright): an integration test of ReadyMode::Configure with a mock
//                   IWebBrowser2?