<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 "base/sys_string_conversions.h"
#include "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/cocoa/cookie_prompt_window_controller.h"
#include "chrome/browser/cookie_modal_dialog.h"
#include "chrome/test/testing_profile.h"

// A mock class which implements just enough functionality to
// act as a radio with a pre-specified selected button.
@interface MockRadioButtonMatrix : NSObject {
 @private
  NSInteger selectedRow_;
}
- (NSInteger)selectedRow;
@end

@implementation MockRadioButtonMatrix

- (id)initWithSelectedRow:(NSInteger)selectedRow {
  if ((self = [super init])) {
    selectedRow_ = selectedRow;
  }
  return self;
}

- (NSInteger)selectedRow {
  return selectedRow_;
}
@end

namespace {

// A subclass of the |CookiePromptModalDialog| that allows tests of
// some of the prompt window controller's functionality without having
// a full environment by overriding select methods and intercepting
// calls that would otherwise rely on behavior only present in a full
// environment.
class CookiePromptModalDialogMock : public CookiePromptModalDialog {
 public:
  CookiePromptModalDialogMock(const GURL& origin,
                              const std::string& cookieLine,
                              HostContentSettingsMap* hostContentSettingsMap);

  virtual void AllowSiteData(bool remember, bool session_expire);
  virtual void BlockSiteData(bool remember);

  bool allow() const { return allow_; }
  bool remember() const { return remember_; }

 private:

  // The result of the block/unblock decision.
  bool allow_;

  // Whether the block/accept decision should be remembered.
  bool remember_;
};

CookiePromptModalDialogMock::CookiePromptModalDialogMock(
    const GURL& origin,
    const std::string& cookieLine,
    HostContentSettingsMap* hostContentSettingsMap)
    : CookiePromptModalDialog(NULL, hostContentSettingsMap, origin, cookieLine,
                              NULL),
      allow_(false),
      remember_(false) {
}

void CookiePromptModalDialogMock::AllowSiteData(bool remember,
                                                bool session_expire) {
  remember_ = remember;
  allow_ = true;
}

void CookiePromptModalDialogMock::BlockSiteData(bool remember) {
  remember_ = remember;
  allow_ = false;
}

class CookiePromptWindowControllerTest : public CocoaTest {
 public:
  CookiePromptWindowControllerTest() {
    hostContentSettingsMap_ = profile_.GetHostContentSettingsMap();
  }

  TestingProfile profile_;
  scoped_refptr<HostContentSettingsMap> hostContentSettingsMap_;<commit_msg>Add a UI thread and message loop to the cookie prompt controller test. This is needed by <http://codereview.chromium.org/1604020> because the host_content_settings now registers as an observer of the profile and needs to unregister itself on the UI thread.<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 "base/sys_string_conversions.h"
#include "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/cocoa/cookie_prompt_window_controller.h"
#include "chrome/browser/cookie_modal_dialog.h"
#include "chrome/test/testing_profile.h"

// A mock class which implements just enough functionality to
// act as a radio with a pre-specified selected button.
@interface MockRadioButtonMatrix : NSObject {
 @private
  NSInteger selectedRow_;
}
- (NSInteger)selectedRow;
@end

@implementation MockRadioButtonMatrix

- (id)initWithSelectedRow:(NSInteger)selectedRow {
  if ((self = [super init])) {
    selectedRow_ = selectedRow;
  }
  return self;
}

- (NSInteger)selectedRow {
  return selectedRow_;
}
@end

namespace {

// A subclass of the |CookiePromptModalDialog| that allows tests of
// some of the prompt window controller's functionality without having
// a full environment by overriding select methods and intercepting
// calls that would otherwise rely on behavior only present in a full
// environment.
class CookiePromptModalDialogMock : public CookiePromptModalDialog {
 public:
  CookiePromptModalDialogMock(const GURL& origin,
                              const std::string& cookieLine,
                              HostContentSettingsMap* hostContentSettingsMap);

  virtual void AllowSiteData(bool remember, bool session_expire);
  virtual void BlockSiteData(bool remember);

  bool allow() const { return allow_; }
  bool remember() const { return remember_; }

 private:

  // The result of the block/unblock decision.
  bool allow_;

  // Whether the block/accept decision should be remembered.
  bool remember_;
};

CookiePromptModalDialogMock::CookiePromptModalDialogMock(
    const GURL& origin,
    const std::string& cookieLine,
    HostContentSettingsMap* hostContentSettingsMap)
    : CookiePromptModalDialog(NULL, hostContentSettingsMap, origin, cookieLine,
                              NULL),
      allow_(false),
      remember_(false) {
}

void CookiePromptModalDialogMock::AllowSiteData(bool remember,
                                                bool session_expire) {
  remember_ = remember;
  allow_ = true;
}

void CookiePromptModalDialogMock::BlockSiteData(bool remember) {
  remember_ = remember;
  allow_ = false;
}

class CookiePromptWindowControllerTest : public CocoaTest {
 public:
  CookiePromptWindowControllerTest()
      : ui_thread_(ChromeThread::UI, &message_loop_) {
    hostContentSettingsMap_ = profile_.GetHostContentSettingsMap();
  }

  MessageLoopForUI message_loop_;
  ChromeThread ui_thread_;
  TestingProfile profile_;
  scoped_refptr<HostContentSettingsMap> hostContentSettingsMap_;
{'fraction_non_alphanumeric': 0.07592116538131963, 'fraction_numerical': 0.002570694087403599, 'mean_word_length': 3.9541595925297113, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '2275776', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1532, 'n_words': 474}