summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/automation_proxy_uitest.h
blob: 55da8cc8ee0b3ed7bc4268c403faab24b594f3c4 (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
// 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.

#ifndef CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_
#define CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_
#pragma once

#include <string>

#include "base/message_loop.h"
#include "base/platform_thread.h"
#include "base/time.h"
#include "chrome/test/automation/automation_proxy.h"
#include "chrome/test/ui/ui_test.h"
#include "gfx/native_widget_types.h"
#include "googleurl/src/gurl.h"
#include "testing/gmock/include/gmock/gmock.h"

class TabProxy;
class ExternalTabUITestMockClient;

// Base class for automation proxy testing.
class AutomationProxyVisibleTest : public UITest {
 protected:
  AutomationProxyVisibleTest() {
    show_window_ = true;
  }
};

// Used to implement external tab UI tests.
//
// We have to derive from AutomationProxy in order to hook up
// OnMessageReceived callbacks.
class ExternalTabUITestMockClient : public AutomationProxy {
 public:
  explicit ExternalTabUITestMockClient(int execution_timeout);
  ~ExternalTabUITestMockClient() {
    EXPECT_TRUE(host_window_ == NULL);
  }

  MOCK_METHOD2(OnDidNavigate, void(int tab_handle,
      const IPC::NavigationInfo& nav_info));
  MOCK_METHOD4(OnForwardMessageToExternalHost, void(int handle,
      const std::string& message, const std::string& origin,
      const std::string& target));
  MOCK_METHOD3(OnRequestStart, void(int tab_handle, int request_id,
      const IPC::AutomationURLRequest& request));
  MOCK_METHOD3(OnRequestRead, void(int tab_handle, int request_id,
      int bytes_to_read));
  MOCK_METHOD3(OnRequestEnd, void(int tab_handle, int request_id,
      const URLRequestStatus& status));
  MOCK_METHOD3(OnSetCookieAsync, void(int tab_handle, const GURL& url,
      const std::string& cookie));
  MOCK_METHOD1(HandleClosed, void(int handle));


  MOCK_METHOD4(OnOpenURL, void(int tab_handle, const GURL& url,
      const GURL& referrer, int open_disposition));
  MOCK_METHOD3(OnNavigationStateChanged, void(int tab_handle, int flags,
      const IPC::NavigationInfo& nav_info));
  MOCK_METHOD2(OnAttachExternalTab, void(int tab_handle,
      const IPC::AttachExternalTabParams& params));
  MOCK_METHOD2(OnLoad, void(int tab_handle, const GURL&url));


  // Action helpers for OnRequest* incoming messages. Create the message and
  // delegate sending to the base class. Apparently we do not have wrappers
  // in AutomationProxy for these messages.
  void ReplyStarted(const IPC::AutomationURLResponse* response,
                    int tab_handle, int request_id);
  void ReplyData(const std::string* data, int tab_handle, int request_id);
  void ReplyEOF(int tab_handle, int request_id);
  void ReplyEnd(const URLRequestStatus& status, int tab_handle, int request_id);
  void Reply404(int tab_handle, int request_id);

  // Test setup helpers
  scoped_refptr<TabProxy> CreateHostWindowAndTab(
      const IPC::ExternalTabSettings& settings);
  scoped_refptr<TabProxy> CreateTabWithUrl(const GURL& initial_url);
  void NavigateInExternalTab(int tab_handle, const GURL& url,
                             const GURL& referrer = GURL());

  // Navigation using keyboard (TAB + Enter) inside the page.
  void NavigateThroughUserGesture();
  void IgnoreFavIconNetworkRequest();

  void ConnectToExternalTab(gfx::NativeWindow parent,
      const IPC::AttachExternalTabParams& attach_params);
  // Helper for network requests.
  void ServeHTMLData(int tab_handle, const GURL& url, const std::string& data);
  // Destroys the host window.
  void DestroyHostWindow();
  // Returns true if the host window exists.
  bool HostWindowExists();
  // It's overlapped and visible by default. For Incognito mode test though we
  // want invisible window since SetCookie is a sync call and a deadlock is
  // very possible.
  unsigned long host_window_style_;

  static const IPC::AutomationURLResponse http_200;
  static const IPC::ExternalTabSettings default_settings;
 protected:
  gfx::NativeWindow host_window_;

  // Simple dispatcher to above OnXXX methods.
  virtual void OnMessageReceived(const IPC::Message& msg);
  virtual void InvalidateHandle(const IPC::Message& message);
};

// Base your external tab UI tests on this.
class ExternalTabUITest : public UITest {
 public:
  ExternalTabUITest() : UITest(MessageLoop::TYPE_UI) {}
  // Override UITest's CreateAutomationProxy to provide the unit test
  // with our special implementation of AutomationProxy.
  // This function is called from within UITest::LaunchBrowserAndServer.
  virtual AutomationProxy* CreateAutomationProxy(int execution_timeout);
 protected:
  // Filtered Inet will override automation callbacks for network resources.
  virtual bool ShouldFilterInet() {
    return false;
  }
  ExternalTabUITestMockClient* mock_;
};

#endif  // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H_