summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/automation_proxy_uitest.h
blob: 7ca2662879e9cf8a8450bab4d86956872c760fa5 (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
// Copyright (c) 2006-2008 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__

#include <string>

#include "base/gfx/native_widget_types.h"
#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 "googleurl/src/gurl.h"

class TabProxy;

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

// Automation proxy UITest that allows tests to override the automation
// proxy used by the UITest base class.
template <class AutomationProxyClass>
class CustomAutomationProxyTest : public AutomationProxyVisibleTest {
 protected:
  CustomAutomationProxyTest() {
  }

  // Override UITest's CreateAutomationProxy to provide our the unit test
  // with our special implementation of AutomationProxy.
  // This function is called from within UITest::LaunchBrowserAndServer.
  virtual AutomationProxy* CreateAutomationProxy(int execution_timeout) {
    AutomationProxyClass* proxy = new AutomationProxyClass(execution_timeout);
    return proxy;
  }
};

// A single-use AutomationProxy implementation that's good
// for a single navigation and a single ForwardMessageToExternalHost
// message.  Once the ForwardMessageToExternalHost message is received
// the class posts a quit message to the thread on which the message
// was received.
class AutomationProxyForExternalTab : public AutomationProxy {
 public:
  // Allows us to reuse this mock for multiple tests. This is done
  // by setting a state to trigger posting of Quit message to the
  // wait loop.
  enum QuitAfter {
    QUIT_INVALID,
    QUIT_AFTER_NAVIGATION,
    QUIT_AFTER_MESSAGE,
  };

  explicit AutomationProxyForExternalTab(int execution_timeout);
  ~AutomationProxyForExternalTab();

  int messages_received() const {
    return messages_received_;
  }

  const std::string& message() const {
    return message_;
  }

  const std::string& origin() const {
    return origin_;
  }

  const std::string& target() const {
    return target_;
  }

  // Creates and sisplays a top-level window, that can be used as a parent
  // to the external tab.window.
  gfx::NativeWindow CreateHostWindow();
  scoped_refptr<TabProxy> CreateTabWithHostWindow(bool is_incognito,
      const GURL& initial_url, gfx::NativeWindow* container_wnd,
      gfx::NativeWindow* tab_wnd);
  void DestroyHostWindow();

  // Wait for the event to happen or timeout
  bool WaitForNavigation(int timeout_ms);
  bool WaitForMessage(int timeout_ms);
  bool WaitForTabCleanup(TabProxy* tab, int timeout_ms);

  // Enters a message loop that processes window messages as well
  // as calling MessageLoop::current()->RunAllPending() to process any
  // incoming IPC messages. The timeout_ms parameter is the maximum
  // time the loop will run. To end the loop earlier, post a quit message to
  // the thread.
  bool RunMessageLoop(int timeout_ms, gfx::NativeWindow window_to_monitor);

 protected:
#if defined(OS_WIN)
  static const int kQuitLoopMessage = WM_APP + 11;
  // Quit the message loop
  void QuitLoop() {
    DCHECK(IsWindow(host_window_));
    // We could post WM_QUIT but lets keep it out of accidental usage
    // by anyone else peeking it.
    PostMessage(host_window_, kQuitLoopMessage, 0, 0);
  }
#endif  // defined(OS_WIN)

  // Internal state to flag posting of a quit message to the loop
  void set_quit_after(QuitAfter q) {
    quit_after_ = q;
  }

  virtual void OnMessageReceived(const IPC::Message& msg);

  void OnDidNavigate(int tab_handle, const IPC::NavigationInfo& nav_info);
  void OnForwardMessageToExternalHost(int handle,
                                      const std::string& message,
                                      const std::string& origin,
                                      const std::string& target);

 protected:
  bool navigate_complete_;
  int messages_received_;
  std::string message_, origin_, target_;
  QuitAfter quit_after_;
  const wchar_t* host_window_class_;
  gfx::NativeWindow host_window_;
};

// A test harness for testing external tabs.
typedef CustomAutomationProxyTest<AutomationProxyForExternalTab>
    ExternalTabTestType;

#endif  // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H__