Chromium Code Reviews| 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 class TestShellWindowGeometryCache : public ShellWindowGeometryCache | |
| 28 { | |
| 29 public: | |
| 30 TestShellWindowGeometryCache(Profile* profile, StateStore* state_store) : | |
| 31 ShellWindowGeometryCache(profile, state_store) {} | |
| 32 | |
| 33 using ShellWindowGeometryCache::OnExtensionLoaded; | |
| 34 using ShellWindowGeometryCache::OnExtensionUnloaded; | |
|
asargent_no_longer_on_chrome
2012/08/28 18:49:41
As mentioned in person, the rest of chrome code us
Marijn Kruisselbrink
2012/08/28 22:14:58
Done.
| |
| 35 }; | |
| 36 | |
| 37 // Base class for tests. | |
| 38 class ShellWindowGeometryCacheTest : public testing::Test { | |
| 39 public: | |
| 40 ShellWindowGeometryCacheTest() : | |
| 41 ui_thread_(BrowserThread::UI, &message_loop_), | |
| 42 file_thread_(BrowserThread::FILE, &message_loop_), | |
| 43 value_store_(new TestingValueStore()) { | |
| 44 | |
| 45 state_store_.reset(new StateStore(&profile_, value_store_)); | |
| 46 cache_.reset( | |
| 47 new TestShellWindowGeometryCache(&profile_, state_store_.get())); | |
| 48 } | |
| 49 | |
| 50 void AddGeometryAndLoadExtension(const std::string& extension_id, | |
| 51 const std::string& window_id, | |
| 52 const gfx::Rect& bounds); | |
| 53 void LoadExtension(const std::string& extension_id); | |
| 54 void UnloadExtension(const std::string& extension_id); | |
| 55 | |
| 56 protected: | |
| 57 TestingProfile profile_; | |
| 58 MessageLoopForUI message_loop_; | |
| 59 content::TestBrowserThread ui_thread_; | |
| 60 content::TestBrowserThread file_thread_; | |
| 61 TestingValueStore* value_store_; | |
| 62 scoped_ptr<StateStore> state_store_; | |
| 63 scoped_ptr<TestShellWindowGeometryCache> cache_; | |
| 64 }; | |
| 65 | |
| 66 void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension( | |
| 67 const std::string& extension_id, const std::string& window_id, | |
| 68 const gfx::Rect& bounds) { | |
| 69 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
| 70 base::DictionaryValue* value = new base::DictionaryValue; | |
| 71 value->SetInteger("x", bounds.x()); | |
| 72 value->SetInteger("y", bounds.y()); | |
| 73 value->SetInteger("w", bounds.width()); | |
| 74 value->SetInteger("h", bounds.height()); | |
| 75 dict->SetWithoutPathExpansion(window_id, value); | |
| 76 state_store_->SetExtensionValue(extension_id, kWindowGeometryKey, | |
| 77 scoped_ptr<base::Value>(dict.release())); | |
| 78 | |
| 79 LoadExtension(extension_id); | |
| 80 } | |
| 81 | |
| 82 void ShellWindowGeometryCacheTest::LoadExtension( | |
| 83 const std::string& extension_id) { | |
| 84 cache_->OnExtensionLoaded(extension_id); | |
| 85 | |
| 86 // TODO(mek): not sure this is the best way to ensure that loading of data | |
| 87 // from the store has actually finished for the given extension id. | |
| 88 content::RunAllPendingInMessageLoop(); | |
| 89 } | |
| 90 | |
| 91 void ShellWindowGeometryCacheTest::UnloadExtension( | |
| 92 const std::string& extension_id) { | |
| 93 cache_->OnExtensionUnloaded(extension_id); | |
| 94 | |
| 95 // TODO(mek): not sure this is the best way to ensure that saving of data to | |
| 96 // the store has actually finished for the given extension id. | |
| 97 content::RunAllPendingInMessageLoop(); | |
| 98 } | |
| 99 | |
| 100 // Test getting geometry from an empty store. | |
| 101 TEST_F(ShellWindowGeometryCacheTest, GetWindowGeometryEmptyStore) { | |
| 102 gfx::Rect bounds; | |
| 103 ASSERT_FALSE(cache_->GetWindowGeometry(kExtensionId, kWindowId, &bounds)); | |
| 104 } | |
| 105 | |
| 106 // Test getting geometry for an unknown extension. | |
| 107 TEST_F(ShellWindowGeometryCacheTest, GetWindowGeometryUnkownExtension) { | |
| 108 AddGeometryAndLoadExtension(kExtensionId, kWindowId, | |
| 109 gfx::Rect(4, 5, 31, 43)); | |
| 110 gfx::Rect bounds; | |
| 111 ASSERT_FALSE(cache_->GetWindowGeometry(kExtensionId2, kWindowId, &bounds)); | |
| 112 } | |
| 113 | |
| 114 // Test getting geometry for an unknown window in a known extension. | |
| 115 TEST_F(ShellWindowGeometryCacheTest, GetWindowGeometryUnkownWindow) { | |
| 116 AddGeometryAndLoadExtension(kExtensionId, kWindowId, | |
| 117 gfx::Rect(4, 5, 31, 43)); | |
| 118 gfx::Rect bounds; | |
| 119 ASSERT_FALSE(cache_->GetWindowGeometry(kExtensionId, kWindowId2, &bounds)); | |
| 120 } | |
| 121 | |
| 122 // Test that loading geometry from the store works correctly. | |
| 123 TEST_F(ShellWindowGeometryCacheTest, GetWindowGeometryFromStore) { | |
| 124 gfx::Rect bounds(4, 5, 31, 43); | |
| 125 AddGeometryAndLoadExtension(kExtensionId, kWindowId, bounds); | |
| 126 gfx::Rect newBounds; | |
| 127 ASSERT_TRUE(cache_->GetWindowGeometry(kExtensionId, kWindowId, &newBounds)); | |
| 128 ASSERT_EQ(bounds, newBounds); | |
| 129 } | |
| 130 | |
| 131 // Test saving geometry to the cache and state store, and reading it back. | |
| 132 TEST_F(ShellWindowGeometryCacheTest, SaveWindowGeometryToStore) { | |
| 133 const std::string extension_id(kExtensionId); | |
| 134 const std::string window_id(kWindowId); | |
| 135 | |
| 136 // inform cache of extension | |
| 137 LoadExtension(extension_id); | |
| 138 | |
| 139 // update geometry stored in cache | |
| 140 gfx::Rect bounds(4, 5, 31, 43); | |
| 141 gfx::Rect newBounds; | |
| 142 cache_->SaveWindowGeometry(extension_id, window_id, bounds); | |
| 143 | |
| 144 // make sure that immediately reading back geometry works | |
| 145 ASSERT_TRUE(cache_->GetWindowGeometry(extension_id, window_id, &newBounds)); | |
| 146 ASSERT_EQ(bounds, newBounds); | |
| 147 | |
| 148 // unload extension to force cache to save data to the state store | |
| 149 UnloadExtension(extension_id); | |
| 150 | |
| 151 // check if geometry got stored correctly in the state store | |
| 152 ValueStore::ReadResult result = value_store_->Get(extension_id + "." + | |
| 153 kWindowGeometryKey); | |
| 154 ASSERT_FALSE(result->HasError()); | |
| 155 DictionaryValue* dict; | |
| 156 ASSERT_TRUE(result->settings()->GetDictionaryWithoutPathExpansion( | |
| 157 extension_id + "." + kWindowGeometryKey, &dict)); | |
| 158 | |
| 159 ASSERT_TRUE(dict->HasKey(window_id)); | |
| 160 int v; | |
| 161 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); | |
| 162 ASSERT_EQ(bounds.x(), v); | |
| 163 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); | |
| 164 ASSERT_EQ(bounds.y(), v); | |
| 165 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); | |
| 166 ASSERT_EQ(bounds.width(), v); | |
| 167 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v)); | |
| 168 ASSERT_EQ(bounds.height(), v); | |
| 169 | |
| 170 // check to make sure cache indeed doesn't know about this extension anymore | |
| 171 ASSERT_FALSE(cache_->GetWindowGeometry(extension_id, window_id, &newBounds)); | |
| 172 | |
| 173 // reload extension | |
| 174 LoadExtension(extension_id); | |
| 175 // and make sure the geometry got reloaded properly too | |
| 176 ASSERT_TRUE(cache_->GetWindowGeometry(extension_id, window_id, &newBounds)); | |
| 177 ASSERT_EQ(bounds, newBounds); | |
| 178 } | |
| 179 | |
| 180 } // namespace extensions | |
| OLD | NEW |