| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/file_util.h" | |
| 6 #include "base/scoped_temp_dir.h" | |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "base/values.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/intents/web_intents_registry.h" | |
| 11 #include "chrome/browser/webdata/web_data_service.h" | |
| 12 #include "chrome/browser/ui/intents/intents_model.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "ui/base/models/tree_node_model.h" | |
| 15 | |
| 16 class IntentsModelTest : public testing::Test { | |
| 17 public: | |
| 18 IntentsModelTest() | |
| 19 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 20 db_thread_(BrowserThread::DB) {} | |
| 21 | |
| 22 protected: | |
| 23 virtual void SetUp() { | |
| 24 db_thread_.Start(); | |
| 25 wds_ = new WebDataService(); | |
| 26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 27 wds_->Init(temp_dir_.path()); | |
| 28 | |
| 29 registry_.Initialize(wds_); | |
| 30 } | |
| 31 | |
| 32 virtual void TearDown() { | |
| 33 if (wds_.get()) | |
| 34 wds_->Shutdown(); | |
| 35 | |
| 36 db_thread_.Stop(); | |
| 37 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); | |
| 38 MessageLoop::current()->Run(); | |
| 39 } | |
| 40 | |
| 41 void LoadRegistry() { | |
| 42 { | |
| 43 WebIntentData provider; | |
| 44 provider.service_url = GURL("http://www.google.com/share"); | |
| 45 provider.action = ASCIIToUTF16("SHARE"); | |
| 46 provider.type = ASCIIToUTF16("text/url"); | |
| 47 provider.title = ASCIIToUTF16("Google"); | |
| 48 registry_.RegisterIntentProvider(provider); | |
| 49 } | |
| 50 { | |
| 51 WebIntentData provider; | |
| 52 provider.service_url = GURL("http://picasaweb.google.com/share"); | |
| 53 provider.action = ASCIIToUTF16("EDIT"); | |
| 54 provider.type = ASCIIToUTF16("image/*"); | |
| 55 provider.title = ASCIIToUTF16("Picasa"); | |
| 56 registry_.RegisterIntentProvider(provider); | |
| 57 } | |
| 58 { | |
| 59 WebIntentData provider; | |
| 60 provider.service_url = GURL("http://www.digg.com/share"); | |
| 61 provider.action = ASCIIToUTF16("SHARE"); | |
| 62 provider.type = ASCIIToUTF16("text/url"); | |
| 63 provider.title = ASCIIToUTF16("Digg"); | |
| 64 registry_.RegisterIntentProvider(provider); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 MessageLoopForUI message_loop_; | |
| 69 BrowserThread ui_thread_; | |
| 70 BrowserThread db_thread_; | |
| 71 scoped_refptr<WebDataService> wds_; | |
| 72 WebIntentsRegistry registry_; | |
| 73 ScopedTempDir temp_dir_; | |
| 74 }; | |
| 75 | |
| 76 class WaitingIntentsObserver : public IntentsModel::Observer { | |
| 77 public: | |
| 78 WaitingIntentsObserver() : event_(true, false), added_(0) {} | |
| 79 | |
| 80 virtual void TreeModelBeginBatch(IntentsModel* model) {} | |
| 81 | |
| 82 virtual void TreeModelEndBatch(IntentsModel* model) { | |
| 83 event_.Signal(); | |
| 84 MessageLoop::current()->Quit(); | |
| 85 } | |
| 86 | |
| 87 virtual void TreeNodesAdded(ui::TreeModel* model, | |
| 88 ui::TreeModelNode* parent, | |
| 89 int start, | |
| 90 int count) { | |
| 91 added_++; | |
| 92 } | |
| 93 | |
| 94 virtual void TreeNodesRemoved(ui::TreeModel* model, | |
| 95 ui::TreeModelNode* node, | |
| 96 int start, | |
| 97 int count) { | |
| 98 } | |
| 99 | |
| 100 virtual void TreeNodeChanged(ui::TreeModel* model, ui::TreeModelNode* node) { | |
| 101 } | |
| 102 | |
| 103 void Wait() { | |
| 104 MessageLoop::current()->Run(); | |
| 105 event_.Wait(); | |
| 106 LOG(INFO) << "DONE!"; | |
| 107 } | |
| 108 | |
| 109 base::WaitableEvent event_; | |
| 110 int added_; | |
| 111 }; | |
| 112 | |
| 113 TEST_F(IntentsModelTest, NodeIDs) { | |
| 114 LoadRegistry(); | |
| 115 WaitingIntentsObserver obs; | |
| 116 IntentsModel intents_model(®istry_); | |
| 117 intents_model.AddIntentsTreeObserver(&obs); | |
| 118 obs.Wait(); | |
| 119 | |
| 120 IntentsTreeNode* n1 = new IntentsTreeNode(ASCIIToUTF16("origin")); | |
| 121 intents_model.Add(intents_model.GetRoot(), n1, | |
| 122 intents_model.GetRoot()->child_count()); | |
| 123 EXPECT_EQ(ASCIIToUTF16("origin"), intents_model.GetTreeNodeId(n1)); | |
| 124 | |
| 125 IntentsTreeNode* ncheck = intents_model.GetTreeNode("origin"); | |
| 126 EXPECT_EQ(ncheck, n1); | |
| 127 | |
| 128 base::ListValue nodes; | |
| 129 intents_model.GetChildNodeList( | |
| 130 intents_model.GetTreeNode("www.google.com"), 0, 1, &nodes); | |
| 131 EXPECT_EQ(static_cast<size_t>(1), nodes.GetSize()); | |
| 132 base::DictionaryValue* dict; | |
| 133 EXPECT_TRUE(nodes.GetDictionary(0, &dict)); | |
| 134 | |
| 135 std::string val; | |
| 136 EXPECT_TRUE(dict->GetString("site", &val)); | |
| 137 EXPECT_EQ("www.google.com", val); | |
| 138 EXPECT_TRUE(dict->GetString("name", &val)); | |
| 139 EXPECT_EQ("Google", val); | |
| 140 EXPECT_TRUE(dict->GetString("url", &val)); | |
| 141 EXPECT_EQ("http://www.google.com/share", val); | |
| 142 EXPECT_TRUE(dict->GetString("icon", &val)); | |
| 143 EXPECT_EQ("http://www.google.com/favicon.ico", val); | |
| 144 base::ListValue* types_list; | |
| 145 EXPECT_TRUE(dict->GetList("types", &types_list)); | |
| 146 EXPECT_EQ(static_cast<size_t>(1), types_list->GetSize()); | |
| 147 EXPECT_TRUE(types_list->GetString(0, &val)); | |
| 148 EXPECT_EQ("text/url", val); | |
| 149 bool bval; | |
| 150 EXPECT_TRUE(dict->GetBoolean("blocked", &bval)); | |
| 151 EXPECT_FALSE(bval); | |
| 152 EXPECT_TRUE(dict->GetBoolean("disabled", &bval)); | |
| 153 EXPECT_FALSE(bval); | |
| 154 } | |
| 155 | |
| 156 TEST_F(IntentsModelTest, LoadFromWebData) { | |
| 157 LoadRegistry(); | |
| 158 WaitingIntentsObserver obs; | |
| 159 IntentsModel intents_model(®istry_); | |
| 160 intents_model.AddIntentsTreeObserver(&obs); | |
| 161 obs.Wait(); | |
| 162 EXPECT_EQ(3, obs.added_); | |
| 163 | |
| 164 IntentsTreeNode* node = intents_model.GetTreeNode("www.google.com"); | |
| 165 ASSERT_NE(static_cast<IntentsTreeNode*>(NULL), node); | |
| 166 EXPECT_EQ(IntentsTreeNode::TYPE_ORIGIN, node->Type()); | |
| 167 EXPECT_EQ(ASCIIToUTF16("www.google.com"), node->GetTitle()); | |
| 168 EXPECT_EQ(1, node->child_count()); | |
| 169 node = node->GetChild(0); | |
| 170 ASSERT_EQ(IntentsTreeNode::TYPE_SERVICE, node->Type()); | |
| 171 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(node); | |
| 172 EXPECT_EQ(ASCIIToUTF16("Google"), snode->ServiceName()); | |
| 173 EXPECT_EQ(ASCIIToUTF16("SHARE"), snode->Action()); | |
| 174 EXPECT_EQ(ASCIIToUTF16("http://www.google.com/share"), snode->ServiceUrl()); | |
| 175 EXPECT_EQ(static_cast<size_t>(1), snode->Types().GetSize()); | |
| 176 string16 stype; | |
| 177 ASSERT_TRUE(snode->Types().GetString(0, &stype)); | |
| 178 EXPECT_EQ(ASCIIToUTF16("text/url"), stype); | |
| 179 | |
| 180 node = intents_model.GetTreeNode("www.digg.com"); | |
| 181 ASSERT_NE(static_cast<IntentsTreeNode*>(NULL), node); | |
| 182 EXPECT_EQ(IntentsTreeNode::TYPE_ORIGIN, node->Type()); | |
| 183 EXPECT_EQ(ASCIIToUTF16("www.digg.com"), node->GetTitle()); | |
| 184 } | |
| OLD | NEW |