summaryrefslogtreecommitdiff
path: root/chromium/components/arc/intent_helper/arc_intent_helper_bridge_unittest.cc
blob: dde660f0f8d042d1418e93109e0f4c487e587c05 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2016 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 "components/arc/intent_helper/arc_intent_helper_bridge.h"

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "components/arc/arc_bridge_service.h"
#include "components/arc/common/intent_helper.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {

namespace {

IntentFilter GetIntentFilter(const std::string& host) {
  std::vector<IntentFilter::AuthorityEntry> authorities;
  authorities.emplace_back(host, -1);
  return IntentFilter(std::move(authorities),
                      std::vector<IntentFilter::PatternMatcher>());
}

}  // namespace

class ArcIntentHelperTest : public testing::Test {
 protected:
  ArcIntentHelperTest() = default;

  class TestOpenUrlDelegate : public ArcIntentHelperBridge::OpenUrlDelegate {
   public:
    ~TestOpenUrlDelegate() override = default;

    // ArcIntentHelperBridge::OpenUrlDelegate:
    void OpenUrl(const GURL& url) override { last_opened_url_ = url; }

    GURL TakeLastOpenedUrl() {
      GURL result = std::move(last_opened_url_);
      last_opened_url_ = GURL();
      return result;
    }

   private:
    GURL last_opened_url_;
  };

  std::unique_ptr<ArcBridgeService> arc_bridge_service_;
  TestOpenUrlDelegate* test_open_url_delegate_;  // owned by |instance_|
  std::unique_ptr<ArcIntentHelperBridge> instance_;

 private:
  void SetUp() override {
    arc_bridge_service_ = std::make_unique<ArcBridgeService>();
    instance_ = std::make_unique<ArcIntentHelperBridge>(
        nullptr /* context */, arc_bridge_service_.get());
    test_open_url_delegate_ = new TestOpenUrlDelegate();
    instance_->SetOpenUrlDelegateForTesting(
        base::WrapUnique(test_open_url_delegate_));
  }

  void TearDown() override {
    instance_.reset();
    arc_bridge_service_.reset();
  }

  DISALLOW_COPY_AND_ASSIGN(ArcIntentHelperTest);
};

// Tests if IsIntentHelperPackage works as expected. Probably too trivial
// to test but just in case.
TEST_F(ArcIntentHelperTest, TestIsIntentHelperPackage) {
  EXPECT_FALSE(ArcIntentHelperBridge::IsIntentHelperPackage(""));
  EXPECT_FALSE(ArcIntentHelperBridge::IsIntentHelperPackage(
      ArcIntentHelperBridge::kArcIntentHelperPackageName + std::string("a")));
  EXPECT_FALSE(ArcIntentHelperBridge::IsIntentHelperPackage(
      ArcIntentHelperBridge::kArcIntentHelperPackageName +
      std::string("/.ArcIntentHelperActivity")));
  EXPECT_TRUE(ArcIntentHelperBridge::IsIntentHelperPackage(
      ArcIntentHelperBridge::kArcIntentHelperPackageName));
}

