1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string>
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kExtensionId[] = "extensionid";
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kApiCall[] = "api.call";
173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)const char kArgs[] = "[\"hello\",\"world\"]";
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // extensions
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace extensions {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using api::activity_log_private::ExtensionActivity;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)typedef testing::Test ActivityLogApiUnitTest;
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(ActivityLogApiUnitTest, ConvertChromeApiAction) {
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<base::ListValue> args(new base::ListValue());
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  args->Set(0, new base::StringValue("hello"));
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  args->Set(1, new base::StringValue("world"));
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action(new Action(kExtensionId,
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                          base::Time::Now(),
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                          Action::ACTION_API_CALL,
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                          kApiCall));
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_args(args.Pass());
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<ExtensionActivity> result = action->ConvertToExtensionActivity();
373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ(ExtensionActivity::ACTIVITY_TYPE_API_CALL, result->activity_type);
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(kExtensionId, *(result->extension_id.get()));
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ(kApiCall, *(result->api_call.get()));
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ(kArgs, *(result->args.get()));
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(NULL, result->activity_id.get());
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(ActivityLogApiUnitTest, ConvertDomAction) {
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<base::ListValue> args(new base::ListValue());
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  args->Set(0, new base::StringValue("hello"));
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  args->Set(1, new base::StringValue("world"));
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action(new Action(kExtensionId,
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                               base::Time::Now(),
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                               Action::ACTION_DOM_ACCESS,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               kApiCall,
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               12345));
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_args(args.Pass());
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_page_title("Title");
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                      DomActionType::INSERTED);
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_other()->SetBoolean(activity_log_constants::kActionPrerender,
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                      false);
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<ExtensionActivity> result = action->ConvertToExtensionActivity();
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(kExtensionId, *(result->extension_id.get()));
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ("http://www.google.com/", *(result->page_url.get()));
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ("Title", *(result->page_title.get()));
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ(kApiCall, *(result->api_call.get()));
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ASSERT_EQ(kArgs, *(result->args.get()));
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_ptr<ExtensionActivity::Other> other(result->other.Pass());
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ASSERT_EQ(ExtensionActivity::Other::DOM_VERB_INSERTED, other->dom_verb);
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ASSERT_TRUE(other->prerender.get());
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ("12345", *(result->activity_id.get()));
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
72558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}  // namespace extensions
73