Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.MediumTest;
     21 import android.webkit.WebStorage;
     22 
     23 /**
     24  * This is a series of unit tests for the WebStorageSizeManager class.
     25  *
     26  */
     27 @MediumTest
     28 public class WebStorageSizeManagerUnitTests extends AndroidTestCase {
     29     // Used for testing the out-of-space callbacks.
     30     private long mNewQuota;
     31     // Callback functor that sets a new quota in case of out-of-space scenarios.
     32     private class MockQuotaUpdater implements WebStorage.QuotaUpdater {
     33         public void updateQuota(long newQuota) {
     34             mNewQuota = newQuota;
     35         }
     36     }
     37 
     38     // Mock the DiskInfo.
     39     private class MockDiskInfo implements WebStorageSizeManager.DiskInfo {
     40         private long mFreeSize;
     41         private long mTotalSize;
     42 
     43         public long getFreeSpaceSizeBytes() {
     44             return mFreeSize;
     45         }
     46 
     47         public long getTotalSizeBytes() {
     48             return mTotalSize;
     49         }
     50 
     51         public void setFreeSpaceSizeBytes(long freeSize) {
     52             mFreeSize = freeSize;
     53         }
     54 
     55         public void setTotalSizeBytes(long totalSize) {
     56             mTotalSize = totalSize;
     57         }
     58     }
     59 
     60     // Mock the AppCacheInfo
     61     public class MockAppCacheInfo implements WebStorageSizeManager.AppCacheInfo {
     62         private long mAppCacheSize;
     63 
     64         public long getAppCacheSizeBytes() {
     65             return mAppCacheSize;
     66         }
     67 
     68         public void setAppCacheSizeBytes(long appCacheSize) {
     69             mAppCacheSize = appCacheSize;
     70         }
     71     }
     72 
     73     private MockQuotaUpdater mQuotaUpdater = new MockQuotaUpdater();
     74     private final MockDiskInfo mDiskInfo = new MockDiskInfo();
     75     private final MockAppCacheInfo mAppCacheInfo = new MockAppCacheInfo();
     76     // Utility for making size computations easier to read.
     77     private long bytes(double megabytes) {
     78         return (new Double(megabytes * 1024 * 1024)).longValue();
     79     }
     80     /**
     81      * Test the onExceededDatabaseQuota and onReachedMaxAppCacheSize callbacks
     82      */
     83     public void testCallbacks() {
     84         long totalUsedQuota = 0;
     85         final long quotaIncrease = WebStorageSizeManager.QUOTA_INCREASE_STEP;  // 1MB
     86 
     87         // We have 75 MB total, 24MB free so the global limit will be 12 MB.
     88         mDiskInfo.setTotalSizeBytes(bytes(75));
     89         mDiskInfo.setFreeSpaceSizeBytes(bytes(24));
     90         // We have an appcache file size of 0 MB.
     91         mAppCacheInfo.setAppCacheSizeBytes(0);
     92         // Create the manager.
     93         WebStorageSizeManager manager = new WebStorageSizeManager(null, mDiskInfo, mAppCacheInfo);
     94         // We add origin 1.
     95         long origin1Quota = 0;
     96         long origin1EstimatedSize = bytes(3.5);
     97         manager.onExceededDatabaseQuota("1", "1", origin1Quota, origin1EstimatedSize, totalUsedQuota, mQuotaUpdater);
     98         assertEquals(origin1EstimatedSize, mNewQuota);
     99         origin1Quota = mNewQuota;
    100         totalUsedQuota += origin1Quota;
    101 
    102         // We add origin 2.
    103         long origin2Quota = 0;
    104         long origin2EstimatedSize = bytes(2.5);
    105         manager.onExceededDatabaseQuota("2", "2", origin2Quota, origin2EstimatedSize, totalUsedQuota, mQuotaUpdater);
    106         assertEquals(origin2EstimatedSize, mNewQuota);
    107         origin2Quota = mNewQuota;
    108         totalUsedQuota += origin2Quota;
    109 
    110         // Origin 1 runs out of space.
    111         manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
    112         assertEquals(origin1EstimatedSize + quotaIncrease, mNewQuota);
    113         totalUsedQuota -= origin1Quota;
    114         origin1Quota = mNewQuota;
    115         totalUsedQuota += origin1Quota;
    116 
    117         // Origin 2 runs out of space.
    118         manager.onExceededDatabaseQuota("2", "2", origin2Quota, 0, totalUsedQuota, mQuotaUpdater);
    119         assertEquals(origin2EstimatedSize + quotaIncrease, mNewQuota);
    120         totalUsedQuota -= origin2Quota;
    121         origin2Quota = mNewQuota;
    122         totalUsedQuota += origin2Quota;
    123 
    124         // We add origin 3. TotalUsedQuota is 8 (3.5 + 2.5 + 1 + 1). AppCacheMaxSize is 3 (12 / 4).
    125         // So we have 1 MB free.
    126         long origin3Quota = 0;
    127         long origin3EstimatedSize = bytes(5);
    128         manager.onExceededDatabaseQuota("3", "3", origin3Quota, origin3EstimatedSize, totalUsedQuota, mQuotaUpdater);
    129         assertEquals(0, mNewQuota);  // We cannot satisfy the estimatedSize
    130         origin3Quota = mNewQuota;
    131         totalUsedQuota += origin3Quota;
    132 
    133         // Origin 1 runs out of space again. It should increase it's quota to take the last 1MB.
    134         manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
    135         assertEquals(origin1Quota + quotaIncrease, mNewQuota);
    136         totalUsedQuota -= origin1Quota;
    137         origin1Quota = mNewQuota;
    138         totalUsedQuota += origin1Quota;
    139 
    140         // Origin 1 runs out of space again. It should inow fail to increase in size.
    141         manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
    142         assertEquals(origin1Quota, mNewQuota);
    143 
    144         // We try adding a new origin. Which will fail.
    145         manager.onExceededDatabaseQuota("4", "4", 0, bytes(1), totalUsedQuota, mQuotaUpdater);
    146         assertEquals(0, mNewQuota);
    147 
    148         // AppCache size increases to 2MB...
    149         mAppCacheInfo.setAppCacheSizeBytes(bytes(2));
    150         // ... and wants 2MB more. Fail.
    151         manager.onReachedMaxAppCacheSize(bytes(2), totalUsedQuota, mQuotaUpdater);
    152         assertEquals(0, mNewQuota);
    153 
    154         // The user nukes origin 2
    155         totalUsedQuota -= origin2Quota;
    156         origin2Quota = 0;
    157         // TotalUsedQuota is 5.5 (9 - 3.5). AppCacheMaxSize is 3. AppCacheSize is 2.
    158         // AppCache wants 1.5MB more
    159         manager.onReachedMaxAppCacheSize(bytes(1.5), totalUsedQuota, mQuotaUpdater);
    160         mAppCacheInfo.setAppCacheSizeBytes(mAppCacheInfo.getAppCacheSizeBytes() + bytes(2.5));
    161         assertEquals(mAppCacheInfo.getAppCacheSizeBytes(), mNewQuota - WebStorageSizeManager.APPCACHE_MAXSIZE_PADDING);
    162 
    163         // We try adding a new origin. This time we succeed.
    164         // TotalUsedQuota is 5.5. AppCacheMaxSize is 5.0. So we have 12 - 10.5 = 1.5 available.
    165         long origin4Quota = 0;
    166         long origin4EstimatedSize = bytes(1.5);
    167         manager.onExceededDatabaseQuota("4", "4", origin4Quota, origin4EstimatedSize, totalUsedQuota, mQuotaUpdater);
    168         assertEquals(bytes(1.5), mNewQuota);
    169         origin4Quota = mNewQuota;
    170         totalUsedQuota += origin4Quota;
    171     }
    172     /**
    173      * Test the application caches max size calculator.
    174      */
    175     public void testCalculateGlobalLimit() {
    176         long fileSystemSize = 78643200;  // 75 MB
    177         long freeSpaceSize = 25165824;  // 24 MB
    178         long maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    179         assertEquals(12582912, maxSize);  // 12MB
    180 
    181         fileSystemSize = 78643200;  // 75 MB
    182         freeSpaceSize = 60 * 1024 * 1024;  // 60MB
    183         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    184         assertEquals(19922944, maxSize);  // 19MB
    185 
    186         fileSystemSize = 8589934592L;  // 8 GB
    187         freeSpaceSize = 4294967296L;  // 4 GB
    188         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    189         assertEquals(536870912L, maxSize);  // 512 MB
    190 
    191         fileSystemSize = -14;
    192         freeSpaceSize = 21;
    193         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    194         assertEquals(0, maxSize);
    195 
    196         fileSystemSize = 100;
    197         freeSpaceSize = 101;
    198         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    199         assertEquals(0, maxSize);
    200 
    201         fileSystemSize = 3774873; // ~4.2 MB
    202         freeSpaceSize = 2560000;  // ~2.4 MB
    203         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    204         assertEquals(2097152, maxSize);  // 2 MB
    205 
    206         fileSystemSize = 4404019; // ~4.2 MB
    207         freeSpaceSize = 3774873;  // ~3.6 MB
    208         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    209         assertEquals(2097152, maxSize);  // 2 MB
    210 
    211         fileSystemSize = 4404019; // ~4.2 MB
    212         freeSpaceSize = 4404019;  // ~4.2 MB
    213         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    214         assertEquals(3145728, maxSize);  // 3 MB
    215 
    216         fileSystemSize = 1048576; // 1 MB
    217         freeSpaceSize = 1048575;  // 1 MB - 1 byte
    218         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    219         assertEquals(0, maxSize);
    220 
    221         fileSystemSize = 3774873; // ~3.6 MB
    222         freeSpaceSize = 2097151;  // 2 MB - 1 byte
    223         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    224         assertEquals(0, maxSize);
    225 
    226         fileSystemSize = 3774873; // ~3.6 MB
    227         freeSpaceSize = 2097151;  // 2 MB
    228         maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
    229         assertEquals(0, maxSize);
    230     }
    231 
    232     public void testManyDatabasesOnOneOrigin() {
    233         // This test ensures that if an origin creates more than one database, the quota that is
    234         // assigned to the origin after the second creation is enough to satisfy all databases
    235         // under that origin.
    236         // See b/2417477.
    237 
    238         long totalUsedQuota = 0;
    239         mDiskInfo.setTotalSizeBytes(bytes(100));
    240         mDiskInfo.setFreeSpaceSizeBytes(bytes(100));
    241         // This should give us a storage area of 13MB, with 3.25MB for appcache and 9.75MB for
    242         // databases.
    243         assertEquals(bytes(13), WebStorageSizeManager.calculateGlobalLimit(
    244                 mDiskInfo.getTotalSizeBytes(), mDiskInfo.getFreeSpaceSizeBytes()));
    245 
    246         // We have an appcache file size of 0 MB.
    247         mAppCacheInfo.setAppCacheSizeBytes(0);
    248 
    249         // Create the manager.
    250         WebStorageSizeManager manager = new WebStorageSizeManager(null, mDiskInfo, mAppCacheInfo);
    251 
    252         // We add an origin.
    253         long originQuota = 0;
    254         long database1EstimatedSize = bytes(2);
    255         manager.onExceededDatabaseQuota("1", "1", originQuota, database1EstimatedSize,
    256                 totalUsedQuota, mQuotaUpdater);
    257         assertEquals(database1EstimatedSize, mNewQuota);
    258         originQuota = mNewQuota;
    259         totalUsedQuota = originQuota;
    260 
    261         // Now try to create a new database under the origin, by invoking onExceededDatabaseQuota
    262         // again. This time, request more space than the old quota + the quota increase step.
    263         long database2EstimatedSize = bytes(3.5);
    264         manager.onExceededDatabaseQuota("1", "2", originQuota, database2EstimatedSize,
    265                 totalUsedQuota, mQuotaUpdater);
    266         assertEquals(database1EstimatedSize + database2EstimatedSize, mNewQuota);
    267         originQuota = mNewQuota;
    268         totalUsedQuota = originQuota;
    269 
    270         // Create another database, but this time use a size that will overflow the space on the
    271         // device. It should be denied.
    272         long database3EstimatedSize = bytes(50);
    273         manager.onExceededDatabaseQuota("1", "3", originQuota, database3EstimatedSize,
    274                 totalUsedQuota, mQuotaUpdater);
    275         assertEquals(originQuota, mNewQuota);
    276 
    277         // Create another database. This time, request less than the old quota.
    278         long database4EstimatedSize = bytes(2);
    279         manager.onExceededDatabaseQuota("1", "4", originQuota, database4EstimatedSize,
    280                 totalUsedQuota, mQuotaUpdater);
    281         assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize,
    282                 mNewQuota);
    283         originQuota = mNewQuota;
    284         totalUsedQuota = originQuota;
    285 
    286         // Now have the first database overflow it's quota. We should get 1 more MB.
    287         manager.onExceededDatabaseQuota("1", "1", originQuota, 0, totalUsedQuota, mQuotaUpdater);
    288         assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize +
    289                 bytes(1), mNewQuota);
    290         originQuota = mNewQuota;
    291         totalUsedQuota = originQuota;
    292 
    293         // Create a db under the origin that uses a quota less than the usual quota increase step.
    294         long database5EstimatedSize = bytes(0.5);
    295         manager.onExceededDatabaseQuota("1", "5", originQuota, database5EstimatedSize,
    296                 totalUsedQuota, mQuotaUpdater);
    297         assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize +
    298                 bytes(1) + database5EstimatedSize, mNewQuota);
    299     }
    300 }
    301