diff options
author | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-19 21:41:36 +0000 |
---|---|---|
committer | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-19 21:41:36 +0000 |
commit | e6e376e2851b35b5ee58935c100452379ce0c1ae (patch) | |
tree | d9171a564b5eaf62c0436b21828397b504e68485 /chrome | |
parent | aaeaa620cf80c5d07560c1fc173dc13e38db9aee (diff) | |
download | chromium_src-e6e376e2851b35b5ee58935c100452379ce0c1ae.zip chromium_src-e6e376e2851b35b5ee58935c100452379ce0c1ae.tar.gz chromium_src-e6e376e2851b35b5ee58935c100452379ce0c1ae.tar.bz2 |
Add automation hooks for fetching history.
Adding a test which exercises the GetHistoryInfo() hooks.
BUG=39275
TEST=python chrome/test/functional/history.py
Review URL: http://codereview.chromium.org/1648015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44964 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 30 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 4 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider_observers.cc | 29 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider_observers.h | 18 | ||||
-rw-r--r-- | chrome/test/functional/history.py | 38 | ||||
-rw-r--r-- | chrome/test/pyautolib/history_info.py | 80 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 23 |
7 files changed, 222 insertions, 0 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index 023b337..dadc49b 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -1571,6 +1571,33 @@ void AutomationProvider::RemoveBookmark(int handle, *success = false; } +// Sample json input: { 'command': 'GetHistoryInfo', +// 'search_text': 'some text' } +// Refer chrome/test/pyautolib/history_info.py for sample json output. +void AutomationProvider::GetHistoryInfo( + DictionaryValue* args, + IPC::Message* reply_message) { + consumer_.CancelAllRequests(); + + std::wstring search_text; + args->GetString(L"search_text", &search_text); + + // Fetch history. + HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); + history::QueryOptions options; + // The observer owns itself. It deletes itself after it fetches history. + AutomationProviderHistoryObserver* history_observer = + new AutomationProviderHistoryObserver(this, reply_message); + hs->QueryHistory( + search_text, + options, + &consumer_, + NewCallback(history_observer, + &AutomationProviderHistoryObserver::HistoryQueryComplete)); +} + +// Sample json input: { 'command': 'GetDownloadsInfo' } +// Refer chrome/test/pyautolib/download_info.py for sample json output. void AutomationProvider::GetDownloadsInfo( DictionaryValue* args, IPC::Message* reply_message) { @@ -1720,6 +1747,9 @@ void AutomationProvider::SendJSONRequest( if (command == "GetDownloadsInfo") { this->GetDownloadsInfo(dict_value, reply_message); return; + } else if (command == "GetHistoryInfo") { + this->GetHistoryInfo(dict_value, reply_message); + return; } else if (command == "WaitForAllDownloadsToComplete") { this->WaitForDownloadsToComplete(dict_value, reply_message); return; diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index 6329a3a..9c502ec 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -339,6 +339,10 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, DictionaryValue* args, IPC::Message* reply_message); + // Get info about history. + // Uses the JSON interface for input/output. + void GetHistoryInfo(DictionaryValue* args, IPC::Message* reply_message); + // Generic pattern for pyautolib void SendJSONRequest(int handle, std::string json_request, diff --git a/chrome/browser/automation/automation_provider_observers.cc b/chrome/browser/automation/automation_provider_observers.cc index d4416c2..a62c11b 100644 --- a/chrome/browser/automation/automation_provider_observers.cc +++ b/chrome/browser/automation/automation_provider_observers.cc @@ -5,6 +5,7 @@ #include "chrome/browser/automation/automation_provider_observers.h" #include "base/basictypes.h" +#include "base/json/json_writer.h" #include "base/string_util.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/automation/automation_provider.h" @@ -942,3 +943,31 @@ void AutomationProviderDownloadItemObserver::OnDownloadFileCompleted( delete this; } } + +void AutomationProviderHistoryObserver::HistoryQueryComplete( + HistoryService::Handle request_handle, + history::QueryResults* results) { + std::string json_return; + bool reply_return = true; + scoped_ptr<DictionaryValue> return_value(new DictionaryValue); + + ListValue* history_list = new ListValue; + for (size_t i = 0; i < results->size(); ++i) { + DictionaryValue* page_value = new DictionaryValue; + history::URLResult const &page = (*results)[i]; + page_value->SetString(L"title", page.title()); + page_value->SetString(L"url", page.url().spec()); + page_value->SetInteger(L"time", + static_cast<int>(page.visit_time().ToTimeT())); + page_value->SetString(L"snippet", page.snippet().text()); + history_list->Append(page_value); + } + + return_value->Set(L"history", history_list); + // Return history info. + base::JSONWriter::Write(return_value.get(), false, &json_return); + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message_, json_return, reply_return); + provider_->Send(reply_message_); + delete this; +} diff --git a/chrome/browser/automation/automation_provider_observers.h b/chrome/browser/automation/automation_provider_observers.h index 6bed0f6..e6a082e 100644 --- a/chrome/browser/automation/automation_provider_observers.h +++ b/chrome/browser/automation/automation_provider_observers.h @@ -583,4 +583,22 @@ class AutomationProviderDownloadItemObserver : public DownloadItem::Observer { DISALLOW_COPY_AND_ASSIGN(AutomationProviderDownloadItemObserver); }; +// Allows the automation provider to wait for history queries to finish. +class AutomationProviderHistoryObserver { + public: + AutomationProviderHistoryObserver( + AutomationProvider* provider, + IPC::Message* reply_message) { + provider_ = provider; + reply_message_ = reply_message; + } + ~AutomationProviderHistoryObserver() {} + void HistoryQueryComplete(HistoryService::Handle request_handle, + history::QueryResults* results); + + private: + AutomationProvider* provider_; + IPC::Message* reply_message_; +}; + #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_OBSERVERS_H_ diff --git a/chrome/test/functional/history.py b/chrome/test/functional/history.py new file mode 100644 index 0000000..2fab6b5 --- /dev/null +++ b/chrome/test/functional/history.py @@ -0,0 +1,38 @@ +#!/usr/bin/python +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import pyauto_functional # Must be imported before pyauto +import pyauto + + +class HistoryTest(pyauto.PyUITest): + """TestCase for History.""" + + def testBasic(self): + url = 'http://www.google.com/' + title = 'Google' + self.NavigateToURL(url) + + history = self.GetHistoryInfo().History() + self.assertEqual(1, len(history)) + self.assertEqual(title, history[0]['title']) + self.assertEqual(url, history[0]['url']) + + def Debug(self): + """Test method for experimentation. + + This method will not run automatically. + """ + while True: + raw_input('Interact with the browser and hit <enter> to dump history.. ') + print '*' * 20 + history = self.GetHistoryInfo().History() + import pprint + pp = pprint.PrettyPrinter(indent=2) + pp.pprint(history) + + +if __name__ == '__main__': + pyauto_functional.Main() diff --git a/chrome/test/pyautolib/history_info.py b/chrome/test/pyautolib/history_info.py new file mode 100644 index 0000000..ec7a732 --- /dev/null +++ b/chrome/test/pyautolib/history_info.py @@ -0,0 +1,80 @@ +#!/usr/bin/python + +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""History: python representation for history. + +Obtain one of these from PyUITestSuite::GetHistoryInfo() call. + +Example: +class MyTest(pyauto.PyUITest): + def testBasic(self): + url = 'http://www.google.com/' + self.NavigateToURL(url) + history = self.GetHistoryInfo() + self.assertEqual(1, len(history)) + self.assertEqual(url, history[0]['url']) + +See more tests in chrome/test/functional/history.py. +""" + +import simplejson as json + +from pyauto_errors import JSONInterfaceError + + +class HistoryInfo(object): + """Represent info about browsing history. + + The info is represented as a list of history items containing url, title, + time, etc. + """ + def __init__(self, json_string): + """Initialize a HistoryInfo from a string of json. + + Args: + json_string: a string of JSON, as returned by a json ipc call for the + command 'GetHistoryInfo' + A typical json string representing history info looks like: + {'history': [ + {'url': 'http://www.google.com/', + 'title': 'Google', + ..., + ..., + }, ] } + + Raises: + pyauto_errors.JSONInterfaceError if the automation call returns an error. + """ + # JSON string prepared in GetHistoryInfo() in automation_provider.cc + self.historydict = json.loads(json_string) + if self.historydict.has_key('error'): + raise JSONInterfaceError(self.historydict['error']) + + def History(self): + """Get history list. + + History is ordered latest first, that is in the same order as + chrome://history/ would list. + + Example: + [ { u'snippet': u'', + u'time': 1271465667, + u'title': u'Google News', + u'url': u'http://news.google.com/'}, + { u'snippet': u'', + u'time': 1271465667, + u'title': u'Google', + u'url': u'http://www.google.com/'}]} + + The snippet attribute will be empty in most cases. If GetHistoryInfo() is + provided a non-empty search_text arg, the snippet attribute will contain the + snippet as it would be visible when searching for that text in the + chrome://history/ UI. + + Returns: + [item1, item2, ...] + """ + return self.historydict.get('history', []) diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index cae280d..cf894bf 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -72,6 +72,7 @@ except ImportError: # Should go after sys.path is set appropriately import bookmark_model import download_info +import history_info import simplejson as json # found in third_party @@ -208,6 +209,28 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): self._SendJSONRequest(0, json.dumps({'command': 'WaitForAllDownloadsToComplete'})) + def GetHistoryInfo(self, search_text=''): + """Return info about browsing history. + + Args: + search_text: the string to search in history. Defaults to empty string + which means that all history would be returned. This is + functionally equivalent to searching for a text in the + chrome://history UI. So partial matches work too. + When non-empty, the history items returned will contain a + "snippet" field corresponding to the snippet visible in + the chrome://history/ UI. + + Returns: + an instance of history_info.HistoryInfo + """ + cmd_dict = { # Prepare command for the json interface + 'command': 'GetHistoryInfo', + 'search_text': search_text, + } + return history_info.HistoryInfo( + self._SendJSONRequest(0, json.dumps(cmd_dict))) + class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): """Base TestSuite for PyAuto UI tests.""" |