summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/intents/web_intent_picker_controller_browsertest.cc
blob: 6702c1282941d230b7c8b96088e556adc38fce45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2011 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.

#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/favicon/favicon_service.h"
#include "chrome/browser/intents/web_intents_registry.h"
#include "chrome/browser/intents/web_intents_registry_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/intents/web_intent_picker.h"
#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
#include "chrome/browser/ui/intents/web_intent_picker_factory.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/web_intent_service_data.h"

using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SaveArg;

namespace {

const string16 kAction1(ASCIIToUTF16("http://www.example.com/share"));
const string16 kAction2(ASCIIToUTF16("http://www.example.com/foobar"));
const string16 kType(ASCIIToUTF16("image/png"));
const GURL kServiceURL1("http://www.google.com");
const GURL kServiceURL2("http://www.chromium.org");

MATCHER_P(VectorIsOfSize, n, "") {
  return arg.size() == static_cast<size_t>(n);
}

}  // namespace

class WebIntentPickerMock : public WebIntentPicker {
 public:
  MOCK_METHOD1(SetServiceURLs, void(const std::vector<GURL>& urls));
  MOCK_METHOD2(SetServiceIcon, void(size_t index, const SkBitmap& icon));
  MOCK_METHOD1(SetDefaultServiceIcon, void(size_t index));
  MOCK_METHOD0(Show, void(void));
  MOCK_METHOD0(Close, void(void));
};

class WebIntentPickerFactoryMock : public WebIntentPickerFactory {
 public:
  MOCK_METHOD3(Create,
               WebIntentPicker*(Browser* browser,
                                TabContentsWrapper* wrapper,
                                WebIntentPickerDelegate* delegate));
  MOCK_METHOD1(ClosePicker, void(WebIntentPicker* picker));
};

class WebIntentPickerControllerBrowserTest : public InProcessBrowserTest {
 protected:
  void AddWebIntentService(const string16& action,
                           const GURL& service_url) {
    WebIntentServiceData web_intent_service_data;
    web_intent_service_data.action = action;
    web_intent_service_data.type = kType;
    web_intent_service_data.service_url = service_url;
    web_data_service_->AddWebIntent(web_intent_service_data);
  }

  void SetPickerExpectations(int expected_service_count,
                             int expected_default_favicons) {
    EXPECT_CALL(*picker_factory_, Create(_, _, _)).
        WillOnce(DoAll(SaveArg<2>(&delegate_), Return(&picker_)));
    EXPECT_CALL(picker_,
                SetServiceURLs(VectorIsOfSize(expected_service_count))).
        Times(1);
    EXPECT_CALL(picker_, SetDefaultServiceIcon(_)).
        Times(expected_default_favicons);
    EXPECT_CALL(*picker_factory_, ClosePicker(_));
  }

  void CheckPendingAsync() {
    if (controller_->pending_async_count() > 0) {
      MessageLoop::current()->PostTask(
          FROM_HERE,
          base::Bind(&WebIntentPickerControllerBrowserTest::CheckPendingAsync,
                     base::Unretained(this)));
      return;
    }

    MessageLoop::current()->Quit();
  }

  void WaitForDialogToShow() {
    CheckPendingAsync();
    MessageLoop::current()->Run();
  }

  WebIntentPickerMock picker_;

  // |controller_| takes ownership.
  WebIntentPickerFactoryMock* picker_factory_;

  scoped_ptr<WebIntentPickerController> controller_;
  WebIntentPickerDelegate* delegate_;
  WebDataService* web_data_service_;
  FaviconService* favicon_service_;
};

IN_PROC_BROWSER_TEST_F(WebIntentPickerControllerBrowserTest, ChooseService) {
  web_data_service_ =
      browser()->profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
  AddWebIntentService(kAction1, kServiceURL1);
  AddWebIntentService(kAction1, kServiceURL2);

  favicon_service_ =
      browser()->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);

  picker_factory_ = new WebIntentPickerFactoryMock();
  controller_.reset(new WebIntentPickerController(
      browser()->GetSelectedTabContentsWrapper(), picker_factory_));

  SetPickerExpectations(2, 2);

  controller_->ShowDialog(NULL, kAction1, kType);
  WaitForDialogToShow();

  delegate_->OnServiceChosen(1);
  ASSERT_EQ(2, browser()->tab_count());
  EXPECT_EQ(GURL(kServiceURL2),
            browser()->GetSelectedTabContents()->GetURL());
}