summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-07 13:21:08 +0000
committerrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-07 13:21:08 +0000
commit415a01c0101f4323f2288f20a842dac54d1ac242 (patch)
tree915adcee4585977d3463cd3e9de6ffaadb51225f /chrome
parentb28dafd1176f7afa9c99d939112f2069d93e7c1f (diff)
downloadchromium_src-415a01c0101f4323f2288f20a842dac54d1ac242.zip
chromium_src-415a01c0101f4323f2288f20a842dac54d1ac242.tar.gz
chromium_src-415a01c0101f4323f2288f20a842dac54d1ac242.tar.bz2
Move code duplicated in two tests up into the TestingProfile.
Move TestURLRequestContextGetter and friends from CookieTreeModelTest and CookiesWindowControllerTest into TestingProfile. In the TestingProfile, return a valid CookieMonster and URLRequstContextGetter. BUG=None TEST=Covered by unit tests Review URL: http://codereview.chromium.org/525072 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35703 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/cocoa/cookies_window_controller_unittest.mm73
-rw-r--r--chrome/browser/cookies_tree_model_unittest.cc41
-rw-r--r--chrome/browser/extensions/user_script_listener_unittest.cc4
-rw-r--r--chrome/test/testing_profile.cc37
-rw-r--r--chrome/test/testing_profile.h18
5 files changed, 81 insertions, 92 deletions
diff --git a/chrome/browser/cocoa/cookies_window_controller_unittest.mm b/chrome/browser/cocoa/cookies_window_controller_unittest.mm
index 0071ff1..91be2d6 100644
--- a/chrome/browser/cocoa/cookies_window_controller_unittest.mm
+++ b/chrome/browser/cocoa/cookies_window_controller_unittest.mm
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009-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.
@@ -7,6 +7,7 @@
#include "app/tree_model.h"
#import "base/scoped_nsobject.h"
#include "base/scoped_ptr.h"
+#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cookies_window_controller.h"
#include "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/net/url_request_context_getter.h"
@@ -53,50 +54,15 @@
}
@end
-// Copied from src/chrome/cookies_tree_model_unittest.cc.
namespace {
-class TestURLRequestContext : public URLRequestContext {
- public:
- TestURLRequestContext() {
- cookie_store_ = new net::CookieMonster();
- }
-};
-
-class TestURLRequestContextGetter : public URLRequestContextGetter {
- public:
- virtual URLRequestContext* GetURLRequestContext() {
- if (!context_)
- context_ = new TestURLRequestContext();
- return context_.get();
- }
- private:
- scoped_refptr<URLRequestContext> context_;
-};
-
-class CookieTestingProfile : public TestingProfile {
- public:
- virtual URLRequestContextGetter* GetRequestContext() {
- if (!url_request_context_getter_.get())
- url_request_context_getter_ = new TestURLRequestContextGetter();
- return url_request_context_getter_.get();
- }
- virtual ~CookieTestingProfile() {}
-
- net::CookieMonster* GetCookieMonster() {
- return GetRequestContext()->GetCookieStore()->GetCookieMonster();
- }
-
- private:
- scoped_refptr<URLRequestContextGetter> url_request_context_getter_;
-};
-
class CookiesWindowControllerTest : public CocoaTest {
public:
virtual void SetUp() {
CocoaTest::SetUp();
+ TestingProfile* profile = browser_helper_.profile();
controller_.reset(
- [[CookiesWindowController alloc] initWithProfile:&profile_]);
+ [[CookiesWindowController alloc] initWithProfile:profile]);
}
virtual void TearDown() {
@@ -113,7 +79,8 @@ class CookiesWindowControllerTest : public CocoaTest {
return [controller_ modelObserver]->FindCocoaNode(node, start);
}
- CookieTestingProfile profile_;
+ protected:
+ BrowserTestHelper browser_helper_;
scoped_nsobject<CookiesWindowController> controller_;
};
@@ -164,9 +131,9 @@ TEST_F(CookiesWindowControllerTest, FindCocoaNodeRecursive) {
}
TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) {
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster();
cm->SetCookie(GURL("http://foo.com"), "A=B");
- CookiesTreeModel model(&profile_);
+ CookiesTreeModel model(browser_helper_.profile());
// Root --> foo.com --> Cookies --> A. Create node for 'A'.
TreeModelNode* node = model.GetRoot()->GetChild(0)->GetChild(0)->GetChild(0);
@@ -185,9 +152,9 @@ TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeCookie) {
}
TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) {
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ net::CookieMonster* cm = browser_helper_.profile()->GetCookieMonster();
cm->SetCookie(GURL("http://foo.com"), "A=B");
- CookiesTreeModel model(&profile_);
+ CookiesTreeModel model(browser_helper_.profile());
// Root --> foo.com --> Cookies --> A. Create node for 'foo.com'.
CookieTreeNode* node = model.GetRoot()->GetChild(0);
@@ -224,11 +191,12 @@ TEST_F(CookiesWindowControllerTest, CocoaNodeFromTreeNodeRecursive) {
TEST_F(CookiesWindowControllerTest, TreeNodesAdded) {
const GURL url = GURL("http://foo.com");
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ TestingProfile* profile = browser_helper_.profile();
+ net::CookieMonster* cm = profile->GetCookieMonster();
cm->SetCookie(url, "A=B");
controller_.reset(
- [[CookiesWindowController alloc] initWithProfile:&profile_]);
+ [[CookiesWindowController alloc] initWithProfile:profile]);
// Root --> foo.com --> Cookies.
NSMutableArray* cocoa_children =
@@ -262,13 +230,14 @@ TEST_F(CookiesWindowControllerTest, TreeNodesAdded) {
TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) {
const GURL url = GURL("http://foo.com");
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ TestingProfile* profile = browser_helper_.profile();
+ net::CookieMonster* cm = profile->GetCookieMonster();
cm->SetCookie(url, "A=B");
cm->SetCookie(url, "C=D");
cm->SetCookie(url, "E=F");
controller_.reset(
- [[CookiesWindowController alloc] initWithProfile:&profile_]);
+ [[CookiesWindowController alloc] initWithProfile:profile]);
// Root --> foo.com --> Cookies.
NSMutableArray* cocoa_children =
@@ -291,13 +260,14 @@ TEST_F(CookiesWindowControllerTest, TreeNodesRemoved) {
TEST_F(CookiesWindowControllerTest, TreeNodeChildrenReordered) {
const GURL url = GURL("http://foo.com");
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ TestingProfile* profile = browser_helper_.profile();
+ net::CookieMonster* cm = profile->GetCookieMonster();
cm->SetCookie(url, "A=B");
cm->SetCookie(url, "C=D");
cm->SetCookie(url, "E=F");
controller_.reset(
- [[CookiesWindowController alloc] initWithProfile:&profile_]);
+ [[CookiesWindowController alloc] initWithProfile:profile]);
// Root --> foo.com --> Cookies.
NSMutableArray* cocoa_children =
@@ -337,11 +307,12 @@ TEST_F(CookiesWindowControllerTest, TreeNodeChildrenReordered) {
TEST_F(CookiesWindowControllerTest, TreeNodeChanged) {
const GURL url = GURL("http://foo.com");
- net::CookieMonster* cm = profile_.GetCookieMonster();
+ TestingProfile* profile = browser_helper_.profile();
+ net::CookieMonster* cm = profile->GetCookieMonster();
cm->SetCookie(url, "A=B");
controller_.reset(
- [[CookiesWindowController alloc] initWithProfile:&profile_]);
+ [[CookiesWindowController alloc] initWithProfile:profile]);
CookiesTreeModel* model = [controller_ treeModel];
// Root --> foo.com --> Cookies.
diff --git a/chrome/browser/cookies_tree_model_unittest.cc b/chrome/browser/cookies_tree_model_unittest.cc
index 317c003..8ed45a5 100644
--- a/chrome/browser/cookies_tree_model_unittest.cc
+++ b/chrome/browser/cookies_tree_model_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009-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.
@@ -15,41 +15,6 @@
namespace {
-class TestURLRequestContext : public URLRequestContext {
- public:
- TestURLRequestContext() {
- cookie_store_ = new net::CookieMonster();
- }
-};
-
-class TestURLRequestContextGetter : public URLRequestContextGetter {
- public:
- virtual URLRequestContext* GetURLRequestContext() {
- if (!context_)
- context_ = new TestURLRequestContext();
- return context_.get();
- }
- private:
- scoped_refptr<URLRequestContext> context_;
-};
-
-class CookieTestingProfile : public TestingProfile {
- public:
- virtual URLRequestContextGetter* GetRequestContext() {
- if (!url_request_context_getter_.get())
- url_request_context_getter_ = new TestURLRequestContextGetter;
- return url_request_context_getter_.get();
- }
- virtual ~CookieTestingProfile() {}
-
- net::CookieMonster* GetCookieMonster() {
- return GetRequestContext()->GetCookieStore()->GetCookieMonster();
- }
-
- private:
- scoped_refptr<URLRequestContextGetter> url_request_context_getter_;
-};
-
class CookiesTreeModelTest : public testing::Test {
public:
CookiesTreeModelTest() : io_thread_(ChromeThread::IO, &message_loop_) {
@@ -59,7 +24,7 @@ class CookiesTreeModelTest : public testing::Test {
}
virtual void SetUp() {
- profile_.reset(new CookieTestingProfile());
+ profile_.reset(new TestingProfile());
}
// Get the cookie names in the cookie list, as a comma seperated string.
@@ -116,7 +81,7 @@ class CookiesTreeModelTest : public testing::Test {
MessageLoop message_loop_;
ChromeThread io_thread_;
- scoped_ptr<CookieTestingProfile> profile_;
+ scoped_ptr<TestingProfile> profile_;
};
TEST_F(CookiesTreeModelTest, RemoveAll) {
diff --git a/chrome/browser/extensions/user_script_listener_unittest.cc b/chrome/browser/extensions/user_script_listener_unittest.cc
index 82a8880..e9129e8 100644
--- a/chrome/browser/extensions/user_script_listener_unittest.cc
+++ b/chrome/browser/extensions/user_script_listener_unittest.cc
@@ -211,6 +211,10 @@ class ExtensionTestingProfile : public TestingProfile {
service_ = NULL;
}
+ // Duplicate old functionality from TestingProfile. The ContextGetter
+ // provided by TestingProfile only has support for the CookieMonster.
+ virtual URLRequestContextGetter* GetRequestContext() { return NULL; }
+
virtual ExtensionsService* GetExtensionsService() {
return service_.get();
}
diff --git a/chrome/test/testing_profile.cc b/chrome/test/testing_profile.cc
index 39e847a..c102f82 100644
--- a/chrome/test/testing_profile.cc
+++ b/chrome/test/testing_profile.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// 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.
@@ -9,8 +9,10 @@
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/dom_ui/ntp_resource_cache.h"
#include "chrome/browser/history/history_backend.h"
+#include "chrome/browser/net/url_request_context_getter.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/common/chrome_constants.h"
+#include "net/url_request/url_request_context.h"
#include "webkit/database/database_tracker.h"
#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
@@ -76,6 +78,33 @@ class BookmarkLoadObserver : public BookmarkModelObserver {
DISALLOW_COPY_AND_ASSIGN(BookmarkLoadObserver);
};
+// This context is used to assist testing the CookieMonster by providing a
+// valid CookieStore. This can probably be expanded to test other aspects of
+// the context as well.
+class TestURLRequestContext : public URLRequestContext {
+ public:
+ TestURLRequestContext() {
+ cookie_store_ = new net::CookieMonster();
+ }
+};
+
+// Used to return a dummy context (normally the context is on the IO thread).
+// The one here can be run on the main test thread. Note that this can lead to
+// a leak if your test does not have a ChromeThread::IO in it because
+// URLRequestContextGetter is defined as a ReferenceCounted object with a
+// DeleteOnIOThread trait.
+class TestURLRequestContextGetter : public URLRequestContextGetter {
+ public:
+ virtual URLRequestContext* GetURLRequestContext() {
+ if (!context_)
+ context_ = new TestURLRequestContext();
+ return context_.get();
+ }
+
+ private:
+ scoped_refptr<URLRequestContext> context_;
+};
+
} // namespace
TestingProfile::TestingProfile()
@@ -202,6 +231,12 @@ void TestingProfile::InitThemes() {
}
}
+URLRequestContextGetter* TestingProfile::GetRequestContext() {
+ if (!request_context_)
+ request_context_ = new TestURLRequestContextGetter();
+ return request_context_.get();
+}
+
NTPResourceCache* TestingProfile::GetNTPResourceCache() {
if (!ntp_resource_cache_.get())
ntp_resource_cache_.reset(new NTPResourceCache(this));
diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h
index f59a18b..28d4e58 100644
--- a/chrome/test/testing_profile.h
+++ b/chrome/test/testing_profile.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009-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.
@@ -13,10 +13,12 @@
#include "chrome/browser/browser_theme_provider.h"
#include "chrome/browser/favicon_service.h"
#include "chrome/browser/history/history.h"
+#include "chrome/browser/net/url_request_context_getter.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/sessions/session_service.h"
#include "chrome/common/pref_service.h"
+#include "net/base/cookie_monster.h"
class TestingProfile : public Profile {
public:
@@ -100,6 +102,9 @@ class TestingProfile : public Profile {
void set_has_history_service(bool has_history_service) {
has_history_service_ = has_history_service;
}
+ net::CookieMonster* GetCookieMonster() {
+ return GetRequestContext()->GetCookieStore()->GetCookieMonster();
+ }
virtual SearchVersusNavigateClassifier* GetSearchVersusNavigateClassifier() {
return NULL;
}
@@ -139,11 +144,16 @@ class TestingProfile : public Profile {
InitThemes();
return theme_provider_.get();
}
- virtual URLRequestContextGetter* GetRequestContext() { return NULL; }
+
+ // Returns a testing ContextGetter. This getter is currently only capable of
+ // returning a Context that helps test the CookieMonster. See implementation
+ // comments for more details.
+ virtual URLRequestContextGetter* GetRequestContext();
virtual URLRequestContextGetter* GetRequestContextForMedia() { return NULL; }
virtual URLRequestContextGetter* GetRequestContextForExtensions() {
return NULL;
}
+
virtual net::SSLConfigService* GetSSLConfigService() { return NULL; }
virtual BlacklistManager* GetBlacklistManager() { return NULL; }
virtual HostZoomMap* GetHostZoomMap() { return NULL; }
@@ -230,6 +240,10 @@ class TestingProfile : public Profile {
scoped_ptr<BrowserThemeProvider> theme_provider_;
bool created_theme_provider_;
+ // Internally, this is a TestURLRequestContextGetter that creates a dummy
+ // request context. Currently, only the CookieMonster is hooked up.
+ scoped_refptr<URLRequestContextGetter> request_context_;
+
// Do we have a history service? This defaults to the value of
// history_service, but can be explicitly set.
bool has_history_service_;