<commit_before>// Copyright (c) 2006-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 "chrome_frame/test/test_mock_with_web_server.h"

#include <mshtmcid.h>

#include "base/scoped_bstr_win.h"
#include "chrome/common/url_constants.h"
#include "chrome_frame/utils.h"
#include "chrome_frame/test/simulate_input.h"
#include "chrome_frame/test/test_with_web_server.h"

#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
#include "testing/gmock_mutant.h"

using testing::_;
using testing::CreateFunctor;
using chrome_frame_test::CloseIeAtEndOfScope;
using chrome_frame_test::ComStackObjectWithUninitialize;
using chrome_frame_test::kChromeFrameLongNavigationTimeoutInSeconds;
using chrome_frame_test::MockWebBrowserEventSink;

const wchar_t kChromeFrameFileUrl[] = L"gcf:file:///C:/";
const wchar_t enter_key[] = { VK_RETURN, 0 };
const wchar_t escape_key[] = { VK_ESCAPE, 0 };
const wchar_t tab_enter_keys[] = { VK_TAB, VK_RETURN, 0 };

namespace chrome_frame_test {

ExpectationSet MockWebBrowserEventSink::ExpectNavigationCardinality(
    const std::wstring& url, testing::Cardinality cardinality) {
  // Expect a single navigation sequence. If URL is specified,
  // match the URL.
  // The navigation sequence is a set of OnBeforeNavigate2, OnFileDownload
  // and OnNavigationComplete2 events.  For certain navigations, internal
  // vs external (and maybe between different IE versions) these events
  // events occur with different frequencies. Hence, the variable
  // cardinality.
  ExpectationSet navigation;
  if (url.empty()) {
    navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_, _, _, _, _, _, _))
        .Times(cardinality);
  } else {
    navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                              testing::Field(&VARIANT::bstrVal,
                              testing::StrCaseEq(url)), _, _, _, _, _))
        .Times(cardinality);
  }
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(cardinality);

  if (url.empty()) {
    navigation += EXPECT_CALL(*this, OnNavigateComplete2(_, _))
        .Times(cardinality);
  } else {
    navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                              testing::Field(&VARIANT::bstrVal,
                              testing::StrCaseEq(url))))
        .Times(cardinality);
  }

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigation(
    const std::wstring& url) {
  // When the onhttpequiv patch is enabled, we will get two
  // BeforeNavigate2/OnNavigateComplete2 notifications due to
  // switching from IE to CF.
  // Note that when going backwards, we don't expect that since the extra
  // navigational entries in the travel log should have been removed.
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _));
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationSequenceForAnchors(
    const std::wstring& url) {
  // When the onhttpequiv patch is enabled, we will get two
  // BeforeNavigate2/OnNavigateComplete2 notifications due to
  // switching from IE to CF.
  // Note that when going backwards, we don't expect that since the extra
  // navigational entries in the travel log should have been removed.
  // On IE6 we don't receive the BeforeNavigate notifications for anchors.
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationAndSwitch(
    const std::wstring& url) {
  return ExpectNavigationCardinality(url, testing::AnyNumber());
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationAndSwitchSequence(
    const std::wstring& url) {
  // When navigation expectations occur in sequence the following order
  // is necessary. This is mainly based on observed quirks rather than
  // any theory.
  // TODO(joshia): Improve expectations here
  ExpectationSet navigation = ExpectNavigationCardinality(url,
                                                          testing::Exactly(1));
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNewWindow(
    MockWebBrowserEventSink* new_window_mock) {
  DCHECK(new_window_mock);
  ExpectationSet new_window;
  new_window += EXPECT_CALL(*this, OnNewWindow3(_, _, _, _, _));
  new_window += EXPECT_CALL(*this, OnNewBrowserWindow(_, _))
      .WillOnce(testing::WithArgs<0>(testing::Invoke(CreateFunctor(
                new_window_mock, &MockWebBrowserEventSink::Attach))));

  new_window_mock->ExpectNavigationAndSwitch(std::wstring());
  return new_window;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationInIE(
    const std::wstring& url) {
  testing::InSequence sequence;
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _));
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))));
  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNewWindowWithIE(
    const std::wstring& url, MockWebBrowserEventSink* new_window_mock) {
  DCHECK(new_window_mock);
  ExpectationSet new_window;
  new_window += EXPECT_CALL(*this, OnNewWindow3(_, _, _, _, _));
  new_window += EXPECT_CALL(*this, OnNewBrowserWindow(_, _))
      .WillOnce(testing::WithArgs<0>(testing::Invoke(CreateFunctor(
                new_window_mock, &MockWebBrowserEventSink::Attach))));

  new_window_mock->ExpectNavigationInIE(url);
  return new_window;
}

}  // namespace chrome_frame_test

