| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/webdata/web_database.h" | 11 #include "chrome/browser/webdata/web_database.h" |
| 12 #include "chrome/browser/webdata/web_intents_table.h" | 12 #include "chrome/browser/webdata/web_intents_table.h" |
| 13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/glue/web_intent_service_data.h" | 16 #include "webkit/glue/web_intent_service_data.h" |
| 17 | 17 |
| 18 using webkit_glue::WebIntentServiceData; |
| 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 GURL test_url("http://google.com/"); | 22 GURL test_url("http://google.com/"); |
| 21 string16 test_action = ASCIIToUTF16("http://webintents.org/intents/share"); | 23 string16 test_action = ASCIIToUTF16("http://webintents.org/intents/share"); |
| 22 string16 test_action_2 = ASCIIToUTF16("http://webintents.org/intents/view"); | 24 string16 test_action_2 = ASCIIToUTF16("http://webintents.org/intents/view"); |
| 23 string16 test_title = ASCIIToUTF16("Test WebIntent"); | 25 string16 test_title = ASCIIToUTF16("Test WebIntent"); |
| 24 string16 test_title_2 = ASCIIToUTF16("Test WebIntent #2"); | 26 string16 test_title_2 = ASCIIToUTF16("Test WebIntent #2"); |
| 25 string16 mime_image = ASCIIToUTF16("image/*"); | 27 string16 mime_image = ASCIIToUTF16("image/*"); |
| 26 string16 mime_video = ASCIIToUTF16("video/*"); | 28 string16 mime_video = ASCIIToUTF16("video/*"); |
| 27 | 29 |
| 28 WebIntentServiceData MakeIntent(const GURL& url, const string16& action, | 30 WebIntentServiceData MakeIntentService(const GURL& url, |
| 29 const string16& type, const string16& title) { | 31 const string16& action, |
| 32 const string16& type, |
| 33 const string16& title) { |
| 30 WebIntentServiceData service; | 34 WebIntentServiceData service; |
| 31 service.service_url = url; | 35 service.service_url = url; |
| 32 service.action = action; | 36 service.action = action; |
| 33 service.type = type; | 37 service.type = type; |
| 34 service.title = title; | 38 service.title = title; |
| 35 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; | 39 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; |
| 36 return service; | 40 return service; |
| 37 } | 41 } |
| 38 | 42 |
| 39 class WebIntentsTableTest : public testing::Test { | 43 class WebIntentsTableTest : public testing::Test { |
| 40 protected: | 44 protected: |
| 41 virtual void SetUp() { | 45 virtual void SetUp() { |
| 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 43 ASSERT_EQ(sql::INIT_OK, | 47 ASSERT_EQ(sql::INIT_OK, |
| 44 db_.Init(temp_dir_.path().AppendASCII("TestWebDatabase.db"))); | 48 db_.Init(temp_dir_.path().AppendASCII("TestWebDatabase.db"))); |
| 45 } | 49 } |
| 46 | 50 |
| 47 WebIntentsTable* IntentsTable() { | 51 WebIntentsTable* IntentsTable() { |
| 48 return db_.GetWebIntentsTable(); | 52 return db_.GetWebIntentsTable(); |
| 49 } | 53 } |
| 50 | 54 |
| 51 WebDatabase db_; | 55 WebDatabase db_; |
| 52 ScopedTempDir temp_dir_; | 56 ScopedTempDir temp_dir_; |
| 53 }; | 57 }; |
| 54 | 58 |
| 55 // Test we can add, retrieve, and remove intents from the database. | 59 // Test we can add, retrieve, and remove intent services from the database. |
| 56 TEST_F(WebIntentsTableTest, SetGetDeleteIntent) { | 60 TEST_F(WebIntentsTableTest, SetGetDeleteIntent) { |
| 57 std::vector<WebIntentServiceData> intents; | 61 std::vector<WebIntentServiceData> services; |
| 58 | 62 |
| 59 // By default, no intents exist. | 63 // By default, no intent services exist. |
| 60 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); | 64 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &services)); |
| 61 EXPECT_EQ(0U, intents.size()); | 65 EXPECT_EQ(0U, services.size()); |
| 62 | 66 |
| 63 // Now adding one. | 67 // Now adding one. |
| 64 WebIntentServiceData intent = MakeIntent(test_url, test_action, mime_image, | 68 WebIntentServiceData service = |
| 65 test_title); | 69 MakeIntentService(test_url, test_action, mime_image, test_title); |
| 66 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 70 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 67 | 71 |
| 68 // Make sure that intent can now be fetched | 72 // Make sure that service can now be fetched |
| 69 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); | 73 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &services)); |
| 70 ASSERT_EQ(1U, intents.size()); | 74 ASSERT_EQ(1U, services.size()); |
| 71 EXPECT_EQ(intent, intents[0]); | 75 EXPECT_EQ(service, services[0]); |
| 72 | 76 |
| 73 // Remove the intent. | 77 // Remove the service. |
| 74 EXPECT_TRUE(IntentsTable()->RemoveWebIntent(intent)); | 78 EXPECT_TRUE(IntentsTable()->RemoveWebIntent(service)); |
| 75 | 79 |
| 76 // Intent should now be gone. | 80 // Should now be gone. |
| 77 intents.clear(); | 81 services.clear(); |
| 78 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); | 82 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &services)); |
| 79 EXPECT_EQ(0U, intents.size()); | 83 EXPECT_EQ(0U, services.size()); |
| 80 } | 84 } |
| 81 | 85 |
| 82 // Test we support multiple intents for the same MIME type | 86 // Test we support multiple intent services for the same MIME type |
| 83 TEST_F(WebIntentsTableTest, SetMultipleIntents) { | 87 TEST_F(WebIntentsTableTest, SetMultipleIntents) { |
| 84 std::vector<WebIntentServiceData> intents; | 88 std::vector<WebIntentServiceData> services; |
| 85 | 89 |
| 86 WebIntentServiceData intent = MakeIntent(test_url, test_action, mime_image, | 90 WebIntentServiceData service = |
| 87 test_title); | 91 MakeIntentService(test_url, test_action, mime_image, test_title); |
| 88 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 92 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 89 | 93 |
| 90 intent.type = mime_video; | 94 service.type = mime_video; |
| 91 intent.title = test_title_2; | 95 service.title = test_title_2; |
| 92 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 96 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 93 | 97 |
| 94 // Recover stored intents from DB. | 98 // Recover stored intent services from DB. |
| 95 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); | 99 EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &services)); |
| 96 ASSERT_EQ(2U, intents.size()); | 100 ASSERT_EQ(2U, services.size()); |
| 97 | 101 |
| 98 // WebIntentsTable does not guarantee order, so ensure order here. | 102 // WebIntentsTable does not guarantee order, so ensure order here. |
| 99 if (intents[0].type == mime_video) | 103 if (services[0].type == mime_video) |
| 100 std::swap(intents[0], intents[1]); | 104 std::swap(services[0], services[1]); |
| 101 | 105 |
| 102 EXPECT_EQ(intent, intents[1]); | 106 EXPECT_EQ(service, services[1]); |
| 103 | 107 |
| 104 intent.type = mime_image; | 108 service.type = mime_image; |
| 105 intent.title = test_title; | 109 service.title = test_title; |
| 106 EXPECT_EQ(intent, intents[0]); | 110 EXPECT_EQ(service, services[0]); |
| 107 } | 111 } |
| 108 | 112 |
| 109 // Test we support getting all intents independent of action. | 113 // Test we support getting all intent services independent of action. |
| 110 TEST_F(WebIntentsTableTest, GetAllIntents) { | 114 TEST_F(WebIntentsTableTest, GetAllIntents) { |
| 111 std::vector<WebIntentServiceData> intents; | 115 std::vector<WebIntentServiceData> services; |
| 112 | 116 |
| 113 WebIntentServiceData intent = MakeIntent(test_url, test_action, mime_image, | 117 WebIntentServiceData service = |
| 114 test_title); | 118 MakeIntentService(test_url, test_action, mime_image, test_title); |
| 115 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 119 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 116 | 120 |
| 117 intent.action = test_action_2; | 121 service.action = test_action_2; |
| 118 intent.title = test_title_2; | 122 service.title = test_title_2; |
| 119 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 123 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 120 | 124 |
| 121 // Recover stored intents from DB. | 125 // Recover stored services from DB. |
| 122 EXPECT_TRUE(IntentsTable()->GetAllWebIntents(&intents)); | 126 EXPECT_TRUE(IntentsTable()->GetAllWebIntents(&services)); |
| 123 ASSERT_EQ(2U, intents.size()); | 127 ASSERT_EQ(2U, services.size()); |
| 124 | 128 |
| 125 // WebIntentsTable does not guarantee order, so ensure order here. | 129 // WebIntentsTable does not guarantee order, so ensure order here. |
| 126 if (intents[0].type == test_action_2) | 130 if (services[0].type == test_action_2) |
| 127 std::swap(intents[0], intents[1]); | 131 std::swap(services[0], services[1]); |
| 128 | 132 |
| 129 EXPECT_EQ(intent, intents[1]); | 133 EXPECT_EQ(service, services[1]); |
| 130 | 134 |
| 131 intent.action = test_action; | 135 service.action = test_action; |
| 132 intent.title = test_title; | 136 service.title = test_title; |
| 133 EXPECT_EQ(intent, intents[0]); | 137 EXPECT_EQ(service, services[0]); |
| 134 } | 138 } |
| 135 | 139 |
| 136 TEST_F(WebIntentsTableTest, DispositionToStringMapping) { | 140 TEST_F(WebIntentsTableTest, DispositionToStringMapping) { |
| 137 WebIntentServiceData intent = MakeIntent(test_url, test_action, mime_image, | 141 WebIntentServiceData service = |
| 138 test_title); | 142 MakeIntentService(test_url, test_action, mime_image, test_title); |
| 139 intent.disposition = WebIntentServiceData::DISPOSITION_WINDOW; | 143 service.disposition = WebIntentServiceData::DISPOSITION_WINDOW; |
| 140 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | 144 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 141 | 145 |
| 142 intent = MakeIntent(test_url, test_action, mime_video, | 146 service = MakeIntentService(test_url, test_action, mime_video, test_title); |
| 143 test_title); | 147 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; |
| 144 intent.disposition = WebIntentServiceData::DISPOSITION_INLINE; | 148 EXPECT_TRUE(IntentsTable()->SetWebIntent(service)); |
| 145 EXPECT_TRUE(IntentsTable()->SetWebIntent(intent)); | |
| 146 | 149 |
| 147 std::vector<WebIntentServiceData> intents; | 150 std::vector<WebIntentServiceData> services; |
| 148 EXPECT_TRUE(IntentsTable()->GetAllWebIntents(&intents)); | 151 EXPECT_TRUE(IntentsTable()->GetAllWebIntents(&services)); |
| 149 ASSERT_EQ(2U, intents.size()); | 152 ASSERT_EQ(2U, services.size()); |
| 150 | 153 |
| 151 if (intents[0].disposition == WebIntentServiceData::DISPOSITION_WINDOW) | 154 if (services[0].disposition == WebIntentServiceData::DISPOSITION_WINDOW) |
| 152 std::swap(intents[0], intents[1]); | 155 std::swap(services[0], services[1]); |
| 153 | 156 |
| 154 EXPECT_EQ(WebIntentServiceData::DISPOSITION_INLINE, intents[0].disposition); | 157 EXPECT_EQ(WebIntentServiceData::DISPOSITION_INLINE, services[0].disposition); |
| 155 EXPECT_EQ(WebIntentServiceData::DISPOSITION_WINDOW, intents[1].disposition); | 158 EXPECT_EQ(WebIntentServiceData::DISPOSITION_WINDOW, services[1].disposition); |
| 156 } | 159 } |
| 157 } // namespace | 160 } // namespace |
| OLD | NEW |