// Tests if FilterOutIntentHelper removes handlers as expected.
TEST_F(ArcIntentHelperTest, TestFilterOutIntentHelper) {
  {
    std::vector<mojom::IntentHandlerInfoPtr> orig;
    std::vector<mojom::IntentHandlerInfoPtr> filtered =
        ArcIntentHelperBridge::FilterOutIntentHelper(std::move(orig));
    EXPECT_EQ(0U, filtered.size());
  }

  {
    std::vector<mojom::IntentHandlerInfoPtr> orig;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[0]->name = "0";
    orig[0]->package_name = "package_name0";
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[1]->name = "1";
    orig[1]->package_name = "package_name1";

    // FilterOutIntentHelper is no-op in this case.
    std::vector<mojom::IntentHandlerInfoPtr> filtered =
        ArcIntentHelperBridge::FilterOutIntentHelper(std::move(orig));
    EXPECT_EQ(2U, filtered.size());
  }

  {
    std::vector<mojom::IntentHandlerInfoPtr> orig;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[0]->name = "0";
    orig[0]->package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[1]->name = "1";
    orig[1]->package_name = "package_name1";

    // FilterOutIntentHelper should remove the first element.
    std::vector<mojom::IntentHandlerInfoPtr> filtered =
        ArcIntentHelperBridge::FilterOutIntentHelper(std::move(orig));
    ASSERT_EQ(1U, filtered.size());
    EXPECT_EQ("1", filtered[0]->name);
    EXPECT_EQ("package_name1", filtered[0]->package_name);
  }

  {
    std::vector<mojom::IntentHandlerInfoPtr> orig;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[0]->name = "0";
    orig[0]->package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[1]->name = "1";
    orig[1]->package_name = "package_name1";
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[2]->name = "2";
    orig[2]->package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;

    // FilterOutIntentHelper should remove two elements.
    std::vector<mojom::IntentHandlerInfoPtr> filtered =
        ArcIntentHelperBridge::FilterOutIntentHelper(std::move(orig));
    ASSERT_EQ(1U, filtered.size());
    EXPECT_EQ("1", filtered[0]->name);
    EXPECT_EQ("package_name1", filtered[0]->package_name);
  }

  {
    std::vector<mojom::IntentHandlerInfoPtr> orig;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[0]->name = "0";
    orig[0]->package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;
    orig.push_back(mojom::IntentHandlerInfo::New());
    orig[1]->name = "1";
    orig[1]->package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;

    // FilterOutIntentHelper should remove all elements.
    std::vector<mojom::IntentHandlerInfoPtr> filtered =
        ArcIntentHelperBridge::FilterOutIntentHelper(std::move(orig));
    EXPECT_EQ(0U, filtered.size());
  }
}

// Tests if observer works as expected.
TEST_F(ArcIntentHelperTest, TestObserver) {
  class FakeObserver : public ArcIntentHelperObserver {
   public:
    FakeObserver() = default;
    void OnIntentFiltersUpdated() override { updated_ = true; }
    bool IsUpdated() { return updated_; }
    void Reset() { updated_ = false; }

   private:
    bool updated_ = false;
  };

  // Observer should be called when intent filter is updated.
  auto observer = std::make_unique<FakeObserver>();
  instance_->AddObserver(observer.get());
  EXPECT_FALSE(observer->IsUpdated());
  instance_->OnIntentFiltersUpdated(std::vector<IntentFilter>());
  EXPECT_TRUE(observer->IsUpdated());

  // Observer should not be called after it's removed.
  observer->Reset();
  instance_->RemoveObserver(observer.get());
  instance_->OnIntentFiltersUpdated(std::vector<IntentFilter>());
  EXPECT_FALSE(observer->IsUpdated());
}

// Tests that ShouldChromeHandleUrl returns true by default.
TEST_F(ArcIntentHelperTest, TestDefault) {
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("http://www.google.com")));
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("https://www.google.com")));
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("file:///etc/password")));
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("chrome://help")));
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("about://chrome")));
}

