| 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/command_line.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "chrome/browser/extensions/activity_log.h" | |
| 9 #include "chrome/browser/extensions/extension_service.h" | |
| 10 #include "chrome/browser/extensions/test_extension_system.h" | |
| 11 #include "chrome/common/chrome_constants.h" | |
| 12 #include "chrome/common/extensions/extension_builder.h" | |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 #include "sql/statement.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 class ActivityLogTest : public ChromeRenderViewHostTestHarness { | |
| 23 public: | |
| 24 ActivityLogTest() | |
| 25 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | |
| 26 db_thread_(BrowserThread::DB), | |
| 27 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} | |
| 28 | |
| 29 virtual void SetUp() OVERRIDE { | |
| 30 ChromeRenderViewHostTestHarness::SetUp(); | |
| 31 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 32 profile_ = | |
| 33 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 34 extension_service_ = static_cast<TestExtensionSystem*>( | |
| 35 ExtensionSystem::Get(profile_))->CreateExtensionService( | |
| 36 &command_line, FilePath(), false); | |
| 37 db_thread_.Start(); | |
| 38 } | |
| 39 | |
| 40 ~ActivityLogTest() { | |
| 41 base::WaitableEvent done(false, false); | |
| 42 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 43 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
| 44 done.Wait(); | |
| 45 db_thread_.Stop(); | |
| 46 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 47 MessageLoop::current()->Run(); | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 ExtensionService* extension_service_; | |
| 52 Profile* profile_; | |
| 53 | |
| 54 private: | |
| 55 content::TestBrowserThread ui_thread_; | |
| 56 content::TestBrowserThread db_thread_; | |
| 57 content::TestBrowserThread file_thread_; | |
| 58 }; | |
| 59 | |
| 60 TEST_F(ActivityLogTest, ConstructAndLog) { | |
| 61 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); | |
| 62 scoped_refptr<const Extension> extension = | |
| 63 ExtensionBuilder() | |
| 64 .SetManifest(DictionaryBuilder() | |
| 65 .Set("name", "Test extension") | |
| 66 .Set("version", "1.0.0") | |
| 67 .Set("manifest_version", 2)) | |
| 68 .Build(); | |
| 69 extension_service_->AddExtension(extension); | |
| 70 scoped_ptr<ListValue> args(new ListValue()); | |
| 71 for (int i = 0; i < 30; i++) { | |
| 72 // Run this a bunch of times and hope that if something goes wrong with | |
| 73 // threading, 30 times is enough to cause it to fail. | |
| 74 activity_log->LogAPIAction(extension, | |
| 75 std::string("tabs.testMethod"), | |
| 76 args.get(), | |
| 77 ""); | |
| 78 } | |
| 79 // Need to ensure the writes were completed. | |
| 80 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(3)); | |
| 81 FilePath db_file = profile_->GetPath().Append( | |
| 82 chrome::kExtensionActivityLogFilename); | |
| 83 sql::Connection db; | |
| 84 ASSERT_TRUE(db.Open(db_file)); | |
| 85 std::string sql_str = "SELECT * FROM " + | |
| 86 std::string(APIAction::kTableName); | |
| 87 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); | |
| 88 ASSERT_TRUE(statement.Step()); | |
| 89 ASSERT_EQ("UNKNOWN_ACTION", statement.ColumnString(2)); | |
| 90 ASSERT_EQ("TABS", statement.ColumnString(3)); | |
| 91 ASSERT_EQ("tabs.testMethod()", statement.ColumnString(4)); | |
| 92 } | |
| 93 | |
| 94 } // namespace extensions | |
| OLD | NEW |