ACTION_P(SetFocusToChrome, mock) {
  mock->SetFocusToChrome();
}

ACTION_P2(Navigate, mock, url) {
  mock->Navigate(url);
}

ACTION_P2(WatchWindow, mock, window_class) {
  mock->WatchChromeWindow(window_class);
}

ACTION_P(StopWindowWatching, mock) {
  mock->StopWatching();
}

ACTION_P6(DelaySendMouseClick, mock, loop, delay, x, y, button) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(mock,
      &MockWebBrowserEventSink::SendMouseClick, x, y, button), delay);
}

ACTION_P3(DelaySendString, loop, delay, str) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendStringW, str), delay);
}

ACTION_P5(SendExtendedKeysEnter, loop, delay, c, repeat, mod) {
  chrome_frame_test::DelaySendExtendedKeysEnter(loop, delay, c, repeat, mod);
}

ACTION(DoCloseWindow) {
  ::PostMessage(arg0, WM_SYSCOMMAND, SC_CLOSE, 0);
}

// This function selects the address bar via the Alt+d shortcut. This is done
// via a delayed task which executes after the delay which is passed in.
// The subsequent operations like typing in the actual url and then hitting
// enter to force the browser to navigate to it each execute as delayed tasks
// timed at the delay passed in. The recommended value for delay is 2000 ms
// to account for the time taken for the accelerator keys to be reflected back
// from Chrome.
ACTION_P3(TypeUrlInAddressBar, loop, url, delay) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendCharA, 'd', simulate_input::ALT),
      delay);

  const unsigned int kInterval = 100;
  int next_delay = delay + kInterval;

  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendStringW, url), next_delay);

  next_delay = next_delay + kInterval;

  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendCharA, VK_RETURN, simulate_input::NONE),
      next_delay);
}<commit_msg>Attempt to fix the ChromeFrame tests which rely on sending input to the IE address bar to initiate navigation. These tests fail at times on the builders because of an incorrect URL actually being sent in to Chrome.<commit_after>// Copyright (c) 2006-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 "chrome_frame/test/test_mock_with_web_server.h"

#include <mshtmcid.h>

#include "base/scoped_bstr_win.h"
#include "chrome/common/url_constants.h"
#include "chrome_frame/utils.h"
#include "chrome_frame/test/simulate_input.h"
#include "chrome_frame/test/test_with_web_server.h"

#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
#include "testing/gmock_mutant.h"

using testing::_;
using testing::CreateFunctor;
using chrome_frame_test::CloseIeAtEndOfScope;
using chrome_frame_test::ComStackObjectWithUninitialize;
using chrome_frame_test::kChromeFrameLongNavigationTimeoutInSeconds;
using chrome_frame_test::MockWebBrowserEventSink;

const wchar_t kChromeFrameFileUrl[] = L"gcf:file:///C:/";
const wchar_t enter_key[] = { VK_RETURN, 0 };
const wchar_t escape_key[] = { VK_ESCAPE, 0 };
const wchar_t tab_enter_keys[] = { VK_TAB, VK_RETURN, 0 };

namespace chrome_frame_test {

ExpectationSet MockWebBrowserEventSink::ExpectNavigationCardinality(
    const std::wstring& url, testing::Cardinality cardinality) {
  // Expect a single navigation sequence. If URL is specified,
  // match the URL.
  // The navigation sequence is a set of OnBeforeNavigate2, OnFileDownload
  // and OnNavigationComplete2 events.  For certain navigations, internal
  // vs external (and maybe between different IE versions) these events
  // events occur with different frequencies. Hence, the variable
  // cardinality.
  ExpectationSet navigation;
  if (url.empty()) {
    navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_, _, _, _, _, _, _))
        .Times(cardinality);
  } else {
    navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                              testing::Field(&VARIANT::bstrVal,
                              testing::StrCaseEq(url)), _, _, _, _, _))
        .Times(cardinality);
  }
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(cardinality);

  if (url.empty()) {
    navigation += EXPECT_CALL(*this, OnNavigateComplete2(_, _))
        .Times(cardinality);
  } else {
    navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                              testing::Field(&VARIANT::bstrVal,
                              testing::StrCaseEq(url))))
        .Times(cardinality);
  }

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigation(
    const std::wstring& url) {
  // When the onhttpequiv patch is enabled, we will get two
  // BeforeNavigate2/OnNavigateComplete2 notifications due to
  // switching from IE to CF.
  // Note that when going backwards, we don't expect that since the extra
  // navigational entries in the travel log should have been removed.
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _));
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationSequenceForAnchors(
    const std::wstring& url) {
  // When the onhttpequiv patch is enabled, we will get two
  // BeforeNavigate2/OnNavigateComplete2 notifications due to
  // switching from IE to CF.
  // Note that when going backwards, we don't expect that since the extra
  // navigational entries in the travel log should have been removed.
  // On IE6 we don't receive the BeforeNavigate notifications for anchors.
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationAndSwitch(
    const std::wstring& url) {
  return ExpectNavigationCardinality(url, testing::AnyNumber());
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationAndSwitchSequence(
    const std::wstring& url) {
  // When navigation expectations occur in sequence the following order
  // is necessary. This is mainly based on observed quirks rather than
  // any theory.
  // TODO(joshia): Improve expectations here
  ExpectationSet navigation = ExpectNavigationCardinality(url,
                                                          testing::Exactly(1));
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))))
      .Times(testing::AnyNumber());

  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNewWindow(
    MockWebBrowserEventSink* new_window_mock) {
  DCHECK(new_window_mock);
  ExpectationSet new_window;
  new_window += EXPECT_CALL(*this, OnNewWindow3(_, _, _, _, _));
  new_window += EXPECT_CALL(*this, OnNewBrowserWindow(_, _))
      .WillOnce(testing::WithArgs<0>(testing::Invoke(CreateFunctor(
                new_window_mock, &MockWebBrowserEventSink::Attach))));

  new_window_mock->ExpectNavigationAndSwitch(std::wstring());
  return new_window;
}

