summaryrefslogtreecommitdiffstats
path: root/chrome/browser/intents/web_intents_registry_unittest.cc
blob: d8aa4cb1b25abad5fdaf24ea3aa4d74c5e40a58d (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
// 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/file_util.h"
#include "base/scoped_temp_dir.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/intents/web_intents_registry.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "testing/gtest/include/gtest/gtest.h"

class WebIntentsRegistryTest : public testing::Test {
 public:
   WebIntentsRegistryTest()
     : ui_thread_(BrowserThread::UI, &message_loop_),
       db_thread_(BrowserThread::DB) {}

 protected:
  virtual void SetUp() {
    db_thread_.Start();
    wds_ = new WebDataService();
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    wds_->Init(temp_dir_.path());
    registry_.Initialize(wds_);
  }

  virtual void TearDown() {
    if (wds_.get())
      wds_->Shutdown();

    db_thread_.Stop();
    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
    MessageLoop::current()->Run();
  }

  MessageLoopForUI message_loop_;
  BrowserThread ui_thread_;
  BrowserThread db_thread_;
  scoped_refptr<WebDataService> wds_;
  WebIntentsRegistry registry_;
  ScopedTempDir temp_dir_;
};

// Simple consumer for WebIntentsRegistry notifications. Stores result data and
// terminates UI thread when callback is invoked.
class TestConsumer: public WebIntentsRegistry::Consumer {
 public:
   virtual void OnIntentsQueryDone(WebIntentsRegistry::QueryID id,
                                   const std::vector<WebIntentData>& intents) {
     DCHECK(id == expected_id_);
     intents_ = intents;

     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     MessageLoop::current()->Quit();
   }

   // Wait for the UI message loop to terminate - happens when OnIntesQueryDone
   // is invoked.
   void WaitForData() {
     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     MessageLoop::current()->Run();
   }

   WebIntentsRegistry::QueryID expected_id_;  // QueryID callback is tied to.
   std::vector<WebIntentData> intents_;  // Result data from callback.
};

TEST_F(WebIntentsRegistryTest, BasicTests) {
  WebIntentData intent;
  intent.service_url = GURL("http://google.com");
  intent.action = ASCIIToUTF16("share");
  intent.type = ASCIIToUTF16("image/*");

  registry_.RegisterIntentProvider(intent);

  intent.type = ASCIIToUTF16("video/*");
  registry_.RegisterIntentProvider(intent);

  intent.action = ASCIIToUTF16("search");
  registry_.RegisterIntentProvider(intent);

  TestConsumer consumer;
  consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"),
                                                       &consumer);
  consumer.WaitForData();
  EXPECT_EQ(2U, consumer.intents_.size());

  consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("search"),
                                                       &consumer);
  consumer.WaitForData();
  EXPECT_EQ(1U, consumer.intents_.size());

  intent.action = ASCIIToUTF16("share");
  intent.type = ASCIIToUTF16("image/*");
  registry_.UnregisterIntentProvider(intent);

  consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"),
                                                       &consumer);
  consumer.WaitForData();
  EXPECT_EQ(1U, consumer.intents_.size());
}