// Tests that ShouldChromeHandleUrl returns false when there's a match.
TEST_F(ArcIntentHelperTest, TestSingleFilter) {
  std::vector<IntentFilter> array;
  array.emplace_back(GetIntentFilter("www.google.com"));
  instance_->OnIntentFiltersUpdated(std::move(array));

  EXPECT_FALSE(instance_->ShouldChromeHandleUrl(GURL("http://www.google.com")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.google.com")));

  EXPECT_TRUE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.google.co.uk")));
}

// Tests the same with multiple filters.
TEST_F(ArcIntentHelperTest, TestMultipleFilters) {
  std::vector<IntentFilter> array;
  array.emplace_back(GetIntentFilter("www.google.com"));
  array.emplace_back(GetIntentFilter("www.google.co.uk"));
  array.emplace_back(GetIntentFilter("dev.chromium.org"));
  instance_->OnIntentFiltersUpdated(std::move(array));

  EXPECT_FALSE(instance_->ShouldChromeHandleUrl(GURL("http://www.google.com")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.google.com")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("http://www.google.co.uk")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.google.co.uk")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("http://dev.chromium.org")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://dev.chromium.org")));

  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("http://www.android.com")));
}

// Tests that ShouldChromeHandleUrl returns true for non http(s) URLs.
TEST_F(ArcIntentHelperTest, TestNonHttp) {
  std::vector<IntentFilter> array;
  array.emplace_back(GetIntentFilter("www.google.com"));
  instance_->OnIntentFiltersUpdated(std::move(array));

  EXPECT_TRUE(
      instance_->ShouldChromeHandleUrl(GURL("chrome://www.google.com")));
  EXPECT_TRUE(
      instance_->ShouldChromeHandleUrl(GURL("custom://www.google.com")));
}

// Tests that ShouldChromeHandleUrl discards the previous filters when
// UpdateIntentFilters is called with new ones.
TEST_F(ArcIntentHelperTest, TestMultipleUpdate) {
  std::vector<IntentFilter> array;
  array.emplace_back(GetIntentFilter("www.google.com"));
  array.emplace_back(GetIntentFilter("dev.chromium.org"));
  instance_->OnIntentFiltersUpdated(std::move(array));

  std::vector<IntentFilter> array2;
  array2.emplace_back(GetIntentFilter("www.google.co.uk"));
  array2.emplace_back(GetIntentFilter("dev.chromium.org"));
  array2.emplace_back(GetIntentFilter("www.android.com"));
  instance_->OnIntentFiltersUpdated(std::move(array2));

  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("http://www.google.com")));
  EXPECT_TRUE(instance_->ShouldChromeHandleUrl(GURL("https://www.google.com")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("http://www.google.co.uk")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.google.co.uk")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("http://dev.chromium.org")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://dev.chromium.org")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("http://www.android.com")));
  EXPECT_FALSE(
      instance_->ShouldChromeHandleUrl(GURL("https://www.android.com")));
}

// Tests that OnOpenUrl opens the URL in Chrome browser.
TEST_F(ArcIntentHelperTest, TestOnOpenUrl) {
  instance_->OnOpenUrl("http://google.com");
  EXPECT_EQ(GURL("http://google.com"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenUrl("https://google.com");
  EXPECT_EQ(GURL("https://google.com"),
            test_open_url_delegate_->TakeLastOpenedUrl());
}

// Tests that OnOpenUrl does not open URLs with the 'chrome://' and equivalent
// schemes like 'about:'.
TEST_F(ArcIntentHelperTest, TestOnOpenUrl_ChromeScheme) {
  instance_->OnOpenUrl("chrome://www.google.com");
  EXPECT_FALSE(test_open_url_delegate_->TakeLastOpenedUrl().is_valid());

  instance_->OnOpenUrl("chrome://settings");
  EXPECT_FALSE(test_open_url_delegate_->TakeLastOpenedUrl().is_valid());

  instance_->OnOpenUrl("about:");
  EXPECT_FALSE(test_open_url_delegate_->TakeLastOpenedUrl().is_valid());

  instance_->OnOpenUrl("about:settings");
  EXPECT_FALSE(test_open_url_delegate_->TakeLastOpenedUrl().is_valid());

  instance_->OnOpenUrl("about:blank");
  EXPECT_FALSE(test_open_url_delegate_->TakeLastOpenedUrl().is_valid());
}

// Tests that OnOpenChromePage opens the specified settings section in the
// Chrome browser.
TEST_F(ArcIntentHelperTest, TestOnOpenChromePage) {
  instance_->OnOpenChromePage(mojom::ChromePage::MAIN);
  EXPECT_EQ(GURL("chrome://settings"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::MULTIDEVICE);
  EXPECT_EQ(GURL("chrome://settings/multidevice"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::WIFI);
  EXPECT_EQ(GURL("chrome://settings/networks/?type=WiFi"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::POWER);
  EXPECT_EQ(GURL("chrome://settings/power"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::BLUETOOTH);
  EXPECT_EQ(GURL("chrome://settings/bluetoothDevices"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::DATETIME);
  EXPECT_EQ(GURL("chrome://settings/dateTime"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::DISPLAY);
  EXPECT_EQ(GURL("chrome://settings/display"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::PRIVACY);
  EXPECT_EQ(GURL("chrome://settings/privacy"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::HELP);
  EXPECT_EQ(GURL("chrome://settings/help"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::ACCOUNTS);
  EXPECT_EQ(GURL("chrome://settings/accounts"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::APPEARANCE);
  EXPECT_EQ(GURL("chrome://settings/appearance"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::AUTOFILL);
  EXPECT_EQ(GURL("chrome://settings/autofill"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::BLUETOOTHDEVICES);
  EXPECT_EQ(GURL("chrome://settings/bluetoothDevices"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::CHANGEPICTURE);
  EXPECT_EQ(GURL("chrome://settings/changePicture"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::CLEARBROWSERDATA);
  EXPECT_EQ(GURL("chrome://settings/clearBrowserData"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::CLOUDPRINTERS);
  EXPECT_EQ(GURL("chrome://settings/cloudPrinters"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::CUPSPRINTERS);
  EXPECT_EQ(GURL("chrome://settings/cupsPrinters"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::DOWNLOADS);
  EXPECT_EQ(GURL("chrome://settings/downloads"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::ABOUTDOWNLOADS);
  EXPECT_EQ(GURL("about:downloads"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::ABOUTHISTORY);
  EXPECT_EQ(GURL("about:history"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::KEYBOARDOVERLAY);
  EXPECT_EQ(GURL("chrome://settings/keyboard-overlay"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::LANGUAGES);
  EXPECT_EQ(GURL("chrome://settings/languages"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::LOCKSCREEN);
  EXPECT_EQ(GURL("chrome://settings/lockScreen"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::MANAGEACCESSIBILITY);
  EXPECT_EQ(GURL("chrome://settings/manageAccessibility"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::NETWORKSTYPEVPN);
  EXPECT_EQ(GURL("chrome://settings/networks?type=VPN"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::ONSTARTUP);
  EXPECT_EQ(GURL("chrome://settings/onStartup"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::PASSWORDS);
  EXPECT_EQ(GURL("chrome://settings/passwords"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::POINTEROVERLAY);
  EXPECT_EQ(GURL("chrome://settings/pointer-overlay"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::RESET);
  EXPECT_EQ(GURL("chrome://settings/reset"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::SEARCH);
  EXPECT_EQ(GURL("chrome://settings/search"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::STORAGE);
  EXPECT_EQ(GURL("chrome://settings/storage"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::SYNCSETUP);
  EXPECT_EQ(GURL("chrome://settings/syncSetup"),
            test_open_url_delegate_->TakeLastOpenedUrl());

  instance_->OnOpenChromePage(mojom::ChromePage::ABOUTBLANK);
  EXPECT_EQ(GURL("about:blank"), test_open_url_delegate_->TakeLastOpenedUrl());
}

// Tests that AppendStringToIntentHelperPackageName works.
TEST_F(ArcIntentHelperTest, TestAppendStringToIntentHelperPackageName) {
  std::string package_name = ArcIntentHelperBridge::kArcIntentHelperPackageName;
  std::string fake_activity = "this_is_a_fake_activity";
  EXPECT_EQ(ArcIntentHelperBridge::AppendStringToIntentHelperPackageName(
                fake_activity),
            package_name + "." + fake_activity);

  std::string empty_string = "";
  EXPECT_EQ(ArcIntentHelperBridge::AppendStringToIntentHelperPackageName(
                empty_string),
            package_name + ".");
}

}  // namespace arc