| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 entry. | 3 // found in the LICENSE entry. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/util/extensions_activity_monitor.h" | 5 #include "chrome/browser/sync/util/extensions_activity_monitor.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/waitable_event.h" | 9 #include "base/waitable_event.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/browser/extensions/extension_bookmarks_module.h" | 11 #include "chrome/browser/extensions/extension_bookmarks_module.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using browser_sync::ExtensionsActivityMonitor; | 17 using browser_sync::ExtensionsActivityMonitor; |
| 18 namespace keys = extension_manifest_keys; | 18 namespace keys = extension_manifest_keys; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 static const char* kTestExtensionPath1 = "c:\\testextension1"; | 22 const char* kTestExtensionPath1 = "c:\\testextension1"; |
| 23 static const char* kTestExtensionPath2 = "c:\\testextension2"; | 23 const char* kTestExtensionPath2 = "c:\\testextension2"; |
| 24 static const char* kTestExtensionVersion = "1.0.0.0"; | 24 const char* kTestExtensionVersion = "1.0.0.0"; |
| 25 static const char* kTestExtensionName = "foo extension"; | 25 const char* kTestExtensionName = "foo extension"; |
| 26 | 26 |
| 27 template <class FunctionType> | 27 template <class FunctionType> |
| 28 class BookmarkAPIEventTask : public Task { | 28 class BookmarkAPIEventTask : public Task { |
| 29 public: | 29 public: |
| 30 BookmarkAPIEventTask(FunctionType* t, Extension* e, size_t repeats, | 30 BookmarkAPIEventTask(FunctionType* t, Extension* e, size_t repeats, |
| 31 base::WaitableEvent* done) : | 31 base::WaitableEvent* done) : |
| 32 function_(t), extension_(e), repeats_(repeats), done_(done) {} | 32 extension_(e), function_(t), repeats_(repeats), done_(done) {} |
| 33 virtual void Run() { | 33 virtual void Run() { |
| 34 for (size_t i = 0; i < repeats_; i++) { | 34 for (size_t i = 0; i < repeats_; i++) { |
| 35 NotificationService::current()->Notify( | 35 NotificationService::current()->Notify( |
| 36 NotificationType::EXTENSION_BOOKMARKS_API_INVOKED, | 36 NotificationType::EXTENSION_BOOKMARKS_API_INVOKED, |
| 37 Source<Extension>(extension_.get()), | 37 Source<Extension>(extension_.get()), |
| 38 Details<const BookmarksFunction>(function_.get())); | 38 Details<const BookmarksFunction>(function_.get())); |
| 39 } | 39 } |
| 40 done_->Signal(); | 40 done_->Signal(); |
| 41 } | 41 } |
| 42 private: | 42 private: |
| 43 scoped_ptr<Extension> extension_; | 43 scoped_ptr<Extension> extension_; |
| 44 scoped_refptr<FunctionType> function_; | 44 scoped_refptr<FunctionType> function_; |
| 45 size_t repeats_; | 45 size_t repeats_; |
| 46 base::WaitableEvent* done_; | 46 base::WaitableEvent* done_; |
| 47 | 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(BookmarkAPIEventTask); | 48 DISALLOW_COPY_AND_ASSIGN(BookmarkAPIEventTask); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 class BookmarkAPIEventGenerator { | 51 class BookmarkAPIEventGenerator { |
| 52 public: | 52 public: |
| 53 BookmarkAPIEventGenerator(MessageLoop* ui_loop) : ui_loop_(ui_loop) {} | 53 BookmarkAPIEventGenerator() {} |
| 54 virtual ~BookmarkAPIEventGenerator() {} | 54 virtual ~BookmarkAPIEventGenerator() {} |
| 55 template <class T> | 55 template <class T> |
| 56 void NewEvent(const std::string& extension_path, | 56 void NewEvent(const std::string& extension_path, |
| 57 T* bookmarks_function, size_t repeats) { | 57 T* bookmarks_function, size_t repeats) { |
| 58 #if defined(OS_WIN) |
| 58 FilePath path(UTF8ToWide(extension_path)); | 59 FilePath path(UTF8ToWide(extension_path)); |
| 60 #elif defined(OS_POSIX) |
| 61 FilePath path(extension_path); |
| 62 #endif |
| 59 Extension* extension = new Extension(path); | 63 Extension* extension = new Extension(path); |
| 60 std::string error; | 64 std::string error; |
| 61 DictionaryValue input; | 65 DictionaryValue input; |
| 62 input.SetString(keys::kVersion, kTestExtensionVersion); | 66 input.SetString(keys::kVersion, kTestExtensionVersion); |
| 63 input.SetString(keys::kName, kTestExtensionName); | 67 input.SetString(keys::kName, kTestExtensionName); |
| 64 extension->InitFromValue(input, false, &error); | 68 extension->InitFromValue(input, false, &error); |
| 65 bookmarks_function->set_name(T::function_name()); | 69 bookmarks_function->set_name(T::function_name()); |
| 66 base::WaitableEvent done_event(false, false); | 70 base::WaitableEvent done_event(false, false); |
| 67 ui_loop_->PostTask(FROM_HERE, new BookmarkAPIEventTask<T>( | 71 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 68 bookmarks_function, extension, repeats, &done_event)); | 72 new BookmarkAPIEventTask<T>(bookmarks_function, extension, |
| 73 repeats, &done_event)); |
| 69 done_event.Wait(); | 74 done_event.Wait(); |
| 70 } | 75 } |
| 71 | 76 |
| 72 private: | 77 private: |
| 73 MessageLoop* ui_loop_; | |
| 74 DISALLOW_COPY_AND_ASSIGN(BookmarkAPIEventGenerator); | 78 DISALLOW_COPY_AND_ASSIGN(BookmarkAPIEventGenerator); |
| 75 }; | 79 }; |
| 76 } // namespace | 80 } // namespace |
| 77 | 81 |
| 78 class DoUIThreadSetupTask : public Task { | 82 class DoUIThreadSetupTask : public Task { |
| 79 public: | 83 public: |
| 80 DoUIThreadSetupTask(NotificationService** service, | 84 DoUIThreadSetupTask(NotificationService** service, |
| 81 base::WaitableEvent* done) | 85 base::WaitableEvent* done) |
| 82 : service_(service), signal_when_done_(done) {} | 86 : service_(service), signal_when_done_(done) {} |
| 83 virtual ~DoUIThreadSetupTask() {} | 87 virtual ~DoUIThreadSetupTask() {} |
| (...skipping 23 matching lines...) Expand all Loading... |
| 107 | 111 |
| 108 virtual void TearDown() { | 112 virtual void TearDown() { |
| 109 ui_thread_.message_loop()->DeleteSoon(FROM_HERE, service_); | 113 ui_thread_.message_loop()->DeleteSoon(FROM_HERE, service_); |
| 110 ui_thread_.Stop(); | 114 ui_thread_.Stop(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 MessageLoop* ui_loop() { return ui_thread_.message_loop(); } | 117 MessageLoop* ui_loop() { return ui_thread_.message_loop(); } |
| 114 | 118 |
| 115 static std::string GetExtensionIdForPath(const std::string& extension_path) { | 119 static std::string GetExtensionIdForPath(const std::string& extension_path) { |
| 116 std::string error; | 120 std::string error; |
| 121 #if defined(OS_WIN) |
| 117 FilePath path(UTF8ToWide(extension_path)); | 122 FilePath path(UTF8ToWide(extension_path)); |
| 123 #elif defined(OS_POSIX) |
| 124 FilePath path(extension_path); |
| 125 #endif |
| 118 Extension e(path); | 126 Extension e(path); |
| 119 DictionaryValue input; | 127 DictionaryValue input; |
| 120 input.SetString(keys::kVersion, kTestExtensionVersion); | 128 input.SetString(keys::kVersion, kTestExtensionVersion); |
| 121 input.SetString(keys::kName, kTestExtensionName); | 129 input.SetString(keys::kName, kTestExtensionName); |
| 122 e.InitFromValue(input, false, &error); | 130 e.InitFromValue(input, false, &error); |
| 123 EXPECT_EQ("", error); | 131 EXPECT_EQ("", error); |
| 124 return e.id(); | 132 return e.id(); |
| 125 } | 133 } |
| 126 private: | 134 private: |
| 127 NotificationService* service_; | 135 NotificationService* service_; |
| 128 ChromeThread ui_thread_; | 136 ChromeThread ui_thread_; |
| 129 }; | 137 }; |
| 130 | 138 |
| 131 TEST_F(ExtensionsActivityMonitorTest, Basic) { | 139 TEST_F(ExtensionsActivityMonitorTest, Basic) { |
| 132 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(ui_loop()); | 140 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(); |
| 133 BookmarkAPIEventGenerator generator(ui_loop()); | 141 BookmarkAPIEventGenerator generator; |
| 134 | 142 |
| 135 generator.NewEvent<RemoveBookmarkFunction>(kTestExtensionPath1, | 143 generator.NewEvent<RemoveBookmarkFunction>(kTestExtensionPath1, |
| 136 new RemoveBookmarkFunction(), 1); | 144 new RemoveBookmarkFunction(), 1); |
| 137 generator.NewEvent<MoveBookmarkFunction>(kTestExtensionPath1, | 145 generator.NewEvent<MoveBookmarkFunction>(kTestExtensionPath1, |
| 138 new MoveBookmarkFunction(), 1); | 146 new MoveBookmarkFunction(), 1); |
| 139 generator.NewEvent<UpdateBookmarkFunction>(kTestExtensionPath1, | 147 generator.NewEvent<UpdateBookmarkFunction>(kTestExtensionPath1, |
| 140 new UpdateBookmarkFunction(), 2); | 148 new UpdateBookmarkFunction(), 2); |
| 141 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, | 149 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, |
| 142 new CreateBookmarkFunction(), 3); | 150 new CreateBookmarkFunction(), 3); |
| 143 generator.NewEvent<SearchBookmarksFunction>(kTestExtensionPath1, | 151 generator.NewEvent<SearchBookmarksFunction>(kTestExtensionPath1, |
| 144 new SearchBookmarksFunction(), 5); | 152 new SearchBookmarksFunction(), 5); |
| 145 const int writes_by_extension1 = 1 + 1 + 2 + 3; | 153 const uint32 writes_by_extension1 = 1 + 1 + 2 + 3; |
| 146 | 154 |
| 147 generator.NewEvent<RemoveTreeBookmarkFunction>(kTestExtensionPath2, | 155 generator.NewEvent<RemoveTreeBookmarkFunction>(kTestExtensionPath2, |
| 148 new RemoveTreeBookmarkFunction(), 8); | 156 new RemoveTreeBookmarkFunction(), 8); |
| 149 generator.NewEvent<GetBookmarkTreeFunction>(kTestExtensionPath2, | 157 generator.NewEvent<GetBookmarkTreeFunction>(kTestExtensionPath2, |
| 150 new GetBookmarkTreeFunction(), 13); | 158 new GetBookmarkTreeFunction(), 13); |
| 151 generator.NewEvent<GetBookmarkChildrenFunction>(kTestExtensionPath2, | 159 generator.NewEvent<GetBookmarkChildrenFunction>(kTestExtensionPath2, |
| 152 new GetBookmarkChildrenFunction(), 21); | 160 new GetBookmarkChildrenFunction(), 21); |
| 153 generator.NewEvent<GetBookmarksFunction>(kTestExtensionPath2, | 161 generator.NewEvent<GetBookmarksFunction>(kTestExtensionPath2, |
| 154 new GetBookmarksFunction(), 33); | 162 new GetBookmarksFunction(), 33); |
| 155 const int writes_by_extension2 = 8; | 163 const uint32 writes_by_extension2 = 8; |
| 156 | 164 |
| 157 ExtensionsActivityMonitor::Records results; | 165 ExtensionsActivityMonitor::Records results; |
| 158 monitor->GetAndClearRecords(&results); | 166 monitor->GetAndClearRecords(&results); |
| 159 | 167 |
| 160 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); | 168 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); |
| 161 std::string id2 = GetExtensionIdForPath(kTestExtensionPath2); | 169 std::string id2 = GetExtensionIdForPath(kTestExtensionPath2); |
| 162 | 170 |
| 163 EXPECT_EQ(2, results.size()); | 171 EXPECT_EQ(2U, results.size()); |
| 164 EXPECT_TRUE(results.end() != results.find(id1)); | 172 EXPECT_TRUE(results.end() != results.find(id1)); |
| 165 EXPECT_TRUE(results.end() != results.find(id2)); | 173 EXPECT_TRUE(results.end() != results.find(id2)); |
| 166 EXPECT_EQ(writes_by_extension1, results[id1].bookmark_write_count); | 174 EXPECT_EQ(writes_by_extension1, results[id1].bookmark_write_count); |
| 167 EXPECT_EQ(writes_by_extension2, results[id2].bookmark_write_count); | 175 EXPECT_EQ(writes_by_extension2, results[id2].bookmark_write_count); |
| 168 | 176 |
| 169 ui_loop()->DeleteSoon(FROM_HERE, monitor); | 177 ui_loop()->DeleteSoon(FROM_HERE, monitor); |
| 170 } | 178 } |
| 171 | 179 |
| 172 TEST_F(ExtensionsActivityMonitorTest, Put) { | 180 TEST_F(ExtensionsActivityMonitorTest, Put) { |
| 173 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(ui_loop()); | 181 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(); |
| 174 BookmarkAPIEventGenerator generator(ui_loop()); | 182 BookmarkAPIEventGenerator generator; |
| 175 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); | 183 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); |
| 176 std::string id2 = GetExtensionIdForPath(kTestExtensionPath2); | 184 std::string id2 = GetExtensionIdForPath(kTestExtensionPath2); |
| 177 | 185 |
| 178 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, | 186 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, |
| 179 new CreateBookmarkFunction(), 5); | 187 new CreateBookmarkFunction(), 5); |
| 180 generator.NewEvent<MoveBookmarkFunction>(kTestExtensionPath2, | 188 generator.NewEvent<MoveBookmarkFunction>(kTestExtensionPath2, |
| 181 new MoveBookmarkFunction(), 8); | 189 new MoveBookmarkFunction(), 8); |
| 182 | 190 |
| 183 ExtensionsActivityMonitor::Records results; | 191 ExtensionsActivityMonitor::Records results; |
| 184 monitor->GetAndClearRecords(&results); | 192 monitor->GetAndClearRecords(&results); |
| 185 | 193 |
| 186 EXPECT_EQ(2, results.size()); | 194 EXPECT_EQ(2U, results.size()); |
| 187 EXPECT_EQ(5, results[id1].bookmark_write_count); | 195 EXPECT_EQ(5U, results[id1].bookmark_write_count); |
| 188 EXPECT_EQ(8, results[id2].bookmark_write_count); | 196 EXPECT_EQ(8U, results[id2].bookmark_write_count); |
| 189 | 197 |
| 190 generator.NewEvent<GetBookmarksFunction>(kTestExtensionPath2, | 198 generator.NewEvent<GetBookmarksFunction>(kTestExtensionPath2, |
| 191 new GetBookmarksFunction(), 3); | 199 new GetBookmarksFunction(), 3); |
| 192 generator.NewEvent<UpdateBookmarkFunction>(kTestExtensionPath2, | 200 generator.NewEvent<UpdateBookmarkFunction>(kTestExtensionPath2, |
| 193 new UpdateBookmarkFunction(), 2); | 201 new UpdateBookmarkFunction(), 2); |
| 194 | 202 |
| 195 // Simulate a commit failure, which augments the active record set with the | 203 // Simulate a commit failure, which augments the active record set with the |
| 196 // refugee records. | 204 // refugee records. |
| 197 monitor->PutRecords(results); | 205 monitor->PutRecords(results); |
| 198 ExtensionsActivityMonitor::Records new_records; | 206 ExtensionsActivityMonitor::Records new_records; |
| 199 monitor->GetAndClearRecords(&new_records); | 207 monitor->GetAndClearRecords(&new_records); |
| 200 | 208 |
| 201 EXPECT_EQ(2, results.size()); | 209 EXPECT_EQ(2U, results.size()); |
| 202 EXPECT_EQ(id1, new_records[id1].extension_id); | 210 EXPECT_EQ(id1, new_records[id1].extension_id); |
| 203 EXPECT_EQ(id2, new_records[id2].extension_id); | 211 EXPECT_EQ(id2, new_records[id2].extension_id); |
| 204 EXPECT_EQ(5, new_records[id1].bookmark_write_count); | 212 EXPECT_EQ(5U, new_records[id1].bookmark_write_count); |
| 205 EXPECT_EQ(8 + 2, new_records[id2].bookmark_write_count); | 213 EXPECT_EQ(8U + 2U, new_records[id2].bookmark_write_count); |
| 206 ui_loop()->DeleteSoon(FROM_HERE, monitor); | 214 ui_loop()->DeleteSoon(FROM_HERE, monitor); |
| 207 } | 215 } |
| 208 | 216 |
| 209 TEST_F(ExtensionsActivityMonitorTest, MultiGet) { | 217 TEST_F(ExtensionsActivityMonitorTest, MultiGet) { |
| 210 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(ui_loop()); | 218 ExtensionsActivityMonitor* monitor = new ExtensionsActivityMonitor(); |
| 211 BookmarkAPIEventGenerator generator(ui_loop()); | 219 BookmarkAPIEventGenerator generator; |
| 212 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); | 220 std::string id1 = GetExtensionIdForPath(kTestExtensionPath1); |
| 213 | 221 |
| 214 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, | 222 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, |
| 215 new CreateBookmarkFunction(), 5); | 223 new CreateBookmarkFunction(), 5); |
| 216 | 224 |
| 217 ExtensionsActivityMonitor::Records results; | 225 ExtensionsActivityMonitor::Records results; |
| 218 monitor->GetAndClearRecords(&results); | 226 monitor->GetAndClearRecords(&results); |
| 219 | 227 |
| 220 EXPECT_EQ(1, results.size()); | 228 EXPECT_EQ(1U, results.size()); |
| 221 EXPECT_EQ(5, results[id1].bookmark_write_count); | 229 EXPECT_EQ(5U, results[id1].bookmark_write_count); |
| 222 | 230 |
| 223 monitor->GetAndClearRecords(&results); | 231 monitor->GetAndClearRecords(&results); |
| 224 EXPECT_TRUE(results.empty()); | 232 EXPECT_TRUE(results.empty()); |
| 225 | 233 |
| 226 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, | 234 generator.NewEvent<CreateBookmarkFunction>(kTestExtensionPath1, |
| 227 new CreateBookmarkFunction(), 3); | 235 new CreateBookmarkFunction(), 3); |
| 228 monitor->GetAndClearRecords(&results); | 236 monitor->GetAndClearRecords(&results); |
| 229 | 237 |
| 230 EXPECT_EQ(1, results.size()); | 238 EXPECT_EQ(1U, results.size()); |
| 231 EXPECT_EQ(3, results[id1].bookmark_write_count); | 239 EXPECT_EQ(3U, results[id1].bookmark_write_count); |
| 232 | 240 |
| 233 ui_loop()->DeleteSoon(FROM_HERE, monitor); | 241 ui_loop()->DeleteSoon(FROM_HERE, monitor); |
| 234 } | 242 } |
| OLD | NEW |