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