OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/message_loop.h" | |
7 #include "chrome/browser/extensions/app_notification_manager.h" | |
8 #include "chrome/browser/extensions/app_notification_test_util.h" | |
9 #include "chrome/common/chrome_notification_types.h" | |
10 #include "chrome/common/extensions/extension.h" | |
11 #include "chrome/common/extensions/extension_manifest_constants.h" | |
12 #include "chrome/common/extensions/extension_test_util.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "content/public/browser/notification_details.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/notification_source.h" | |
17 #include "content/public/test/test_browser_thread.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace util = app_notification_test_util; | |
23 | |
24 namespace extensions { | |
25 | |
26 class AppNotificationManagerTest : public testing::Test { | |
27 public: | |
28 AppNotificationManagerTest() | |
29 : ui_thread_(BrowserThread::UI, &ui_loop_), | |
30 file_thread_(BrowserThread::FILE) {} | |
31 | |
32 virtual ~AppNotificationManagerTest() {} | |
33 | |
34 virtual void SetUp() OVERRIDE { | |
35 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
36 file_thread_.Start(); | |
37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
38 profile_.reset(new TestingProfile(temp_dir_.path())); | |
39 InitializeManager(); | |
40 } | |
41 | |
42 virtual void TearDown() OVERRIDE { | |
43 mgr_ = NULL; | |
44 // Let the manager's shutdown of storage on the file thread complete. | |
45 WaitForFileThread(); | |
46 } | |
47 | |
48 protected: | |
49 void InitializeManager() { | |
50 if (mgr_.get()) | |
51 WaitForFileThread(); | |
52 mgr_ = new AppNotificationManager(profile_.get()); | |
53 mgr_->Init(); | |
54 WaitForFileThread(); | |
55 } | |
56 | |
57 static void PostQuitToUIThread() { | |
58 BrowserThread::PostTask(BrowserThread::UI, | |
59 FROM_HERE, | |
60 MessageLoop::QuitClosure()); | |
61 } | |
62 | |
63 static void WaitForFileThread() { | |
64 BrowserThread::PostTask(BrowserThread::FILE, | |
65 FROM_HERE, | |
66 base::Bind(&PostQuitToUIThread)); | |
67 MessageLoop::current()->Run(); | |
68 } | |
69 | |
70 MessageLoop ui_loop_; | |
71 content::TestBrowserThread ui_thread_; | |
72 content::TestBrowserThread file_thread_; | |
73 base::ScopedTempDir temp_dir_; | |
74 scoped_ptr<TestingProfile> profile_; | |
75 scoped_refptr<AppNotificationManager> mgr_; | |
76 }; | |
77 | |
78 TEST_F(AppNotificationManagerTest, Simple) { | |
79 std::string id = extension_test_util::MakeId("whatever"); | |
80 AppNotificationList list; | |
81 util::AddNotifications(&list, id, 2, "foo"); | |
82 EXPECT_TRUE(util::AddCopiesFromList(mgr_.get(), list)); | |
83 | |
84 // Cause |mgr_| to be recreated, re-reading from its storage. | |
85 InitializeManager(); | |
86 | |
87 const AppNotification* tmp = mgr_->GetLast(id); | |
88 ASSERT_TRUE(tmp); | |
89 EXPECT_EQ(list[1]->guid(), tmp->guid()); | |
90 EXPECT_EQ(list[1]->extension_id(), tmp->extension_id()); | |
91 EXPECT_EQ(list[1]->is_local(), tmp->is_local()); | |
92 EXPECT_TRUE(list[1]->Equals(*tmp)); | |
93 const AppNotificationList* tmp_list = mgr_->GetAll(id); | |
94 ASSERT_TRUE(tmp_list != NULL); | |
95 util::ExpectListsEqual(list, *tmp_list); | |
96 mgr_->ClearAll(id); | |
97 EXPECT_EQ(NULL, mgr_->GetLast(id)); | |
98 EXPECT_EQ(NULL, mgr_->GetAll(id)); | |
99 } | |
100 | |
101 // Test that AppNotificationManager correctly listens to EXTENSION_UNINSTALLED | |
102 // notifications and removes associated data when that happens. | |
103 TEST_F(AppNotificationManagerTest, ExtensionUninstall) { | |
104 // Add some items from two test extension ids. | |
105 scoped_refptr<Extension> extension1 = | |
106 extension_test_util::CreateExtensionWithID("id1"); | |
107 scoped_refptr<Extension> extension2 = | |
108 extension_test_util::CreateExtensionWithID("id2"); | |
109 std::string id1 = extension1->id(); | |
110 std::string id2 = extension2->id(); | |
111 AppNotificationList list1; | |
112 AppNotificationList list2; | |
113 util::AddNotifications(&list1, id1, 5, "foo1"); | |
114 util::AddNotifications(&list2, id2, 3, "foo2"); | |
115 util::AddCopiesFromList(mgr_.get(), list1); | |
116 util::AddCopiesFromList(mgr_.get(), list2); | |
117 util::ExpectListsEqual(list1, *mgr_->GetAll(id1)); | |
118 util::ExpectListsEqual(list2, *mgr_->GetAll(id2)); | |
119 | |
120 // Send the uninstall notification for extension id1. | |
121 content::NotificationService::current()->Notify( | |
122 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | |
123 content::Source<Profile>(profile_.get()), | |
124 content::Details<const Extension>(extension1.get())); | |
125 | |
126 // The id1 items should be gone but the id2 items should still be there. | |
127 EXPECT_EQ(NULL, mgr_->GetLast(id1)); | |
128 EXPECT_EQ(NULL, mgr_->GetAll(id1)); | |
129 util::ExpectListsEqual(list2, *mgr_->GetAll(id2)); | |
130 } | |
131 | |
132 } // namespace extensions | |
OLD | NEW |