Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Mes tests i millores
Browse files Browse the repository at this point in the history
Comprovar si existeix un thread abans de intentar fer el join, metode
per obtenir la llibreria
  • Loading branch information
bcedu committed Jan 7, 2020
1 parent e7bd1ea commit ce525de
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
11 changes: 11 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash
echo "========================"
echo "== Testing VGrive =="
echo "== Compiling tests... =="
echo "========================"
mv meson.build old_meson.build
mv test_meson.build meson.build
rm -r build
Expand All @@ -7,7 +11,14 @@ cd build
ninja
ninja install
cd ..
echo "======================"
echo "== Tests compiled! =="
echo "== Running tests... =="
echo "======================"
TESTDIR=`pwd`/tests .testbuild/bin/com.github.bcedu.vgrive
mv meson.build test_meson.build
mv old_meson.build meson.build
rm -r `pwd`/.testbuild
echo "====================="
echo "== All tests done! =="
echo "====================="
29 changes: 15 additions & 14 deletions src/VGriveClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace App {
public int changes_check_period = 10;
private Gee.HashMap<string,string>? library;

Thread<int> thread;
public Thread<int> thread;

public VGriveClient (AppController? controler=null, owned string? main_path=null, owned string? trash_path=null) {
this.app_controller = controler;
Expand All @@ -119,19 +119,16 @@ namespace App {
}

public string get_auth_uri() {
// TODO: TEST
return "https://accounts.google.com/o/oauth2/v2/auth?scope=%s&access_type=offline&redirect_uri=%s&response_type=code&client_id=%s".printf(scope, redirect, client_id);
}

public void log_message(string msg, int level=1) {
// TODO: TEST
if (this.app_controller != null && level >= log_level) {
this.app_controller.log_message(msg);
}
}

public void change_main_path(string new_path) {
// TODO: TEST
this.stop_syncing ();
this.main_path = new_path;
this.trash_path = main_path+"/.trash";
Expand All @@ -147,20 +144,17 @@ namespace App {
////////////////////////////////////////////////////////////////////////////////

private Soup.Session get_current_session() {
// TODO: TEST
if (this.session == null) {
this.session = new Soup.Session ();
}
return this.session;
}

public bool is_syncing() {
// TODO: TEST
return this.syncing;
}

public void start_syncing() {
// TODO: TEST
// Starts the process to sync files.
// If the sync was already started (`syncing` is True), nothing is done.
// The attributes `access_token` and `refresh_token` must be set with `request_credentials` or `load_local_credentials`
Expand All @@ -185,9 +179,11 @@ namespace App {
}

public void stop_syncing() {
// TODO: TEST
this.syncing = false;
int result = this.thread.join ();
if (this.thread != null) {
this.thread.join ();
this.thread = null;
}
this.log_message(_("Syncing stopped by user request"));
//this.save_library();
//this.thread.exit(1);
Expand Down Expand Up @@ -369,6 +365,7 @@ namespace App {

private void watch_local_changes() {
// TODO: TEST
// TODO: utilitzar ThreadPool?
try {
string[] dirs_to_watch = this.get_all_dirs(this.main_path);

Expand Down Expand Up @@ -488,11 +485,11 @@ namespace App {
file = File.new_for_path(credentials_file_path);
if (file.query_exists()) {
stdout.printf("Warning: file %s already exists and it will be deleted.", credentials_file_path);
try {
file.delete ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
try {
file.delete ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
}
file.create(FileCreateFlags.NONE);
FileIOStream io = file.open_readwrite();
Expand Down Expand Up @@ -1150,6 +1147,10 @@ namespace App {
*/
////////////////////////////////////////////////////////////////////////////////

public Gee.HashMap<string,string>? get_library () {
return this.library;
}

public Gee.HashMap<string,string>? load_library() {
// TODO: TEST
File f = File.new_for_path(this.main_path+"/.vgrive_library");
Expand Down
94 changes: 94 additions & 0 deletions tests/vgrive_tests.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ class TestVGrive : Gee.TestCase {
base("TestVGrive");
// add test methods
add_test(" * Test has credentials (test_has_credentials)", test_has_credentials);
add_test(" * Test get auth uri (test_get_auth_uri)", test_get_auth_uri);
add_test(" * Test if is in syncing process (test_is_syncing)", test_is_syncing);
add_test(" * Test change the main path of vgrive (test_change_main_path)", test_change_main_path);
add_test(" * Test list files (test_list_files)", test_list_files);
add_test(" * Test search files and encode for q (test_search_files_and_encode_for_q)", test_search_files_and_encode_for_q);
add_test(" * Test get file ID and then get its content (test_get_file_id_and_test_get_file_content)", test_get_file_id_and_test_get_file_content);
add_test(" * Test starting a new sync process and stop it (test_start_and_stop_syncing)", test_start_and_stop_syncing);
}

public override void set_up () {
Expand Down Expand Up @@ -65,6 +69,50 @@ class TestVGrive : Gee.TestCase {
assert (this.client.has_credentials () == true);
}

public void test_get_auth_uri () {
string res = this.client.get_auth_uri ();
assert (res == "https://accounts.google.com/o/oauth2/v2/auth?scope=%s&access_type=offline&redirect_uri=%s&response_type=code&client_id=%s".printf (this.client.scope, this.client.redirect, this.client.client_id));
}

public void test_is_syncing () {
// When it's started is not syncing
bool is_syncing = this.client.is_syncing ();
assert (is_syncing == false);
// Start sync process
this.client.syncing = true;
is_syncing = this.client.is_syncing ();
assert (is_syncing == true);
// Stop sync process
this.client.syncing = false;
is_syncing = this.client.is_syncing ();
assert (is_syncing == false);
}

public void test_change_main_path () {
/*
* Test que canvia el directori principal de vgrive a "VGriveTEST_ALT". Al canviar el directori principal es fan les següents accions:
* - Es posa l'atribut 'syncing' a false
* - Es canvia l'atribut 'main_path'
* - Es canvia l'atribut 'trash_path'
* - Es posa l'atribut 'library' a null
*
* Un cop canviat el torna a canviar al original (VGriveTEST)
* */
// Primer posem el syncing a true per comprovar realment que el metode el posa a false
this.client.syncing = true;
// Ara canviem el directori principal
this.client.change_main_path (mainpath+"_ALT");
assert(this.client.syncing == false);
assert(this.client.main_path == mainpath+"_ALT");
assert(this.client.trash_path == mainpath+"_ALT/.trash");
assert(this.client.get_library () == null);
this.client.change_main_path (mainpath);
assert(this.client.syncing == false);
assert(this.client.main_path == mainpath);
assert(this.client.trash_path == mainpath+"/.trash");
assert(this.client.get_library () == null);
}

public void test_list_files () {
/*
* Test que comprova que quan demanem un llistst de fitxers a la API ens retorna lo esperat, que es:
Expand Down Expand Up @@ -177,5 +225,51 @@ class TestVGrive : Gee.TestCase {
assert_strings (content, get_fixture_content ("muse.txt", false));
}

public void test_start_and_stop_syncing () {
/*
* Test que inicia el proces de sincronitzacio. Al iniciarse es fa el següent:
* - Es posa a true la variable 'syncing'
* - Crea el directori main_path si no existeix
* - Crea el directori trash_path si no existeix
* - Inicialitza la llibreria (atribut 'library')
* - Inicia un nou thread que executa el métode 'sync_files'. Aquest thread es guarda al atribut 'thread'.
*
*
* Després es comprova que no hi hagi hagut cap canvi, ja que nongu ha tocat res es suposa.
*
* Finalment atura el procés amb 'stop_syncing'. Al fer-ho es fan les següents accions:
* - Es posa a false la variable 'syncing'
* - Es fa el join amb el thread del atribut 'thread'
*
* */
// Primer eliminem els directoris main_path i trash_path perque es tornin a crear despres.
// Ja existien perque al crear el VGriveClient es crean.
File mainDir = File.new_for_path (mainpath);
File trashDir = File.new_for_path (mainpath+"/.trash");
assert (mainDir.query_exists () == true);
assert (trashDir.query_exists () == true);
try {
trashDir.delete ();
mainDir.delete ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
assert (mainDir.query_exists () == false);
assert (trashDir.query_exists () == false);
// Iniciem la sincronitzacio. Comprovarem que fa les 5 accions que esperem
this.client.start_syncing ();
assert (this.client.syncing == true);
assert (mainDir.query_exists () == true);
assert (trashDir.query_exists () == true);
assert (this.client.get_library () != null);
assert (this.client.thread != null);
// Comprovem que no hi hagi hagut cap canvi detectat
assert (this.client.change_detected == false);
// Parem de sincronitzar i comprovem que ha fet les dos accions que ha de realitzar
this.client.stop_syncing ();
assert (this.client.syncing == false);
assert (this.client.thread == null);
}

}

0 comments on commit ce525de

Please sign in to comment.