<commit_before>// 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 <string>

#include "base/rand_util.h"
#include "chrome/browser/webdata/web_database.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/chrome_constants.h"
#include "chrome_frame/test/mock_ie_event_sink_actions.h"
#include "chrome_frame/test/mock_ie_event_sink_test.h"

using testing::_;

namespace chrome_frame_test {

class DeleteBrowsingHistoryTest
    : public MockIEEventSinkTest,
      public testing::Test {
 public:
  DeleteBrowsingHistoryTest() {}

  virtual void SetUp() {
    // We will use the OnAccLoad event to monitor page loads, so we ignore
    // these.
    ie_mock_.ExpectAnyNavigations();
    ie_mock2_.ExpectAnyNavigations();
    ie_mock3_.ExpectAnyNavigations();
    EXPECT_CALL(acc_observer_, OnAccDocLoad(_)).Times(testing::AnyNumber());

    // Use a random image_path to ensure that a prior run does not
    // interfere with our expectations about caching.
    image_path_ = L"/" + RandomChars(32);
    topHtml =
        "<html><head>"
        "<meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
        "</head>"
        "<body>"
        "<form method=\"POST\" action=\"/form\">"
        "<input title=\"username\" type=\"text\" name=\"username\" />"
        "<input type=\"submit\" title=\"Submit\" name=\"Submit\" />"
        "</form>"
        "<img alt=\"Blank image.\" src=\"" + WideToASCII(image_path_) + "\" />"
        "This is some text.</body></html>";
  }

 protected:
  std::wstring image_path_;
  std::string topHtml;

  testing::NiceMock<MockAccEventObserver> acc_observer_;
  MockWindowObserver delete_browsing_history_window_observer_mock_;
  MockObjectWatcherDelegate ie_process_exit_watcher_mock_;

  testing::StrictMock<MockIEEventSink> ie_mock2_;
  testing::StrictMock<MockIEEventSink> ie_mock3_;

  // Returns a string of |count| lowercase random characters.
  static std::wstring RandomChars(int count) {
    srand(static_cast<unsigned int>(time(NULL)));
    std::wstring str;
    for (int i = 0; i < count; ++i)
      str += L'a' + base::RandInt(0, 25);
    return str;
  }
};

namespace {

const wchar_t* kFormFieldName = L"username";
const wchar_t* kFormFieldValue = L"test_username";

const char* kHtmlHttpHeaders =
    "HTTP/1.1 200 OK\r\n"
    "Connection: close\r\n"
    "Content-Type: text/html\r\n";
const char* kFormResultHtml =
    "<html><head><meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
    "</head><body>Nice work.</body></html>";
const char* kBlankPngResponse[] = {
    "HTTP/1.1 200 OK\r\n"
    "Connection: close\r\n"
    "Content-Type: image/png\r\n"
    "Cache-Control: max-age=3600, must-revalidate\r\n",
    "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
    "\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00\x25\xdb\x56"
    "\xca\x00\x00\x00\x03\x50\x4c\x54\x45\x00\x00\x00\xa7\x7a\x3d\xda"
    "\x00\x00\x00\x01\x74\x52\x4e\x53\x00\x40\xe6\xd8\x66\x00\x00\x00"
    "\x0a\x49\x44\x41\x54\x08\xd7\x63\x60\x00\x00\x00\x02\x00\x01\xe2"
    "\x21\xbc\x33\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82"};

const size_t kBlankPngFileLength = 95;
}  // anonymous namespace

// Looks up |element_name| in the Chrome form data DB and ensures that the
// results match |matcher|.
ACTION_P2(ExpectFormValuesForElementNameMatch, element_name, matcher) {
  FilePath profile_path(
      chrome_frame_test::GetProfilePath(kIexploreProfileName)
          .Append(L"Default").Append(chrome::kWebDataFilename));

  WebDatabase web_database;
  sql::InitStatus init_status = web_database.Init(profile_path);
  EXPECT_EQ(sql::INIT_OK, init_status);

  if (init_status == sql::INIT_OK) {
    std::vector<string16> values;
    web_database.GetFormValuesForElementName(element_name, L"", &values, 9999);
    EXPECT_THAT(values, matcher);
  }
}

// Launch |ie_mock| and navigate it to |url|.
ACTION_P2(LaunchThisIEAndNavigate, ie_mock, url) {
  EXPECT_HRESULT_SUCCEEDED(ie_mock->event_sink()->LaunchIEAndNavigate(url,
                                                                      ie_mock));
}

TEST_F(DeleteBrowsingHistoryTest, CFDeleteBrowsingHistory) {
  if (GetInstalledIEVersion() < IE_8) {
    LOG(ERROR) << "Test does not apply to IE versions < 8.";
    return;
  }
  delete_browsing_history_window_observer_mock_.WatchWindow(
      "Delete Browsing History");

  // For some reason, this page is occasionally being cached, so we randomize
  // its name to ensure that, at least the first time we request it, it is
  // retrieved.
  std::wstring top_name = RandomChars(32);
  std::wstring top_url = server_mock_.Resolve(top_name);
  std::wstring top_path = L"/" + top_name;

  // Even still, it might not be hit the second or third time, so let's just
  // not worry about how often or whether it's called
  EXPECT_CALL(server_mock_, Get(_, testing::StrEq(top_path), _))
      .Times(testing::AnyNumber())
      .WillRepeatedly(SendFast(kHtmlHttpHeaders, topHtml));

  testing::InSequence expect_in_sequence_for_scope;

  // First launch will hit the server, requesting top.html and then image_path_
  EXPECT_CALL(server_mock_, Get(_, testing::StrEq(image_path_), _))
      .WillOnce(SendFast(kBlankPngResponse[0],
                         std::string(kBlankPngResponse[1],
                                     kBlankPngFileLength)));

  // top.html contains a form. Fill in the username field and submit, causing<commit_msg>Disable this test, which works 100 out of 100 times locally, but never on the Chrome Frame Tests (IE8) build agent.<commit_after>// 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 <string>

#include "base/rand_util.h"
#include "chrome/browser/webdata/web_database.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/chrome_constants.h"
#include "chrome_frame/test/mock_ie_event_sink_actions.h"
#include "chrome_frame/test/mock_ie_event_sink_test.h"

using testing::_;

namespace chrome_frame_test {

class DeleteBrowsingHistoryTest
    : public MockIEEventSinkTest,
      public testing::Test {
 public:
  DeleteBrowsingHistoryTest() {}

  virtual void SetUp() {
    // We will use the OnAccLoad event to monitor page loads, so we ignore
    // these.
    ie_mock_.ExpectAnyNavigations();
    ie_mock2_.ExpectAnyNavigations();
    ie_mock3_.ExpectAnyNavigations();
    EXPECT_CALL(acc_observer_, OnAccDocLoad(_)).Times(testing::AnyNumber());

    // Use a random image_path to ensure that a prior run does not
    // interfere with our expectations about caching.
    image_path_ = L"/" + RandomChars(32);
    topHtml =
        "<html><head>"
        "<meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
        "</head>"
        "<body>"
        "<form method=\"POST\" action=\"/form\">"
        "<input title=\"username\" type=\"text\" name=\"username\" />"
        "<input type=\"submit\" title=\"Submit\" name=\"Submit\" />"
        "</form>"
        "<img alt=\"Blank image.\" src=\"" + WideToASCII(image_path_) + "\" />"
        "This is some text.</body></html>";
  }

 protected:
  std::wstring image_path_;
  std::string topHtml;

  testing::NiceMock<MockAccEventObserver> acc_observer_;
  MockWindowObserver delete_browsing_history_window_observer_mock_;
  MockObjectWatcherDelegate ie_process_exit_watcher_mock_;

  testing::StrictMock<MockIEEventSink> ie_mock2_;
  testing::StrictMock<MockIEEventSink> ie_mock3_;

  // Returns a string of |count| lowercase random characters.
  static std::wstring RandomChars(int count) {
    srand(static_cast<unsigned int>(time(NULL)));
    std::wstring str;
    for (int i = 0; i < count; ++i)
      str += L'a' + base::RandInt(0, 25);
    return str;
  }
};

namespace {

const wchar_t* kFormFieldName = L"username";
const wchar_t* kFormFieldValue = L"test_username";

const char* kHtmlHttpHeaders =
    "HTTP/1.1 200 OK\r\n"
    "Connection: close\r\n"
    "Content-Type: text/html\r\n";
const char* kFormResultHtml =
    "<html><head><meta http-equiv=\"x-ua-compatible\" content=\"chrome=1\" />"
    "</head><body>Nice work.</body></html>";
const char* kBlankPngResponse[] = {
    "HTTP/1.1 200 OK\r\n"
    "Connection: close\r\n"
    "Content-Type: image/png\r\n"
    "Cache-Control: max-age=3600, must-revalidate\r\n",
    "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
    "\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00\x25\xdb\x56"
    "\xca\x00\x00\x00\x03\x50\x4c\x54\x45\x00\x00\x00\xa7\x7a\x3d\xda"
    "\x00\x00\x00\x01\x74\x52\x4e\x53\x00\x40\xe6\xd8\x66\x00\x00\x00"
    "\x0a\x49\x44\x41\x54\x08\xd7\x63\x60\x00\x00\x00\x02\x00\x01\xe2"
    "\x21\xbc\x33\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82"};

const size_t kBlankPngFileLength = 95;
}  // anonymous namespace

// Looks up |element_name| in the Chrome form data DB and ensures that the
// results match |matcher|.
ACTION_P2(ExpectFormValuesForElementNameMatch, element_name, matcher) {
  FilePath profile_path(
      chrome_frame_test::GetProfilePath(kIexploreProfileName)
          .Append(L"Default").Append(chrome::kWebDataFilename));

  WebDatabase web_database;
  sql::InitStatus init_status = web_database.Init(profile_path);
  EXPECT_EQ(sql::INIT_OK, init_status);

  if (init_status == sql::INIT_OK) {
    std::vector<string16> values;
    web_database.GetFormValuesForElementName(element_name, L"", &values, 9999);
    EXPECT_THAT(values, matcher);
  }
}

// Launch |ie_mock| and navigate it to |url|.
ACTION_P2(LaunchThisIEAndNavigate, ie_mock, url) {
  EXPECT_HRESULT_SUCCEEDED(ie_mock->event_sink()->LaunchIEAndNavigate(url,
                                                                      ie_mock));
}

TEST_F(DeleteBrowsingHistoryTest, DISABLED_CFDeleteBrowsingHistory) {
  if (GetInstalledIEVersion() < IE_8) {
    LOG(ERROR) << "Test does not apply to IE versions < 8.";
    return;
  }
  delete_browsing_history_window_observer_mock_.WatchWindow(
      "Delete Browsing History");

  // For some reason, this page is occasionally being cached, so we randomize
  // its name to ensure that, at least the first time we request it, it is
  // retrieved.
  std::wstring top_name = RandomChars(32);
  std::wstring top_url = server_mock_.Resolve(top_name);
  std::wstring top_path = L"/" + top_name;

  // Even still, it might not be hit the second or third time, so let's just
  // not worry about how often or whether it's called
  EXPECT_CALL(server_mock_, Get(_, testing::StrEq(top_path), _))
      .Times(testing::AnyNumber())
      .WillRepeatedly(SendFast(kHtmlHttpHeaders, topHtml));

  testing::InSequence expect_in_sequence_for_scope;

  // First launch will hit the server, requesting top.html and then image_path_
  EXPECT_CALL(server_mock_, Get(_, testing::StrEq(image_path_), _))
      .WillOnce(SendFast(kBlankPngResponse[0],
                         std::string(kBlankPngResponse[1],
                                     kBlankPngFileLength)));

  // top.html contains a form. Fill in the username field and submit, causing
{'fraction_non_alphanumeric': 0.13618121822884152, 'fraction_numerical': 0.03790243467403906, 'mean_word_length': 3.540080971659919, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '667403', 'n_tokens_mistral': 4172, 'n_tokens_neox': 3931, 'n_words': 805}