1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/healthreport/MockHealthReportSQLiteOpenHelper.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,172 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.healthreport; 1.8 + 1.9 +import java.io.File; 1.10 + 1.11 +import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage.HealthReportSQLiteOpenHelper; 1.12 + 1.13 +import android.content.Context; 1.14 +import android.database.sqlite.SQLiteDatabase; 1.15 + 1.16 +public class MockHealthReportSQLiteOpenHelper extends HealthReportSQLiteOpenHelper { 1.17 + private int version; 1.18 + 1.19 + public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name) { 1.20 + super(context, fakeProfileDirectory, name); 1.21 + version = HealthReportSQLiteOpenHelper.CURRENT_VERSION; 1.22 + } 1.23 + 1.24 + public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name, int version) { 1.25 + super(context, fakeProfileDirectory, name, version); 1.26 + this.version = version; 1.27 + } 1.28 + 1.29 + @Override 1.30 + public void onCreate(SQLiteDatabase db) { 1.31 + if (version == HealthReportSQLiteOpenHelper.CURRENT_VERSION) { 1.32 + super.onCreate(db); 1.33 + } else if (version == 4) { 1.34 + onCreateSchemaVersion4(db); 1.35 + } else { 1.36 + throw new IllegalStateException("Unknown version number, " + version + "."); 1.37 + } 1.38 + } 1.39 + 1.40 + // Copy-pasta from HealthReportDatabaseStorage.onCreate from v4. 1.41 + public void onCreateSchemaVersion4(SQLiteDatabase db) { 1.42 + db.beginTransaction(); 1.43 + try { 1.44 + db.execSQL("CREATE TABLE addons (id INTEGER PRIMARY KEY AUTOINCREMENT, " + 1.45 + " body TEXT, " + 1.46 + " UNIQUE (body) " + 1.47 + ")"); 1.48 + 1.49 + db.execSQL("CREATE TABLE environments (id INTEGER PRIMARY KEY AUTOINCREMENT, " + 1.50 + " hash TEXT, " + 1.51 + " profileCreation INTEGER, " + 1.52 + " cpuCount INTEGER, " + 1.53 + " memoryMB INTEGER, " + 1.54 + " isBlocklistEnabled INTEGER, " + 1.55 + " isTelemetryEnabled INTEGER, " + 1.56 + " extensionCount INTEGER, " + 1.57 + " pluginCount INTEGER, " + 1.58 + " themeCount INTEGER, " + 1.59 + " architecture TEXT, " + 1.60 + " sysName TEXT, " + 1.61 + " sysVersion TEXT, " + 1.62 + " vendor TEXT, " + 1.63 + " appName TEXT, " + 1.64 + " appID TEXT, " + 1.65 + " appVersion TEXT, " + 1.66 + " appBuildID TEXT, " + 1.67 + " platformVersion TEXT, " + 1.68 + " platformBuildID TEXT, " + 1.69 + " os TEXT, " + 1.70 + " xpcomabi TEXT, " + 1.71 + " updateChannel TEXT, " + 1.72 + " addonsID INTEGER, " + 1.73 + " FOREIGN KEY (addonsID) REFERENCES addons(id) ON DELETE RESTRICT, " + 1.74 + " UNIQUE (hash) " + 1.75 + ")"); 1.76 + 1.77 + db.execSQL("CREATE TABLE measurements (id INTEGER PRIMARY KEY AUTOINCREMENT, " + 1.78 + " name TEXT, " + 1.79 + " version INTEGER, " + 1.80 + " UNIQUE (name, version) " + 1.81 + ")"); 1.82 + 1.83 + db.execSQL("CREATE TABLE fields (id INTEGER PRIMARY KEY AUTOINCREMENT, " + 1.84 + " measurement INTEGER, " + 1.85 + " name TEXT, " + 1.86 + " flags INTEGER, " + 1.87 + " FOREIGN KEY (measurement) REFERENCES measurements(id) ON DELETE CASCADE, " + 1.88 + " UNIQUE (measurement, name)" + 1.89 + ")"); 1.90 + 1.91 + db.execSQL("CREATE TABLE events_integer (" + 1.92 + " date INTEGER, " + 1.93 + " env INTEGER, " + 1.94 + " field INTEGER, " + 1.95 + " value INTEGER, " + 1.96 + " FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " + 1.97 + " FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" + 1.98 + ")"); 1.99 + 1.100 + db.execSQL("CREATE TABLE events_textual (" + 1.101 + " date INTEGER, " + 1.102 + " env INTEGER, " + 1.103 + " field INTEGER, " + 1.104 + " value TEXT, " + 1.105 + " FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " + 1.106 + " FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" + 1.107 + ")"); 1.108 + 1.109 + db.execSQL("CREATE INDEX idx_events_integer_date_env_field ON events_integer (date, env, field)"); 1.110 + db.execSQL("CREATE INDEX idx_events_textual_date_env_field ON events_textual (date, env, field)"); 1.111 + 1.112 + db.execSQL("CREATE VIEW events AS " + 1.113 + "SELECT date, env, field, value FROM events_integer " + 1.114 + "UNION ALL " + 1.115 + "SELECT date, env, field, value FROM events_textual"); 1.116 + 1.117 + db.execSQL("CREATE VIEW named_events AS " + 1.118 + "SELECT date, " + 1.119 + " environments.hash AS environment, " + 1.120 + " measurements.name AS measurement_name, " + 1.121 + " measurements.version AS measurement_version, " + 1.122 + " fields.name AS field_name, " + 1.123 + " fields.flags AS field_flags, " + 1.124 + " value FROM " + 1.125 + "events JOIN environments ON events.env = environments.id " + 1.126 + " JOIN fields ON events.field = fields.id " + 1.127 + " JOIN measurements ON fields.measurement = measurements.id"); 1.128 + 1.129 + db.execSQL("CREATE VIEW named_fields AS " + 1.130 + "SELECT measurements.name AS measurement_name, " + 1.131 + " measurements.id AS measurement_id, " + 1.132 + " measurements.version AS measurement_version, " + 1.133 + " fields.name AS field_name, " + 1.134 + " fields.id AS field_id, " + 1.135 + " fields.flags AS field_flags " + 1.136 + "FROM fields JOIN measurements ON fields.measurement = measurements.id"); 1.137 + 1.138 + db.execSQL("CREATE VIEW current_measurements AS " + 1.139 + "SELECT name, MAX(version) AS version FROM measurements GROUP BY name"); 1.140 + 1.141 + // createAddonsEnvironmentsView(db): 1.142 + db.execSQL("CREATE VIEW environments_with_addons AS " + 1.143 + "SELECT e.id AS id, " + 1.144 + " e.hash AS hash, " + 1.145 + " e.profileCreation AS profileCreation, " + 1.146 + " e.cpuCount AS cpuCount, " + 1.147 + " e.memoryMB AS memoryMB, " + 1.148 + " e.isBlocklistEnabled AS isBlocklistEnabled, " + 1.149 + " e.isTelemetryEnabled AS isTelemetryEnabled, " + 1.150 + " e.extensionCount AS extensionCount, " + 1.151 + " e.pluginCount AS pluginCount, " + 1.152 + " e.themeCount AS themeCount, " + 1.153 + " e.architecture AS architecture, " + 1.154 + " e.sysName AS sysName, " + 1.155 + " e.sysVersion AS sysVersion, " + 1.156 + " e.vendor AS vendor, " + 1.157 + " e.appName AS appName, " + 1.158 + " e.appID AS appID, " + 1.159 + " e.appVersion AS appVersion, " + 1.160 + " e.appBuildID AS appBuildID, " + 1.161 + " e.platformVersion AS platformVersion, " + 1.162 + " e.platformBuildID AS platformBuildID, " + 1.163 + " e.os AS os, " + 1.164 + " e.xpcomabi AS xpcomabi, " + 1.165 + " e.updateChannel AS updateChannel, " + 1.166 + " addons.body AS addonsBody " + 1.167 + "FROM environments AS e, addons " + 1.168 + "WHERE e.addonsID = addons.id"); 1.169 + 1.170 + db.setTransactionSuccessful(); 1.171 + } finally { 1.172 + db.endTransaction(); 1.173 + } 1.174 + } 1.175 +}