test_utils.h revision 868fa2fe829687343ffae624259930155e16dbd8
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#ifndef CHROME_FRAME_TEST_UTILS_H_
6#define CHROME_FRAME_TEST_UTILS_H_
7
8#include <string>
9
10#include <atlbase.h>
11#include <atlcom.h>
12
13#include "base/strings/string16.h"
14
15namespace base {
16class FilePath;
17}
18
19extern const wchar_t kChromeFrameDllName[];
20extern const wchar_t kChromeLauncherExeName[];
21
22// Helper class used to register different chrome frame DLLs while running
23// tests. The default constructor registers the DLL found in the build path.
24// Programs that use this class MUST include a call to the class's
25// RegisterAndExitProcessIfDirected method at the top of their main entrypoint.
26//
27// At destruction, again registers the DLL found in the build path if another
28// DLL has since been registered. Triggers GTEST asserts on failure.
29//
30// TODO(robertshield): Ideally, make this class restore the originally
31// registered chrome frame DLL (e.g. by looking in HKCR) on destruction.
32class ScopedChromeFrameRegistrar {
33 public:
34  enum RegistrationType {
35    PER_USER,
36    SYSTEM_LEVEL,
37  };
38
39  explicit ScopedChromeFrameRegistrar(RegistrationType registration_type);
40  ScopedChromeFrameRegistrar(const std::wstring& path,
41                             RegistrationType registration_type);
42  virtual ~ScopedChromeFrameRegistrar();
43
44  void RegisterChromeFrameAtPath(const std::wstring& path);
45  void UnegisterChromeFrameAtPath(const std::wstring& path);
46  void RegisterReferenceChromeFrameBuild();
47
48  std::wstring GetChromeFrameDllPath() const;
49
50  static void RegisterAtPath(const std::wstring& path,
51                             RegistrationType registration_type);
52  static void UnregisterAtPath(const std::wstring& path,
53                               RegistrationType registration_type);
54  static void RegisterDefaults();
55  static base::FilePath GetReferenceChromeFrameDllPath();
56
57  // Registers or unregisters a COM DLL and exits the process if the process's
58  // command line is:
59  // this.exe --call-registration-entrypoint path_to_dll entrypoint
60  // Otherwise simply returns. This method should be invoked at the start of the
61  // entrypoint in each executable that uses ScopedChromeFrameRegistrar to
62  // register or unregister DLLs.
63  static void RegisterAndExitProcessIfDirected();
64
65 private:
66  enum RegistrationOperation {
67    REGISTER,
68    UNREGISTER,
69  };
70
71  // The string "--call-registration-entrypoint".
72  static const wchar_t kCallRegistrationEntrypointSwitch[];
73
74  static void DoRegistration(const string16& path,
75                             RegistrationType registration_type,
76                             RegistrationOperation registration_operation);
77
78  // Contains the path of the most recently registered Chrome Frame DLL.
79  std::wstring new_chrome_frame_dll_path_;
80
81  // Contains the path of the Chrome Frame DLL to be registered at destruction.
82  std::wstring original_dll_path_;
83
84  // Indicates whether per user or per machine registration is needed.
85  RegistrationType registration_type_;
86  // We need to register the chrome path provider only once per process. This
87  // flag keeps track of that.
88  static bool register_chrome_path_provider_;
89};
90
91// Returns the path to the Chrome Frame DLL in the build directory. Assumes
92// that the test executable is running from the build folder or a similar
93// folder structure.
94base::FilePath GetChromeFrameBuildPath();
95
96// Callback description for onload, onloaderror, onmessage
97static _ATL_FUNC_INFO g_single_param = {CC_STDCALL, VT_EMPTY, 1, {VT_VARIANT}};
98// Simple class that forwards the callbacks.
99template <typename T>
100class DispCallback
101    : public IDispEventSimpleImpl<1, DispCallback<T>, &IID_IDispatch> {
102 public:
103  typedef HRESULT (T::*Method)(const VARIANT* param);
104
105  DispCallback(T* owner, Method method) : owner_(owner), method_(method) {
106  }
107
108  BEGIN_SINK_MAP(DispCallback)
109    SINK_ENTRY_INFO(1, IID_IDispatch, DISPID_VALUE, OnCallback, &g_single_param)
110  END_SINK_MAP()
111
112  virtual ULONG STDMETHODCALLTYPE AddRef() {
113    return owner_->AddRef();
114  }
115  virtual ULONG STDMETHODCALLTYPE Release() {
116    return owner_->Release();
117  }
118
119  STDMETHOD(OnCallback)(VARIANT param) {
120    return (owner_->*method_)(&param);
121  }
122
123  IDispatch* ToDispatch() {
124    return reinterpret_cast<IDispatch*>(this);
125  }
126
127  T* owner_;
128  Method method_;
129};
130
131// If the workstation is locked and cannot receive user input.
132bool IsWorkstationLocked();
133
134#endif  // CHROME_FRAME_TEST_UTILS_H_
135