plugin_test.h revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
1// Copyright (c) 2011 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 CONTENT_TEST_PLUGIN_PLUGIN_TEST_H_
6#define CONTENT_TEST_PLUGIN_PLUGIN_TEST_H_
7
8#include <string>
9
10#include "third_party/npapi/bindings/npapi.h"
11#include "third_party/npapi/bindings/nphostapi.h"
12
13namespace NPAPIClient {
14
15// A PluginTest represents an instance of the plugin, which in
16// our case is a test case.
17class PluginTest {
18 public:
19  // Constructor.
20  PluginTest(NPP id, NPNetscapeFuncs *host_functions);
21
22  // Destructor
23  virtual ~PluginTest();
24
25  // Returns true if the test runs in windowless plugin mode.
26  virtual bool IsWindowless() const;
27
28  //
29  // NPAPI Functions
30  //
31  virtual NPError New(uint16 mode, int16 argc, const char* argn[],
32                      const char* argv[], NPSavedData* saved);
33  virtual NPError Destroy();
34  virtual NPError SetWindow(NPWindow* pNPWindow);
35  virtual NPError NewStream(NPMIMEType type, NPStream* stream,
36                            NPBool seekable, uint16* stype);
37  virtual int32   WriteReady(NPStream *stream);
38  virtual int32   Write(NPStream *stream, int32 offset, int32 len,
39                        void *buffer);
40  virtual NPError DestroyStream(NPStream *stream, NPError reason);
41  virtual void    StreamAsFile(NPStream* stream, const char* fname);
42  virtual void    URLNotify(const char* url, NPReason reason, void* data);
43  virtual int16   HandleEvent(void* event);
44  virtual void    URLRedirectNotify(const char* url, int32_t status,
45                                    void* notify_data);
46
47  // Returns true if the test has not had any errors.
48  bool Succeeded() { return test_status_.length() == 0; }
49
50  // Sets an error for the test case.  Appends the msg to the
51  // error that will be returned from the test.
52  void SetError(const std::string &msg);
53
54  // Expect two string values are equal, and if not, logs an
55  // appropriate error about it.
56  void ExpectStringLowerCaseEqual(const std::string &val1,
57                                  const std::string &val2);
58
59  // Expect two values to not be equal, and if they are
60  // logs an appropriate error about it.
61  void ExpectAsciiStringNotEqual(const char *val1, const char *val2);
62
63  // Expect two integer values are equal, and if not, logs an
64  // appropriate error about it.
65  void ExpectIntegerEqual(int val1, int val2);
66
67  // Signals to the Test that invoked us that the test is
68  // completed.  This is done by forcing the plugin to
69  // set a cookie in the browser window, which the test program
70  // is waiting for.  Note - because this is done by
71  // using javascript, the browser must have the frame
72  // setup before the plugin calls this function.  So plugin
73  // tests MUST NOT call this function prior to having
74  // received the SetWindow() callback from the browser.
75  void SignalTestCompleted();
76
77 protected:
78  // Helper function to lookup names in the input array.
79  // If the name is found, returns the value, otherwise
80  // returns NULL.
81  const char *GetArgValue(const char *name, const int16 argc,
82                          const char *argn[], const char *argv[]);
83
84  // Access to the list of functions provided
85  // by the NPAPI host.
86  NPNetscapeFuncs *HostFunctions() { return host_functions_; }
87
88  // The NPP Identifier for this plugin instance.
89  NPP id() { return id_; }
90  std::string test_id() const { return test_id_; }
91  std::string test_name() const { return test_name_; }
92  bool test_completed() const { return test_completed_; }
93 private:
94  NPP                       id_;
95  NPNetscapeFuncs *         host_functions_;
96  std::string               test_name_;
97  std::string               test_id_;
98  std::string               test_status_;
99  bool                      test_completed_;
100};
101
102}  // namespace NPAPIClient
103
104#endif  // CONTENT_TEST_PLUGIN_PLUGIN_TEST_H_
105