summaryrefslogtreecommitdiffstats
path: root/chrome/test/url_fetch_test/url_fetch_test.cc
blob: 76f73c340fe3794eb8bc65737861ca03d41611cd (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
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2010 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.

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/ui/ui_test.h"

namespace {

// Provides a UI Test that lets us take the browser to a url, and
// wait for a cookie value to be set before closing the page.
class UrlFetchTest : public UITest {
 public:
  UrlFetchTest() {
    show_window_ = true;
    dom_automation_enabled_ = true;
  }
  struct UrlFetchTestResult {
    std::string cookie_value;
    std::string javascript_variable;
  };

  void SetUp() {
    const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
    if (cmd_line->HasSwitch("reference_build")) {
      FilePath dir;
      PathService::Get(chrome::DIR_TEST_TOOLS, &dir);
      dir = dir.AppendASCII("reference_build");
#if defined(OS_WIN)
      dir = dir.AppendASCII("chrome");
#elif defined(OS_LINUX)
      dir = dir.AppendASCII("chrome_linux");
#elif defined(OS_MACOSX)
      dir = dir.AppendASCII("chrome_mac");
#endif
      browser_directory_ = dir;
    }
    UITest::SetUp();

    // We've shortened the default timeouts, but dom_perf on Vista needs longer
    // timeouts. Setting timeouts to the previous values before r52666.
    set_action_timeout_ms(std::max(60000, action_timeout_ms()));
  }

  void RunTest(const GURL& url, const char* wait_cookie_name,
               const char* wait_cookie_value, const char* var_to_fetch,
               UrlFetchTestResult* result) {
    scoped_refptr<TabProxy> tab(GetActiveTab());
    ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url));

    if (wait_cookie_name) {
      if (wait_cookie_value) {
        bool completed = WaitUntilCookieValue(tab.get(), url, wait_cookie_name,
                                              UITest::test_timeout_ms(),
                                              wait_cookie_value);
        ASSERT_TRUE(completed);
      } else {
        result->cookie_value = WaitUntilCookieNonEmpty(
            tab.get(), url, wait_cookie_name, UITest::test_timeout_ms());
        ASSERT_TRUE(result->cookie_value.length());
      }
    }
    if (var_to_fetch) {
      std::string script = StringPrintf(
          "window.domAutomationController.send(%s);", var_to_fetch);

      std::wstring value;
      bool success = tab->ExecuteAndExtractString(L"", ASCIIToWide(script),
                                                  &value);
      ASSERT_TRUE(success);
      result->javascript_variable = WideToUTF8(value);
    }
  }
};

bool WriteValueToFile(std::string value, const FilePath& path) {
  int retval = file_util::WriteFile(path, value.c_str(), value.length());
  return retval == static_cast<int>(value.length());
}

// To actually do anything useful, this test should have a url
// passed on the command line, eg.
//
// --url=http://foo.bar.com
//
// Additional arguments:
//
// --wait_cookie_name=<name>
//   Waits for a cookie named <name> to be set before exiting successfully.
//
// --wait_cookie_value=<value>
//   In conjunction with --wait_cookie_name, this waits for a specific value
//   to be set. (Incompatible with --wait_cookie_output)
//
// --wait_cookie_output=<filepath>
//   In conjunction with --wait_cookie_name, this saves the cookie value to
//   a file at the given path. (Incompatible with --wait_cookie_value)
//
// --jsvar=<name>
//   At the end of the test, fetch the named javascript variable from the page.
//
// --jsvar_output=<filepath>
//   Write the value of the variable named by '--jsvar' to a file at the given
//   path.
//
// --reference_build
//   Use the reference build of chrome for the test.
TEST_F(UrlFetchTest, UrlFetch) {
  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();

  if (!cmd_line->HasSwitch("url"))
    return;

  std::string cookie_name =
      cmd_line->GetSwitchValueASCII("wait_cookie_name");
  std::string cookie_value =
      cmd_line->GetSwitchValueASCII("wait_cookie_value");

  std::string jsvar = cmd_line->GetSwitchValueASCII("jsvar");

  UrlFetchTestResult result;
  RunTest(GURL(cmd_line->GetSwitchValueASCII("url")),
          cookie_name.length() > 0 ? cookie_name.c_str() : NULL,
          cookie_value.length() > 0 ? cookie_value.c_str() : NULL,
          jsvar.length() > 0 ? jsvar.c_str() : NULL,
          &result);

  // Write out the cookie if requested
  FilePath cookie_output_path =
      cmd_line->GetSwitchValuePath("wait_cookie_output");
  if (cookie_output_path.value().size() > 0) {
    ASSERT_TRUE(WriteValueToFile(result.cookie_value, cookie_output_path));
  }

  // Write out the JS Variable if requested
  FilePath jsvar_output_path = cmd_line->GetSwitchValuePath("jsvar_output");
  if (jsvar_output_path.value().size() > 0) {
    ASSERT_TRUE(WriteValueToFile(result.javascript_variable,
                                 jsvar_output_path));
  }
}

}  // namespace