Index: chrome/browser/webdata/web_intents_table_unittest.cc |
diff --git a/chrome/browser/webdata/web_intents_table_unittest.cc b/chrome/browser/webdata/web_intents_table_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..018d00ca94cd522e13260a65bfac7c97304c29d4 |
--- /dev/null |
+++ b/chrome/browser/webdata/web_intents_table_unittest.cc |
@@ -0,0 +1,103 @@ |
+// 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 <algorithm> |
+ |
+#include "base/file_util.h" |
+#include "base/scoped_temp_dir.h" |
+#include "base/string16.h" |
+#include "base/utf_string_conversions.h" |
+#include "chrome/browser/webdata/web_database.h" |
+#include "chrome/browser/webdata/web_intents_table.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "googleurl/src/gurl.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::Time; |
+ |
+class WebIntentsTableTest : public testing::Test { |
+ public: |
+ WebIntentsTableTest() : db_(NULL) {} |
James Hawkins
2011/07/26 23:35:06
Move these methods to the protected section.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ |
+ WebIntentsTable* IntentsTable() { |
+ CHECK(db_ != NULL); |
Paweł Hajdan Jr.
2011/07/26 23:30:53
Could you remove this CHECK? It's going to crash a
James Hawkins
2011/07/26 23:35:06
Don't use CHECK (or DCHECK) in testing code.
Use
groby-ooo-7-16
2011/07/27 00:09:25
Done.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ return db_->GetWebIntentsTable(); |
+ } |
+ |
+ protected: |
+ virtual void SetUp() { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ db_ = new WebDatabase(); |
+ ASSERT_TRUE(db_ != NULL); |
Paweł Hajdan Jr.
2011/07/26 23:30:53
nit: Not needed.
James Hawkins
2011/07/26 23:35:06
ASSERT_NE(...)
groby-ooo-7-16
2011/07/27 00:09:25
Done.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ ASSERT_EQ(sql::INIT_OK, |
James Hawkins
2011/07/26 23:35:06
Params should line up in the same column if wrappi
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ db_->Init(temp_dir_.path().AppendASCII("TestWebDatabase.db"))); |
+ } |
+ |
+ virtual void TearDown() { |
James Hawkins
2011/07/26 23:35:06
Use a scoped_ptr for |db_| and get rid of TearDown
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ delete db_; |
Paweł Hajdan Jr.
2011/07/26 23:30:53
Either use scoped_ptr or just do WebDatabase db_;
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ } |
+ |
+ ScopedTempDir temp_dir_; |
+ WebDatabase* db_; |
+}; |
+ |
+namespace { |
James Hawkins
2011/07/26 23:35:06
Wrap the entire test implementation (including the
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ static GURL test_url("http://google.com/"); |
James Hawkins
2011/07/26 23:35:06
Don't use static inside an unnamed namespace.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ static string16 test_action = ASCIIToUTF16("http://webintents.org/intents/share"); |
James Hawkins
2011/07/26 23:35:06
nit: 80 cols
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ static string16 mime_image = ASCIIToUTF16("image/*"); |
+ static string16 mime_video = ASCIIToUTF16("video/*"); |
+} |
+ |
+// Test we can add, retrieve, and remove intents from the database. |
+TEST_F(WebIntentsTableTest, SetGetDeleteIntent) { |
+ std::vector<WebIntentData> intents; |
+ |
+ // By default, no intents exist. |
+ EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); |
+ EXPECT_EQ(0U, intents.size()); |
+ |
+ // Now adding one. |
+ EXPECT_TRUE(IntentsTable()->SetWebIntent(test_action, mime_image, test_url)); |
+ |
+ // Make sure that intent can now be fetched |
+ EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); |
+ ASSERT_EQ(1U, intents.size()); |
+ |
+ EXPECT_EQ(test_url.spec(), intents[0].service_url.spec()); |
+ EXPECT_EQ(test_action, intents[0].action); |
+ EXPECT_EQ(mime_image, intents[0].type); |
+ |
+ // Remove the intent. |
+ EXPECT_TRUE(IntentsTable()->RemoveWebIntent(test_action, mime_image, |
+ test_url)); |
+ |
+ // Intent should now be gone. |
+ intents.clear(); |
+ EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); |
+ EXPECT_EQ(0U, intents.size()); |
+} |
+ |
+// Test we support multiple intents for the same MIME type |
+TEST_F(WebIntentsTableTest, SetMultipleIntents) { |
+ std::vector<WebIntentData> intents; |
+ |
+ EXPECT_TRUE(IntentsTable()->SetWebIntent(test_action, mime_image, test_url)); |
+ EXPECT_TRUE(IntentsTable()->SetWebIntent(test_action, mime_video, test_url)); |
+ |
+ // Recover stored intents from DB |
James Hawkins
2011/07/26 23:35:06
Period at end of sentence, here and elsewhere.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ EXPECT_TRUE(IntentsTable()->GetWebIntents(test_action, &intents)); |
+ ASSERT_EQ(2U, intents.size()); |
+ |
+ // WebIntentsTable does not guarantee order, so ensure order here. |
+ if (intents[0].type == mime_video) |
+ std::swap(intents[0],intents[1]); |
James Hawkins
2011/07/26 23:35:06
Space after comma.
groby-ooo-7-16
2011/07/27 00:09:25
Done.
|
+ |
+ EXPECT_EQ(test_url, intents[0].service_url); |
+ EXPECT_EQ(test_action, intents[0].action); |
+ EXPECT_EQ(mime_image, intents[0].type); |
+ |
+ EXPECT_EQ(test_url, intents[1].service_url); |
+ EXPECT_EQ(test_action, intents[1].action); |
+ EXPECT_EQ(mime_video, intents[1].type); |
+} |