| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include <string> | 5 #include <string> | 
| 6 | 6 | 
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" | 
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" | 
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" | 
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" | 
|  | 11 #include "base/run_loop.h" | 
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" | 
| 12 #include "chrome/browser/background/background_contents_service.h" | 13 #include "chrome/browser/background/background_contents_service.h" | 
| 13 #include "chrome/browser/background/background_contents_service_factory.h" | 14 #include "chrome/browser/background/background_contents_service_factory.h" | 
| 14 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" | 
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 
| 16 #include "chrome/browser/tab_contents/background_contents.h" | 17 #include "chrome/browser/tab_contents/background_contents.h" | 
| 17 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" | 
| 18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" | 
|  | 20 #include "chrome/test/base/scoped_testing_local_state.h" | 
|  | 21 #include "chrome/test/base/testing_browser_process.h" | 
| 19 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" | 
| 20 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" | 
|  | 24 #include "chrome/test/base/testing_profile_manager.h" | 
| 21 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" | 
|  | 26 #include "content/public/test/test_browser_thread_bundle.h" | 
| 22 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" | 
| 23 #include "testing/platform_test.h" | 28 #include "testing/platform_test.h" | 
|  | 29 #include "ui/message_center/message_center.h" | 
| 24 #include "url/gurl.h" | 30 #include "url/gurl.h" | 
| 25 | 31 | 
| 26 class BackgroundContentsServiceTest : public testing::Test { | 32 class BackgroundContentsServiceTest : public testing::Test { | 
| 27  public: | 33  protected: | 
| 28   BackgroundContentsServiceTest() {} | 34   BackgroundContentsServiceTest() | 
| 29   virtual ~BackgroundContentsServiceTest() {} | 35       : command_line_(CommandLine::NO_PROGRAM), | 
| 30   virtual void SetUp() { | 36         profile_manager_(TestingBrowserProcess::GetGlobal()), | 
| 31     command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 37         profile_(NULL) { | 
|  | 38     CHECK(profile_manager_.SetUp()); | 
|  | 39     profile_ = profile_manager_.CreateTestingProfile("TestProfile"); | 
|  | 40     service_.reset(new BackgroundContentsService(profile_, &command_line_)); | 
|  | 41     BackgroundContentsServiceFactory::GetInstance()-> | 
|  | 42         RegisterUserPrefsOnBrowserContext(profile_); | 
|  | 43   } | 
|  | 44 | 
|  | 45   virtual ~BackgroundContentsServiceTest() { | 
|  | 46     base::RunLoop().RunUntilIdle(); | 
|  | 47   } | 
|  | 48 | 
|  | 49   static void SetUpTestCase() { | 
|  | 50     message_center::MessageCenter::Initialize(); | 
|  | 51   } | 
|  | 52   static void TearDownTestCase() { | 
|  | 53     message_center::MessageCenter::Shutdown(); | 
| 32   } | 54   } | 
| 33 | 55 | 
| 34   const DictionaryValue* GetPrefs(Profile* profile) { | 56   const DictionaryValue* GetPrefs(Profile* profile) { | 
| 35     return profile->GetPrefs()->GetDictionary( | 57     return profile->GetPrefs()->GetDictionary( | 
| 36         prefs::kRegisteredBackgroundContents); | 58         prefs::kRegisteredBackgroundContents); | 
| 37   } | 59   } | 
| 38 | 60 | 
| 39   // Returns the stored pref URL for the passed app id. | 61   // Returns the stored pref URL for the passed app id. | 
| 40   std::string GetPrefURLForApp(Profile* profile, const string16& appid) { | 62   std::string GetPrefURLForApp(Profile* profile, const string16& appid) { | 
| 41     const DictionaryValue* pref = GetPrefs(profile); | 63     const DictionaryValue* pref = GetPrefs(profile); | 
| 42     EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); | 64     EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); | 
| 43     const DictionaryValue* value; | 65     const DictionaryValue* value; | 
| 44     pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); | 66     pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); | 
| 45     std::string url; | 67     std::string url; | 
| 46     value->GetString("url", &url); | 68     value->GetString("url", &url); | 
| 47     return url; | 69     return url; | 
| 48   } | 70   } | 
| 49 | 71 | 
| 50   scoped_ptr<CommandLine> command_line_; | 72   content::TestBrowserThreadBundle thread_bundle; | 
|  | 73   CommandLine command_line_; | 
|  | 74   TestingProfileManager profile_manager_; | 
|  | 75   TestingProfile* profile_;  // Not owned. | 
|  | 76   scoped_ptr<BackgroundContentsService> service_; | 
| 51 }; | 77 }; | 
| 52 | 78 | 
| 53 class MockBackgroundContents : public BackgroundContents { | 79 class MockBackgroundContents : public BackgroundContents { | 
| 54  public: | 80  public: | 
| 55   explicit MockBackgroundContents(Profile* profile) | 81   explicit MockBackgroundContents(Profile* profile) | 
| 56       : appid_(ASCIIToUTF16("app_id")), | 82       : appid_(ASCIIToUTF16("app_id")), | 
| 57         profile_(profile) { | 83         profile_(profile) { | 
| 58   } | 84   } | 
| 59   MockBackgroundContents(Profile* profile, const std::string& id) | 85   MockBackgroundContents(Profile* profile, const std::string& id) | 
| 60       : appid_(ASCIIToUTF16(id)), | 86       : appid_(ASCIIToUTF16(id)), | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
| 90         chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, | 116         chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, | 
| 91         content::Source<Profile>(profile_), | 117         content::Source<Profile>(profile_), | 
| 92         content::Details<BackgroundContents>(this)); | 118         content::Details<BackgroundContents>(this)); | 
| 93   } | 119   } | 
| 94 | 120 | 
| 95   const string16& appid() { return appid_; } | 121   const string16& appid() { return appid_; } | 
| 96 | 122 | 
| 97  private: | 123  private: | 
| 98   GURL url_; | 124   GURL url_; | 
| 99 | 125 | 
| 100   // The ID of our parent application | 126   // The ID of our parent application. | 
| 101   string16 appid_; | 127   string16 appid_; | 
| 102 | 128 | 
| 103   // Parent profile | 129   // Parent profile. Not owned. | 
| 104   Profile* profile_; | 130   Profile* profile_; | 
| 105 }; | 131 }; | 
| 106 | 132 | 
| 107 TEST_F(BackgroundContentsServiceTest, Create) { | 133 TEST_F(BackgroundContentsServiceTest, Create) { | 
| 108   // Check for creation and leaks. | 134   // Check for creation and leaks when the basic objects in the | 
| 109   TestingProfile profile; | 135   // fixtures are created/destructed. | 
| 110   BackgroundContentsService service(&profile, command_line_.get()); |  | 
| 111 } | 136 } | 
| 112 | 137 | 
| 113 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { | 138 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { | 
| 114   TestingProfile profile; | 139   MockBackgroundContents* contents = new MockBackgroundContents(profile_); | 
| 115   BackgroundContentsService service(&profile, command_line_.get()); | 140   EXPECT_FALSE(service_->IsTracked(contents)); | 
| 116   MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 141   contents->SendOpenedNotification(service_.get()); | 
| 117   EXPECT_FALSE(service.IsTracked(contents)); | 142   EXPECT_TRUE(service_->IsTracked(contents)); | 
| 118   contents->SendOpenedNotification(&service); |  | 
| 119   EXPECT_TRUE(service.IsTracked(contents)); |  | 
| 120   delete contents; | 143   delete contents; | 
| 121   EXPECT_FALSE(service.IsTracked(contents)); | 144   EXPECT_FALSE(service_->IsTracked(contents)); | 
| 122 } | 145 } | 
| 123 | 146 | 
| 124 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { | 147 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { | 
| 125   TestingProfile profile; |  | 
| 126   BackgroundContentsService service(&profile, command_line_.get()); |  | 
| 127   BackgroundContentsServiceFactory::GetInstance()-> |  | 
| 128       RegisterUserPrefsOnBrowserContext(&profile); |  | 
| 129   GURL orig_url; | 148   GURL orig_url; | 
| 130   GURL url("http://a/"); | 149   GURL url("http://a/"); | 
| 131   GURL url2("http://a/"); | 150   GURL url2("http://a/"); | 
| 132   { | 151   { | 
| 133     scoped_ptr<MockBackgroundContents> contents( | 152     scoped_ptr<MockBackgroundContents> contents( | 
| 134         new MockBackgroundContents(&profile)); | 153         new MockBackgroundContents(profile_)); | 
| 135     EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 154     EXPECT_EQ(0U, GetPrefs(profile_)->size()); | 
| 136     contents->SendOpenedNotification(&service); | 155     contents->SendOpenedNotification(service_.get()); | 
| 137 | 156 | 
| 138     contents->Navigate(url); | 157     contents->Navigate(url); | 
| 139     EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 158     EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 140     EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 159     EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); | 
| 141 | 160 | 
| 142     // Navigate the contents to a new url, should not change url. | 161     // Navigate the contents to a new url, should not change url. | 
| 143     contents->Navigate(url2); | 162     contents->Navigate(url2); | 
| 144     EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 163     EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 145     EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 164     EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); | 
| 146   } | 165   } | 
| 147   // Contents are deleted, url should persist. | 166   // Contents are deleted, url should persist. | 
| 148   EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 167   EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 149 } | 168 } | 
| 150 | 169 | 
| 151 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { | 170 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { | 
| 152   TestingProfile profile; |  | 
| 153   BackgroundContentsService service(&profile, command_line_.get()); |  | 
| 154   BackgroundContentsServiceFactory::GetInstance()-> |  | 
| 155       RegisterUserPrefsOnBrowserContext(&profile); |  | 
| 156 |  | 
| 157   GURL url("http://a/"); | 171   GURL url("http://a/"); | 
| 158   MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 172   MockBackgroundContents* contents = new MockBackgroundContents(profile_); | 
| 159   EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 173   EXPECT_EQ(0U, GetPrefs(profile_)->size()); | 
| 160   contents->SendOpenedNotification(&service); | 174   contents->SendOpenedNotification(service_.get()); | 
| 161   contents->Navigate(url); | 175   contents->Navigate(url); | 
| 162   EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 176   EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 163   EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 177   EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); | 
| 164 | 178 | 
| 165   // Fake a window closed by script. | 179   // Fake a window closed by script. | 
| 166   contents->MockClose(&profile); | 180   contents->MockClose(profile_); | 
| 167   EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 181   EXPECT_EQ(0U, GetPrefs(profile_)->size()); | 
| 168 } | 182 } | 
| 169 | 183 | 
| 170 // Test what happens if a BackgroundContents shuts down (say, due to a renderer | 184 // Test what happens if a BackgroundContents shuts down (say, due to a renderer | 
| 171 // crash) then is restarted. Should not persist URL twice. | 185 // crash) then is restarted. Should not persist URL twice. | 
| 172 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { | 186 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { | 
| 173   TestingProfile profile; |  | 
| 174   BackgroundContentsService service(&profile, command_line_.get()); |  | 
| 175   BackgroundContentsServiceFactory::GetInstance()-> |  | 
| 176       RegisterUserPrefsOnBrowserContext(&profile); |  | 
| 177 |  | 
| 178   GURL url("http://a/"); | 187   GURL url("http://a/"); | 
| 179   { | 188   { | 
| 180     scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 189     scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 
| 181         &profile, "appid")); | 190         profile_, "appid")); | 
| 182     contents->SendOpenedNotification(&service); | 191     contents->SendOpenedNotification(service_.get()); | 
| 183     contents->Navigate(url); | 192     contents->Navigate(url); | 
| 184     EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 193     EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 185     EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 194     EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); | 
| 186   } | 195   } | 
| 187   // Contents deleted, url should be persisted. | 196   // Contents deleted, url should be persisted. | 
| 188   EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 197   EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 189 | 198 | 
| 190   { | 199   { | 
| 191     // Reopen the BackgroundContents to the same URL, we should not register the | 200     // Reopen the BackgroundContents to the same URL, we should not register the | 
| 192     // URL again. | 201     // URL again. | 
| 193     scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 202     scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 
| 194         &profile, "appid")); | 203         profile_, "appid")); | 
| 195     contents->SendOpenedNotification(&service); | 204     contents->SendOpenedNotification(service_.get()); | 
| 196     contents->Navigate(url); | 205     contents->Navigate(url); | 
| 197     EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 206     EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 198   } | 207   } | 
| 199 } | 208 } | 
| 200 | 209 | 
| 201 // Ensures that BackgroundContentsService properly tracks the association | 210 // Ensures that BackgroundContentsService properly tracks the association | 
| 202 // between a BackgroundContents and its parent extension, including | 211 // between a BackgroundContents and its parent extension, including | 
| 203 // unregistering the BC when the extension is uninstalled. | 212 // unregistering the BC when the extension is uninstalled. | 
| 204 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { | 213 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { | 
| 205   TestingProfile profile; | 214   EXPECT_EQ(NULL, service_->GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 
| 206   BackgroundContentsService service(&profile, command_line_.get()); | 215   MockBackgroundContents* contents = new MockBackgroundContents(profile_, | 
| 207   BackgroundContentsServiceFactory::GetInstance()-> |  | 
| 208       RegisterUserPrefsOnBrowserContext(&profile); |  | 
| 209 |  | 
| 210   EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |  | 
| 211   MockBackgroundContents* contents = new MockBackgroundContents(&profile, |  | 
| 212                                                                 "appid"); | 216                                                                 "appid"); | 
| 213   scoped_ptr<MockBackgroundContents> contents2( | 217   scoped_ptr<MockBackgroundContents> contents2( | 
| 214       new MockBackgroundContents(&profile, "appid2")); | 218       new MockBackgroundContents(profile_, "appid2")); | 
| 215   contents->SendOpenedNotification(&service); | 219   contents->SendOpenedNotification(service_.get()); | 
| 216   EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid())); | 220   EXPECT_EQ(contents, service_->GetAppBackgroundContents(contents->appid())); | 
| 217   contents2->SendOpenedNotification(&service); | 221   contents2->SendOpenedNotification(service_.get()); | 
| 218   EXPECT_EQ(contents2.get(), service.GetAppBackgroundContents( | 222   EXPECT_EQ(contents2.get(), service_->GetAppBackgroundContents( | 
| 219       contents2->appid())); | 223       contents2->appid())); | 
| 220   EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 224   EXPECT_EQ(0U, GetPrefs(profile_)->size()); | 
| 221 | 225 | 
| 222   // Navigate the contents, then make sure the one associated with the extension | 226   // Navigate the contents, then make sure the one associated with the extension | 
| 223   // is unregistered. | 227   // is unregistered. | 
| 224   GURL url("http://a/"); | 228   GURL url("http://a/"); | 
| 225   GURL url2("http://b/"); | 229   GURL url2("http://b/"); | 
| 226   contents->Navigate(url); | 230   contents->Navigate(url); | 
| 227   EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 231   EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 228   contents2->Navigate(url2); | 232   contents2->Navigate(url2); | 
| 229   EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 233   EXPECT_EQ(2U, GetPrefs(profile_)->size()); | 
| 230   service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 234   service_->ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 
| 231   EXPECT_FALSE(service.IsTracked(contents)); | 235   EXPECT_FALSE(service_->IsTracked(contents)); | 
| 232   EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 236   EXPECT_EQ(NULL, service_->GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 
| 233   EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 237   EXPECT_EQ(1U, GetPrefs(profile_)->size()); | 
| 234   EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 238   EXPECT_EQ(url2.spec(), GetPrefURLForApp(profile_, contents2->appid())); | 
| 235 } | 239 } | 
| OLD | NEW | 
|---|