<commit_before>// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"

#include "chrome/browser/extensions/event_listener_map.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"

namespace {

const char kExt1Id[] = "extension_1";
const char kExt2Id[] = "extension_2";
const char kEvent1Name[] = "event1";
const char kEvent2Name[] = "event2";

class EmptyDelegate : public EventListenerMap::Delegate {
  virtual void OnListenerAdded(const EventListener* listener) OVERRIDE {};
  virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE {};
};

class EventListenerMapUnittest : public testing::Test {
 public:
  EventListenerMapUnittest()
    : delegate_(new EmptyDelegate),
      listeners_(new EventListenerMap(delegate_.get())),
      browser_context_(new content::TestBrowserContext),
      process_(new content::MockRenderProcessHost(browser_context_.get())) {
  }

  scoped_ptr<DictionaryValue> CreateHostSuffixFilter(
      const std::string& suffix) {
    scoped_ptr<DictionaryValue> filter(new DictionaryValue);
    scoped_ptr<ListValue> filter_list(new ListValue);
    scoped_ptr<DictionaryValue> filter_dict(new DictionaryValue);

    filter_dict->Set("hostSuffix", new StringValue(suffix));

    filter_list->Append(filter_dict.release());
    filter->Set("url", filter_list.release());
    return filter.Pass();
  }

  scoped_ptr<ExtensionEvent> CreateNamedEvent(const std::string& event_name) {
    return CreateEvent(event_name, GURL());
  }

  scoped_ptr<ExtensionEvent> CreateEvent(const std::string& event_name,
                                         const GURL& url) {
    EventFilteringInfo info;
    info.SetURL(url);
    scoped_ptr<ExtensionEvent> result(new ExtensionEvent(event_name, "", GURL(),
        NULL, "", ExtensionEventRouter::USER_GESTURE_UNKNOWN, info));
    return result.Pass();
  }

 protected:
  scoped_ptr<EventListenerMap::Delegate> delegate_;
  scoped_ptr<EventListenerMap> listeners_;
  scoped_ptr<content::TestBrowserContext> browser_context_;
  scoped_ptr<content::MockRenderProcessHost> process_;
};

TEST_F(EventListenerMapUnittest, UnfilteredEventsGoToAllListeners) {
  listeners_->AddListener(scoped_ptr<EventListener>(new EventListener(
      kEvent1Name, kExt1Id, NULL, scoped_ptr<DictionaryValue>())));<commit_msg>Revert 145191 - Fix memory leak in EventListenerMapUnittest.<commit_after>// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"

#include "chrome/browser/extensions/event_listener_map.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"

namespace {

const char kExt1Id[] = "extension_1";
const char kExt2Id[] = "extension_2";
const char kEvent1Name[] = "event1";
const char kEvent2Name[] = "event2";

class EmptyDelegate : public EventListenerMap::Delegate {
  virtual void OnListenerAdded(const EventListener* listener) OVERRIDE {};
  virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE {};
};

class EventListenerMapUnittest : public testing::Test {
 public:
  EventListenerMapUnittest()
    : delegate_(new EmptyDelegate),
      listeners_(new EventListenerMap(delegate_.get())),
      process_(new content::MockRenderProcessHost(
          new content::TestBrowserContext)) {
  }

  scoped_ptr<DictionaryValue> CreateHostSuffixFilter(
      const std::string& suffix) {
    scoped_ptr<DictionaryValue> filter(new DictionaryValue);
    scoped_ptr<ListValue> filter_list(new ListValue);
    scoped_ptr<DictionaryValue> filter_dict(new DictionaryValue);

    filter_dict->Set("hostSuffix", new StringValue(suffix));

    filter_list->Append(filter_dict.release());
    filter->Set("url", filter_list.release());
    return filter.Pass();
  }

  scoped_ptr<ExtensionEvent> CreateNamedEvent(const std::string& event_name) {
    return CreateEvent(event_name, GURL());
  }

  scoped_ptr<ExtensionEvent> CreateEvent(const std::string& event_name,
                                         const GURL& url) {
    EventFilteringInfo info;
    info.SetURL(url);
    scoped_ptr<ExtensionEvent> result(new ExtensionEvent(event_name, "", GURL(),
        NULL, "", ExtensionEventRouter::USER_GESTURE_UNKNOWN, info));
    return result.Pass();
  }

 protected:
  scoped_ptr<EventListenerMap::Delegate> delegate_;
  scoped_ptr<EventListenerMap> listeners_;
  scoped_ptr<content::MockRenderProcessHost> process_;
};

TEST_F(EventListenerMapUnittest, UnfilteredEventsGoToAllListeners) {
  listeners_->AddListener(scoped_ptr<EventListener>(new EventListener(
      kEvent1Name, kExt1Id, NULL, scoped_ptr<DictionaryValue>())));
{'fraction_non_alphanumeric': 0.11034618410700237, 'fraction_numerical': 0.006687647521636507, 'mean_word_length': 5.364205256570713, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4936471', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1467, 'n_words': 305}