ExpectationSet MockWebBrowserEventSink::ExpectNavigationInIE(
    const std::wstring& url) {
  testing::InSequence sequence;
  ExpectationSet navigation;
  navigation += EXPECT_CALL(*this, OnBeforeNavigate2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url)), _, _, _, _, _));
  navigation += EXPECT_CALL(*this, OnFileDownload(VARIANT_TRUE, _))
      .Times(testing::AnyNumber());
  navigation += EXPECT_CALL(*this, OnNavigateComplete2(_,
                            testing::Field(&VARIANT::bstrVal,
                            testing::StrCaseEq(url))));
  return navigation;
}

ExpectationSet MockWebBrowserEventSink::ExpectNewWindowWithIE(
    const std::wstring& url, MockWebBrowserEventSink* new_window_mock) {
  DCHECK(new_window_mock);
  ExpectationSet new_window;
  new_window += EXPECT_CALL(*this, OnNewWindow3(_, _, _, _, _));
  new_window += EXPECT_CALL(*this, OnNewBrowserWindow(_, _))
      .WillOnce(testing::WithArgs<0>(testing::Invoke(CreateFunctor(
                new_window_mock, &MockWebBrowserEventSink::Attach))));

  new_window_mock->ExpectNavigationInIE(url);
  return new_window;
}

}  // namespace chrome_frame_test

ACTION_P(SetFocusToChrome, mock) {
  mock->SetFocusToChrome();
}

ACTION_P2(Navigate, mock, url) {
  mock->Navigate(url);
}

ACTION_P2(WatchWindow, mock, window_class) {
  mock->WatchChromeWindow(window_class);
}

ACTION_P(StopWindowWatching, mock) {
  mock->StopWatching();
}

ACTION_P6(DelaySendMouseClick, mock, loop, delay, x, y, button) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableMethod(mock,
      &MockWebBrowserEventSink::SendMouseClick, x, y, button), delay);
}

ACTION_P3(DelaySendString, loop, delay, str) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendStringW, str), delay);
}

ACTION_P5(SendExtendedKeysEnter, loop, delay, c, repeat, mod) {
  chrome_frame_test::DelaySendExtendedKeysEnter(loop, delay, c, repeat, mod);
}

ACTION(DoCloseWindow) {
  ::PostMessage(arg0, WM_SYSCOMMAND, SC_CLOSE, 0);
}

// This function selects the address bar via the Alt+d shortcut. This is done
// via a delayed task which executes after the delay which is passed in.
// The subsequent operations like typing in the actual url and then hitting
// enter to force the browser to navigate to it each execute as delayed tasks
// timed at the delay passed in. The recommended value for delay is 2000 ms
// to account for the time taken for the accelerator keys to be reflected back
// from Chrome.
ACTION_P3(TypeUrlInAddressBar, loop, url, delay) {
  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendCharA, 'd', simulate_input::ALT),
      delay);

  const unsigned int kInterval = 500;
  int next_delay = delay + kInterval;

  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendStringW, url), next_delay);

  next_delay = next_delay + kInterval;

  loop->PostDelayedTask(FROM_HERE, NewRunnableFunction(
      simulate_input::SendCharA, VK_RETURN, simulate_input::NONE),
      next_delay);
}
{'fraction_non_alphanumeric': 0.10836491752522287, 'fraction_numerical': 0.005338173277104574, 'mean_word_length': 3.7451874366767983, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4221027', 'n_tokens_mistral': 5543, 'n_tokens_neox': 5442, 'n_words': 1251}