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

// 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:
  explicit AutomationProxyForExternalTab(int execution_timeout);

  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_;
  }

  // Waits for the DidNavigate event to be processed on the current thread.
  // Returns true if the event arrived, false if there was a timeout.
  bool WaitForNavigationComplete(int max_time_to_wait_ms);

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

  void OnDidNavigate(int tab_handle, int navigation_type, int relative_offset,
                     const GURL& url) {
    navigate_complete_ = true;
  }

  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_;
};

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

#if defined(OS_WIN)
// Custom message loop for external tab testing.
//
// Creates a window and makes external_tab_window (the external tab's
// window handle) a child of that window.
//
// The time_to_wait_ms parameter is the maximum time the loop will run. To
// end the loop earlier, post a quit message (using the Win32
// PostQuitMessage API) to the thread.
bool ExternalTabMessageLoop(HWND external_tab_window,
                            int time_to_wait_ms);
#endif  // defined(OS_WIN)

#endif  // CHROME_TEST_AUTOMATION_AUTOMATION_PROXY_UITEST_H__