| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/extensions/shell_window_geometry_cache.h" |
| 7 #include "chrome/browser/extensions/state_store.h" |
| 8 #include "chrome/browser/value_store/testing_value_store.h" |
| 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/test/test_browser_thread.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 static const char kExtensionId[] = "extensionid"; |
| 16 static const char kExtensionId2[] = "extensionid2"; |
| 17 static const char kWindowId[] = "windowid"; |
| 18 static const char kWindowId2[] = "windowid2"; |
| 19 |
| 20 const char kWindowGeometryKey[] = "window_geometry"; |
| 21 } // namespace |
| 22 |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 // Base class for tests. |
| 28 class ShellWindowGeometryCacheTest : public testing::Test { |
| 29 public: |
| 30 ShellWindowGeometryCacheTest() : |
| 31 ui_thread_(BrowserThread::UI, &message_loop_), |
| 32 file_thread_(BrowserThread::FILE, &message_loop_), |
| 33 value_store_(new TestingValueStore()) { |
| 34 |
| 35 state_store_.reset(new StateStore(&profile_, value_store_)); |
| 36 cache_.reset( |
| 37 new ShellWindowGeometryCache(&profile_, state_store_.get())); |
| 38 } |
| 39 |
| 40 void AddGeometryAndLoadExtension(const std::string& extension_id, |
| 41 const std::string& window_id, |
| 42 const gfx::Rect& bounds); |
| 43 void LoadExtension(const std::string& extension_id); |
| 44 void UnloadExtension(const std::string& extension_id); |
| 45 |
| 46 protected: |
| 47 TestingProfile profile_; |
| 48 MessageLoopForUI message_loop_; |
| 49 content::TestBrowserThread ui_thread_; |
| 50 content::TestBrowserThread file_thread_; |
| 51 TestingValueStore* value_store_; |
| 52 scoped_ptr<StateStore> state_store_; |
| 53 scoped_ptr<ShellWindowGeometryCache> cache_; |
| 54 }; |
| 55 |
| 56 void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension( |
| 57 const std::string& extension_id, const std::string& window_id, |
| 58 const gfx::Rect& bounds) { |
| 59 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 60 base::DictionaryValue* value = new base::DictionaryValue; |
| 61 value->SetInteger("x", bounds.x()); |
| 62 value->SetInteger("y", bounds.y()); |
| 63 value->SetInteger("w", bounds.width()); |
| 64 value->SetInteger("h", bounds.height()); |
| 65 dict->SetWithoutPathExpansion(window_id, value); |
| 66 state_store_->SetExtensionValue(extension_id, kWindowGeometryKey, |
| 67 scoped_ptr<base::Value>(dict.release())); |
| 68 |
| 69 LoadExtension(extension_id); |
| 70 } |
| 71 |
| 72 void ShellWindowGeometryCacheTest::LoadExtension( |
| 73 const std::string& extension_id) { |
| 74 cache_->OnExtensionLoaded(extension_id); |
| 75 |
| 76 // TODO(mek): not sure this is the best way to ensure that loading of data |
| 77 // from the store has actually finished for the given extension id. |
| 78 content::RunAllPendingInMessageLoop(); |
| 79 } |
| 80 |
| 81 void ShellWindowGeometryCacheTest::UnloadExtension( |
| 82 const std::string& extension_id) { |
| 83 cache_->OnExtensionUnloaded(extension_id); |
| 84 |
| 85 // TODO(mek): not sure this is the best way to ensure that saving of data to |
| 86 // the store has actually finished for the given extension id. |
| 87 content::RunAllPendingInMessageLoop(); |
| 88 } |
| 89 |
| 90 // Test getting geometry from an empty store. |
| 91 TEST_F(ShellWindowGeometryCacheTest, GetGeometryEmptyStore) { |
| 92 gfx::Rect bounds; |
| 93 ASSERT_FALSE(cache_->GetGeometry(kExtensionId, kWindowId, &bounds)); |
| 94 } |
| 95 |
| 96 // Test getting geometry for an unknown extension. |
| 97 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) { |
| 98 AddGeometryAndLoadExtension(kExtensionId, kWindowId, |
| 99 gfx::Rect(4, 5, 31, 43)); |
| 100 gfx::Rect bounds; |
| 101 ASSERT_FALSE(cache_->GetGeometry(kExtensionId2, kWindowId, &bounds)); |
| 102 } |
| 103 |
| 104 // Test getting geometry for an unknown window in a known extension. |
| 105 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) { |
| 106 AddGeometryAndLoadExtension(kExtensionId, kWindowId, |
| 107 gfx::Rect(4, 5, 31, 43)); |
| 108 gfx::Rect bounds; |
| 109 ASSERT_FALSE(cache_->GetGeometry(kExtensionId, kWindowId2, &bounds)); |
| 110 } |
| 111 |
| 112 // Test that loading geometry from the store works correctly. |
| 113 TEST_F(ShellWindowGeometryCacheTest, GetGeometryFromStore) { |
| 114 gfx::Rect bounds(4, 5, 31, 43); |
| 115 AddGeometryAndLoadExtension(kExtensionId, kWindowId, bounds); |
| 116 gfx::Rect newBounds; |
| 117 ASSERT_TRUE(cache_->GetGeometry(kExtensionId, kWindowId, &newBounds)); |
| 118 ASSERT_EQ(bounds, newBounds); |
| 119 } |
| 120 |
| 121 // Test saving geometry to the cache and state store, and reading it back. |
| 122 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryToStore) { |
| 123 const std::string extension_id(kExtensionId); |
| 124 const std::string window_id(kWindowId); |
| 125 |
| 126 // inform cache of extension |
| 127 LoadExtension(extension_id); |
| 128 |
| 129 // update geometry stored in cache |
| 130 gfx::Rect bounds(4, 5, 31, 43); |
| 131 gfx::Rect newBounds; |
| 132 cache_->SaveGeometry(extension_id, window_id, bounds); |
| 133 |
| 134 // make sure that immediately reading back geometry works |
| 135 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); |
| 136 ASSERT_EQ(bounds, newBounds); |
| 137 |
| 138 // unload extension to force cache to save data to the state store |
| 139 UnloadExtension(extension_id); |
| 140 |
| 141 // check if geometry got stored correctly in the state store |
| 142 ValueStore::ReadResult result = value_store_->Get(extension_id + "." + |
| 143 kWindowGeometryKey); |
| 144 ASSERT_FALSE(result->HasError()); |
| 145 DictionaryValue* dict; |
| 146 ASSERT_TRUE(result->settings()->GetDictionaryWithoutPathExpansion( |
| 147 extension_id + "." + kWindowGeometryKey, &dict)); |
| 148 |
| 149 ASSERT_TRUE(dict->HasKey(window_id)); |
| 150 int v; |
| 151 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); |
| 152 ASSERT_EQ(bounds.x(), v); |
| 153 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); |
| 154 ASSERT_EQ(bounds.y(), v); |
| 155 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); |
| 156 ASSERT_EQ(bounds.width(), v); |
| 157 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v)); |
| 158 ASSERT_EQ(bounds.height(), v); |
| 159 |
| 160 // check to make sure cache indeed doesn't know about this extension anymore |
| 161 ASSERT_FALSE(cache_->GetGeometry(extension_id, window_id, &newBounds)); |
| 162 |
| 163 // reload extension |
| 164 LoadExtension(extension_id); |
| 165 // and make sure the geometry got reloaded properly too |
| 166 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); |
| 167 ASSERT_EQ(bounds, newBounds); |
| 168 } |
| 169 |
| 170 } // namespace extensions |
| OLD | NEW |