diff options
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 55 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 4 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider_observers.cc | 4 | ||||
-rw-r--r-- | chrome/test/functional/history.py | 26 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 26 |
5 files changed, 113 insertions, 2 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index 76b9b9b..d528169 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -1616,6 +1616,60 @@ void AutomationProvider::GetHistoryInfo( &AutomationProviderHistoryObserver::HistoryQueryComplete)); } +// Sample json input: { "command": "AddHistoryItem", +// "item": { "URL": "http://www.google.com", +// "title": "Google", # optional +// "time": 12345 # optional (time_t) +// } } +// Refer chrome/test/pyautolib/pyauto.py for details on input. +void AutomationProvider::AddHistoryItem( + DictionaryValue* args, + IPC::Message* reply_message) { + bool reply_return = true; + std::string json_return = "{}"; + + DictionaryValue* item = NULL; + args->GetDictionary(L"item", &item); + string16 url_text; + std::wstring title; + base::Time time = base::Time::Now(); + + if (item->GetString("url", &url_text)) { + GURL gurl(url_text); + item->GetString(L"title", &title); // Don't care if it fails. + int it; + double dt; + if (item->GetInteger(L"time", &it)) + time = base::Time::FromTimeT(it); + else if (item->GetReal(L"time", &dt)) + time = base::Time::FromDoubleT(dt); + + // Ideas for "dummy" values (e.g. id_scope) came from + // chrome/browser/autocomplete/history_contents_provider_unittest.cc + HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); + const void* id_scope = reinterpret_cast<void*>(1); + hs->AddPage(gurl, time, + id_scope, + 0, + GURL(), + PageTransition::LINK, + history::RedirectList(), + false); + if (title.length()) { + // TODO(jrg): add a string16 interface for + // HistoryService::SetPageTitle(), then use it. + hs->SetPageTitle(gurl, title); + } + } else { + json_return = "{\"error\": \"bad args (no URL in dict?).\"}"; + reply_return = false; + } + + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, reply_return); + Send(reply_message); +} + // Sample json input: { "command": "GetDownloadsInfo" } // Refer chrome/test/pyautolib/download_info.py for sample json output. void AutomationProvider::GetDownloadsInfo( @@ -1915,6 +1969,7 @@ void AutomationProvider::SendJSONRequest( handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo; handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo; + handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem; handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo; handler_map["SetPrefs"] = &AutomationProvider::SetPrefs; diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index 83b52e6..5662835 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -349,6 +349,10 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, // Uses the JSON interface for input/output. void GetHistoryInfo(DictionaryValue* args, IPC::Message* reply_message); + // Add an item to the history service. + // Uses the JSON interface for input/output. + void AddHistoryItem(DictionaryValue* args, IPC::Message* reply_message); + // Get info about preferences. // Uses the JSON interface for input/output. void GetPrefsInfo(DictionaryValue* args, IPC::Message* reply_message); diff --git a/chrome/browser/automation/automation_provider_observers.cc b/chrome/browser/automation/automation_provider_observers.cc index eb8e247..3ca82e6 100644 --- a/chrome/browser/automation/automation_provider_observers.cc +++ b/chrome/browser/automation/automation_provider_observers.cc @@ -952,8 +952,8 @@ void AutomationProviderHistoryObserver::HistoryQueryComplete( 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->SetReal(L"time", + static_cast<double>(page.visit_time().ToDoubleT())); page_value->SetString(L"snippet", page.snippet().text()); page_value->SetBoolean( L"starred", diff --git a/chrome/test/functional/history.py b/chrome/test/functional/history.py index 992e2e5..f149ed8 100644 --- a/chrome/test/functional/history.py +++ b/chrome/test/functional/history.py @@ -4,6 +4,7 @@ # found in the LICENSE file. import os +import time import pyauto_functional # Must be imported before pyauto import pyauto @@ -170,6 +171,31 @@ class HistoryTest(pyauto.PyUITest): self.assertEqual(2, len(history)) self.assertEqual(landing_url, history[0]['url']) + def testForge(self): + """Brief test of forging history items. + + Note the history system can tweak values (e.g. lower-case a URL or + append an '/' on it) so be careful with exact comparison. + """ + assert not self.GetHistoryInfo().History(), 'Expecting clean history.' + # Minimal interface + self.AddHistoryItem({'url': 'http://ZOINKS'}) + history = self.GetHistoryInfo().History() + self.assertEqual(1, len(history)) + self.assertTrue('zoinks' in history[0]['url']) # yes it gets lower-cased. + # Full interface (specify both title and url) + now = time.time() + self.AddHistoryItem({'title': 'Google', + 'url': 'http://www.google.com', + 'time': now}) + # Expect a second item + history = self.GetHistoryInfo().History() + self.assertEqual(2, len(history)) + # And make sure our forged item is there. + self.assertEqual('Google', history[0]['title']) + self.assertTrue('google.com' in history[0]['url']) + self.assertTrue(abs(now - history[0]['time']) < 1.0) + if __name__ == '__main__': pyauto_functional.Main() diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 6438c5f..700d156 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -330,6 +330,32 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): return history_info.HistoryInfo( self._SendJSONRequest(0, json.dumps(cmd_dict))) + def AddHistoryItem(self, item): + """Forge a history item for Chrome. + + Args: + item: a python dictionary representing the history item. Example: + { + # URL is the only mandatory item. + 'url': 'http://news.google.com', + # Title is optional. + 'title': 'Google News', + # Time is optional; if not set, assume "now". Time is in + # seconds since the Epoch. The python construct to get "Now" + # in the right scale is "time.time()". Can be float or int. + 'time': 1271781612 + } + """ + cmd_dict = { # Prepare command for the json interface + 'command': 'AddHistoryItem', + 'item': item + } + if not 'url' in item: + raise JSONInterfaceError('must specify url') + ret_dict = json.loads(self._SendJSONRequest(0, json.dumps(cmd_dict))) + if ret_dict.has_key('error'): + raise JSONInterfaceError(ret_dict['error']) + def GetPluginsInfo(self): """Return info about plugins. |