summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/web_ui_browsertest.h
blob: 91f136d697028f67ac353a72cc1d52ca3107c7ad (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
// Copyright (c) 2011 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_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_
#define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_
#pragma once

#include <string>

#include "base/file_path.h"
#include "chrome/browser/ui/webui/web_ui_test_handler.h"
#include "chrome/test/in_process_browser_test.h"

class Value;
class WebUIMessageHandler;

// This macro simplifies the declaration of simple javascript unit tests.
// Use:
//   WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest);
#define WEB_UI_UNITTEST_F(x, y) \
  IN_PROC_BROWSER_TEST_F(x, y) { \
    ASSERT_TRUE(RunJavascriptTest(#y)); \
  }

// The runner of WebUI javascript based tests.
// See chrome/test/data/webui/test_api.js for the javascript side test API's.
//
// These tests should follow the form given in:
// chrome/test/data/webui/sample_downloads.js.
// and the lone test within this class.
class WebUIBrowserTest : public InProcessBrowserTest {
 public:
  typedef std::vector<const Value*> ConstValueVector;
  virtual ~WebUIBrowserTest();

  // Add a custom helper JS library for your test.
  // If a relative path is specified, it'll be read
  // as relative to the test data dir.
  void AddLibrary(const FilePath& library_path);

  // Runs a javascript function in the context of all libraries.
  // Note that calls to functions in test_api.js are not supported.
  bool RunJavascriptFunction(const std::string& function_name);
  bool RunJavascriptFunction(const std::string& function_name,
                             const Value& arg);
  bool RunJavascriptFunction(const std::string& function_name,
                             const Value& arg1,
                             const Value& arg2);
  bool RunJavascriptFunction(const std::string& function_name,
                             const ConstValueVector& function_arguments);

  // Runs a test that may include calls to functions in test_api.js.
  bool RunJavascriptTest(const std::string& test_name);
  bool RunJavascriptTest(const std::string& test_name,
                         const Value& arg);
  bool RunJavascriptTest(const std::string& test_name,
                         const Value& arg1,
                         const Value& arg2);
  bool RunJavascriptTest(const std::string& test_name,
                         const ConstValueVector& test_arguments);

  // Preloads the javascript libraries and sets the |libraries_preloaded| flag
  // to prevent re-loading at next javascript invocation. If
  // |override_chrome_send| is true, then chrome.send is overridden for
  // javascript to register handlers.
  void PreLoadJavascriptLibraries(bool override_chrome_send);

 protected:
  WebUIBrowserTest();

  // Setup test path.
  virtual void SetUpInProcessBrowserTestFixture();

  // Returns a mock WebUI object under test (if any).
  virtual WebUIMessageHandler* GetMockMessageHandler();

  // Skip this test with |skip_test_message|.
  void skipTest(const std::string& skip_test_message);

  // Returns a file:// GURL constructed from |path| inside the test data dir for
  // webui tests.
  static GURL WebUITestDataPathToURL(const FilePath::StringType& path);

 private:
  // Builds a string containing all added javascript libraries.
  void BuildJavascriptLibraries(std::string* content);

  // Builds a string with a call to the runTest JS function, passing the
  // given test and its arguments.
  string16 BuildRunTestJSCall(const std::string& test_name,
                              const WebUIBrowserTest::ConstValueVector& args);

  // Calls the specified function with all libraries available. If |is_test|
  // is true, the framework listens for pass fail messages from javascript.
  // The provided arguments vector is passed to |function_name|.
  bool RunJavascriptUsingHandler(const std::string& function_name,
                                 const ConstValueVector& function_arguments,
                                 bool is_test);

  // Attaches mock and test handlers.
  void SetupHandlers();

  // Handles test framework messages.
  scoped_ptr<WebUITestHandler> test_handler_;

  // Location of test data (currently test/data/webui).
  FilePath test_data_directory_;

  // User added libraries
  std::vector<FilePath> user_libraries_;

  // Indicates that the libraries have been pre-loaded and to not load them
  // again.
  bool libraries_preloaded_;

  bool skip_test_;
  std::string skip_test_message_;
};

#endif  // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_