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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 virtual void SetUp() { | 30 virtual void SetUp() { |
31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); |
32 } | 32 } |
33 | 33 |
34 const DictionaryValue* GetPrefs(Profile* profile) { | 34 const DictionaryValue* GetPrefs(Profile* profile) { |
35 return profile->GetPrefs()->GetDictionary( | 35 return profile->GetPrefs()->GetDictionary( |
36 prefs::kRegisteredBackgroundContents); | 36 prefs::kRegisteredBackgroundContents); |
37 } | 37 } |
38 | 38 |
39 // Returns the stored pref URL for the passed app id. | 39 // Returns the stored pref URL for the passed app id. |
40 std::string GetPrefURLForApp(Profile* profile, const string16& appid) { | 40 std::string GetPrefURLForApp(Profile* profile, const base::string16& appid) { |
41 const DictionaryValue* pref = GetPrefs(profile); | 41 const DictionaryValue* pref = GetPrefs(profile); |
42 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); | 42 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); |
43 const DictionaryValue* value; | 43 const DictionaryValue* value; |
44 pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); | 44 pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); |
45 std::string url; | 45 std::string url; |
46 value->GetString("url", &url); | 46 value->GetString("url", &url); |
47 return url; | 47 return url; |
48 } | 48 } |
49 | 49 |
50 scoped_ptr<CommandLine> command_line_; | 50 scoped_ptr<CommandLine> command_line_; |
51 }; | 51 }; |
52 | 52 |
53 class MockBackgroundContents : public BackgroundContents { | 53 class MockBackgroundContents : public BackgroundContents { |
54 public: | 54 public: |
55 explicit MockBackgroundContents(Profile* profile) | 55 explicit MockBackgroundContents(Profile* profile) |
56 : appid_(ASCIIToUTF16("app_id")), | 56 : appid_(ASCIIToUTF16("app_id")), |
57 profile_(profile) { | 57 profile_(profile) { |
58 } | 58 } |
59 MockBackgroundContents(Profile* profile, const std::string& id) | 59 MockBackgroundContents(Profile* profile, const std::string& id) |
60 : appid_(ASCIIToUTF16(id)), | 60 : appid_(ASCIIToUTF16(id)), |
61 profile_(profile) { | 61 profile_(profile) { |
62 } | 62 } |
63 | 63 |
64 void SendOpenedNotification(BackgroundContentsService* service) { | 64 void SendOpenedNotification(BackgroundContentsService* service) { |
65 string16 frame_name = ASCIIToUTF16("background"); | 65 base::string16 frame_name = ASCIIToUTF16("background"); |
66 BackgroundContentsOpenedDetails details = { | 66 BackgroundContentsOpenedDetails details = { |
67 this, frame_name, appid_ }; | 67 this, frame_name, appid_ }; |
68 service->BackgroundContentsOpened(&details); | 68 service->BackgroundContentsOpened(&details); |
69 } | 69 } |
70 | 70 |
71 virtual void Navigate(GURL url) { | 71 virtual void Navigate(GURL url) { |
72 url_ = url; | 72 url_ = url; |
73 content::NotificationService::current()->Notify( | 73 content::NotificationService::current()->Notify( |
74 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, | 74 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, |
75 content::Source<Profile>(profile_), | 75 content::Source<Profile>(profile_), |
76 content::Details<BackgroundContents>(this)); | 76 content::Details<BackgroundContents>(this)); |
77 } | 77 } |
78 virtual const GURL& GetURL() const OVERRIDE { return url_; } | 78 virtual const GURL& GetURL() const OVERRIDE { return url_; } |
79 | 79 |
80 void MockClose(Profile* profile) { | 80 void MockClose(Profile* profile) { |
81 content::NotificationService::current()->Notify( | 81 content::NotificationService::current()->Notify( |
82 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED, | 82 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED, |
83 content::Source<Profile>(profile), | 83 content::Source<Profile>(profile), |
84 content::Details<BackgroundContents>(this)); | 84 content::Details<BackgroundContents>(this)); |
85 delete this; | 85 delete this; |
86 } | 86 } |
87 | 87 |
88 virtual ~MockBackgroundContents() { | 88 virtual ~MockBackgroundContents() { |
89 content::NotificationService::current()->Notify( | 89 content::NotificationService::current()->Notify( |
90 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, | 90 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, |
91 content::Source<Profile>(profile_), | 91 content::Source<Profile>(profile_), |
92 content::Details<BackgroundContents>(this)); | 92 content::Details<BackgroundContents>(this)); |
93 } | 93 } |
94 | 94 |
95 const string16& appid() { return appid_; } | 95 const base::string16& appid() { return appid_; } |
96 | 96 |
97 private: | 97 private: |
98 GURL url_; | 98 GURL url_; |
99 | 99 |
100 // The ID of our parent application | 100 // The ID of our parent application |
101 string16 appid_; | 101 base::string16 appid_; |
102 | 102 |
103 // Parent profile | 103 // Parent profile |
104 Profile* profile_; | 104 Profile* profile_; |
105 }; | 105 }; |
106 | 106 |
107 TEST_F(BackgroundContentsServiceTest, Create) { | 107 TEST_F(BackgroundContentsServiceTest, Create) { |
108 // Check for creation and leaks. | 108 // Check for creation and leaks. |
109 TestingProfile profile; | 109 TestingProfile profile; |
110 BackgroundContentsService service(&profile, command_line_.get()); | 110 BackgroundContentsService service(&profile, command_line_.get()); |
111 } | 111 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 contents->Navigate(url); | 226 contents->Navigate(url); |
227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
228 contents2->Navigate(url2); | 228 contents2->Navigate(url2); |
229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); |
230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); |
231 EXPECT_FALSE(service.IsTracked(contents)); | 231 EXPECT_FALSE(service.IsTracked(contents)); |
232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); |
235 } | 235 } |
OLD | NEW |